I am trying to understand the exact limits on enums with generic associated values in Swift.
You might think that they are supported, since Optional is such a type. Here is the code defining Optional in the Swift standard library:
enum Optional<T> : Reflectable, NilLiteralConvertible {
case None
case Some(T)
// ...
}
It seems like the case member Some has an associated value of variable type T, right?
However, it is mentioned in the book Functional Programming in Swift (p 87), that such types are not supported:
We would like to define a new enumeration that is generic in the result associated with Success:
enum Result<T> { case Success(T) case Failure(NSError) }Unfortunately, generic associated values are not supported by the current Swift compiler.
And indeed, if you type that snippet into the compiler, you get an error (error: unimplemented IR generation feature non-fixed multi-payload enum layout).
So what is going on here? Is it just that it is not supported in general, but is supported for Optional as a special case? Is there any way to see how Optional receives this special support? Or if other standard library types also get special support?
Tis undetermined, since if it is a non-class type then it cannot be certainly known to have the size of an object pointer. – algal Dec 2 '14 at 20:20