You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched the issues and couldn't find a related discussion, but apologies if I missed it. I'm wondering if there has been consideration for allowing factories in destructure/object patterns?
A motivating example for this is the following sealed class hiearchy:
While it's certainly possible to just make Circle and Square public, the beauty of redirecting factories IME is that it improves the discoverability of subclasses in ADTs, where the enumeration of all values is visible on the base class. It would be great if, in the same way these classes are constructed, they could likewise be destructured which would reduce confusion and developer cycles.
The text was updated successfully, but these errors were encountered:
In general, I don't think factory constructors would work well with pattern matching. The problem is that from the declarations, all we know is that Shape.circle and Shape.square both return Shape. The exhaustiveness checker doesn't know that the former returns _Circle and the latter _Square. In the absence of that, it doesn't know those cases are exhaustive.
In theory, it could know this because these happen to be const constructors and it could the examine the body of the constructor. But that feels pretty brittle to me.
I think you're better off just making the two subclasses public and let the user construct them directly. If we ever do #336, then you could nest the declarations of Circle and Square directly inside Shape, which would give you the discoverability you want and work well with pattern matching.
I searched the issues and couldn't find a related discussion, but apologies if I missed it. I'm wondering if there has been consideration for allowing factories in destructure/object patterns?
A motivating example for this is the following sealed class hiearchy:
While it's certainly possible to just make
Circle
andSquare
public, the beauty of redirecting factories IME is that it improves the discoverability of subclasses in ADTs, where the enumeration of all values is visible on the base class. It would be great if, in the same way these classes are constructed, they could likewise be destructured which would reduce confusion and developer cycles.The text was updated successfully, but these errors were encountered: