freezed icon indicating copy to clipboard operation
freezed copied to clipboard

why 'A freezed union cannot have private constructors'?

Open IlyaMax opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. I use flutter bloc and freezed for events. but some of them are used internally in bloc only so it would be very useful if it could be possible to hide them.

Is there some explanation why this made like that? Maybe it causes any problems?

IlyaMax avatar Aug 24 '22 14:08 IlyaMax

Because it's not possible to have private named parameters.

So freezed cannot generate something like when(_union: ..., _union2: ...)

rrousselGit avatar Aug 24 '22 14:08 rrousselGit

But what if it would be accessible in when/map/etc but we could NOT create instance of it? Again for events it looks not bad, but maybe I miss something

IlyaMax avatar Aug 24 '22 14:08 IlyaMax

I mean generator looks for constructors names with _ and instead of creating when(_union: ..., _union2: ...) it generateswhen(union: ..., union2: ...)

IlyaMax avatar Aug 24 '22 14:08 IlyaMax

We could, yes.
But then that'd mean the private constructors would leak into the public API.

In that case, they might as well be public

rrousselGit avatar Sep 27 '22 14:09 rrousselGit

@rrousselGit I understand that some api will leak but at least we could forbid the construction of models. So we could create instances of models within library only

IlyaMax avatar Sep 27 '22 14:09 IlyaMax

You could annotate the constructor with @visibleForTesting then

That would achieve roughly the same thing.

rrousselGit avatar Sep 27 '22 19:09 rrousselGit

Yeap, thx, I think that's enough for my case

IlyaMax avatar Sep 27 '22 21:09 IlyaMax

Closing in favor of https://github.com/rrousselGit/freezed/issues/925

rrousselGit avatar May 24 '23 14:05 rrousselGit