Sum types covering Swift enum
use-cases.
#723
Replies: 4 comments 14 replies
-
One thing occurred to me. If I'm not mistaken, Swift's enum cannot have raw values if they also have associated values. Nonetheless, there must be an implicit discriminator used by the language implement to tag the different cases. It is probably some sort of integer, possibly hidden in the bits of the enum's storage. I think it isn't unreasonable to imagine a world where we could access it at the user level. Obviously we also need a discriminator in Val for the general case (e.g., |
Beta Was this translation helpful? Give feedback.
-
Yes. But, as I mentioned in slack, there's no good reason for that restriction… except possibly avoiding confusion.
Hidden in the bits yes, but not necessarily stored, which means it may only be a notional value. After going through your post, I believe you understand the following, but I think it may be useful for other readers: The size of However, there are limits to how smart we can expect to be about taking advantage of that.
Yes, “what is the ordinal of the subtype stored in this Sum type?” is a reasonable question to ask. But also note that the
That's cute. But even if the discriminator is exposed to users as a metatype, I think it's probably also useful to be able to get an ordinal value and I'm not sure that should ever be completely hidden. |
Beta Was this translation helpful? Give feedback.
-
If I may, I have an idea about type generalization in general and a suggestion for enums in particular.
All these definitions are slightly different. Tuples are non-nominal, structs are (may be) ABI compatible for future changes, enum cases are sealed. But their prime purpose is to carry data, and they do it equally good.
And the coresponding switch statement is almost like dynamic type casting, but has the property of completeness.
So maybe the type system should support
A unit type always represent one value. Another thing I find useful is tagged types. First of all they prevent mistakes when there are several business type of the same real type, like
With tagged types one can't pass a password string to a function accepting OAuthToken. The differentiation between nominal structs and non-nominal tuples in Swift seems to be inconvenient.
|
Beta Was this translation helpful? Give feedback.
-
Is there another property that |
Beta Was this translation helpful? Give feedback.
-
Sum types have the following properties:
Sum<A, B> == Sum<B, A>
,Sum<A, A> == Sum<A>
,Sum<A> == A
.The question is how to generalize them to cover the use-cases handled by Swift enums. There are two issues: named cases and raw values.
Named cases can be expressed as labeled tuple types, e.g.:
The
RawValue
type can be expressed as an optional leading named generic argument, defaulted toInt
:Which makes the idea generalizable to cases with payloads.
Lastly, various sugars can be explored to make typical
enum
use-cases more palatable to work with, eg. making(z:)
a way to write(z:())
Beta Was this translation helpful? Give feedback.
All reactions