3 ms·
> One new feature is Optional Typing. You can decide whether a variable is ever allowed to be null. Ugh, Swift is really confusing terminology here. Swift has
by munificent 12y ago
> One new feature is Optional Typing. You can decide whether a variable is ever allowed to be null.
Ugh, Swift is really confusing terminology here. Swift has an "option" type, like "Option" in Haskell and F#. It's a wrapper around an object that may or may not be present. You can use that to avoid null-everywhere.
This is totally unrelated to "optional typing", which is a style of static type systems that support mixing typed and untyped code.
Alas, Swift called their option type "Optional" and then talks about "an optional type".
- ch 12y agoThis is why I appreciate that Haskell calls this type 'Maybe'. I think this better captures the types semantics. I mean, an 'Option' or and 'Optional' makes it seem like you are given a choice... but a 'Maybe' implies more that what you are declaring exists only with some probability! Q. Will this function give a result? A. Maybe. Q. Will this function give a result? A. Option. The choice is obvious.
- krick 12y agoTotally agree. I wondered the same way about why they chose Option over Maybe in Rust. There're so many things in the world that actually can be called Option (I could imagine <select> children or name of some business object) but pretty much none of them are about being null or not. When Maybe is so intuitive that I'm now using this word for the similar cases even in dynamic languages.
- eridius 12y agoThe choice is only obvious because you tailored the question to the Haskell type name. But that's not the question you're asking. The question is "What result will this function give me?". The Haskell answer is "Maybe a". It will give me something that might be an `a`? The Swift answer is "Optional<T>". That makes a bit more sense than "Maybe". The name makes even more sense when you talk about function arguments instead of function result types. Q. What argument does this take? A. Maybe a. It takes an argument that might be an `a`? Q. What argument does this take? A. Optional<T> Still not natural english, but the interpretation here is a bit cleaner: It takes an optional value of type `T`.
- ch 12y agoI'm not sure I totally agree. Well I do agree that I tailored the question to suit my point! But to your reply, I think you've changed the semantics of my meaning. My questions were more from the perspective of the caller, and even then it was a bit abstracted from the actual type and implementation ... just more of a conversational "Does it? Maybe!" You're rephrasing is still looking at it from the caller, but now takes into account the language semantics (which mine conveniently didn't :) ). I would rephrase your questions, and look at them from the callee's perspective: "Will I get an argument? Maybe!" And really, shouldn't we be saying that the function takes Just a to ... and takes Nothing to ... ? Perhaps this has stretched as far as it might go. Maybe?
- scanr 12y agoMight have got it from Java (either the Guava library or Java 8 http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html http://docs.oracle.com/javase/8/docs/api/java/util/Optional....)