|
|
|
|
|
by katfang
4159 days ago
|
|
I agree! Eithers are cool, too. What I'm really excited about is that these sorts of concepts are starting to find their way into languages like Swift. Getting built-in support for Eithers would be super awesome. In the meantime, you can vaguely simulate it with named tuples. let response = (error: "Could not connect to server", success: false) It's not quite the same, but at least there's an easy way to bundle your data together. edit: I like Someone's suggestion to use and discussion about using enums as union types below. |
|
“Define an enumeration type called Barcode, which can take either a value of UPCA with an associated value of type (Int, Int, Int, Int), or a value of QRCode with an associated value of type String.”
So, you can do
Unfortunately, enums and generics do not mix, AFAIK, so it looks clumsy.I think the reason for the special syntax of '?.' is that the designers think it is a very common case, at least in Swift programs.
And yes, I would like to see a better syntax for that, too. A generalization of C's short-hand 'if'
For switch statements would be cool.