It would be nice if the blog post said what language this is! I guess it’s probably Swift, but I have no way of knowing.
It’s not Swift, Rust, or Scala. I think it’s pseudocode.
I don’t think the idea of ‘nil’ being a subtype of every other type plays well with the idea of representing none-values using an optional type ADT (the author alludes to this a bit).
In a language like Swift, the declaration “let myVar : Foo” guarantees that myVar will never contain nil, which ensures at compile time that a variable containing nil will never be improperly dereferenced (unless you do type-system-subverting things like force-unwrapping).
If your declaration “Foo myVar” implies myVar can be Foo or nil, then optional types become little more than an opt-in annotation on the parameters or return values of a function/method. You have to figure out what semantics Some(nil) has, and hope your APIs are well-behaved enough to respect that meaning.
What do you mean? I’m just trying to understand what you said I alluded to in the post.
Mostly this, which I agree with:
One way to interpret this is to consider that nil is a subtype of Int and that’s why no error is thrown at compile time. One example of a bad type system that allow this is Java’s where you can return null from a method that its return type is Integer.
Some languages, like Scala, have that behavior and the ADT optional.
Yeah, it’s definitely not a good thing. At least the std libs (in Scala) promote the usage of optionals, which is an improvement anyway. Even Java has Optional<T> nowadays.
For me the best way is what Rust does (and I’m sure other languages also do) which is just having Option<T>.
I don’t mention because it’s not important for the point of the post and also because it’s not a real language. The syntax is just a mix of Ruby and Rust.
Considering you’re talking about the semantics of something, your post makes no sense without picking a language or at least describing the semantics of your made up lanuage. Your post is nonsense otherwise.
I suggest you to read the post again because you missed the point. There’s no need to know the semantics. The post is clear about nil being a completely different thing from None and why. But I guess I shouldn’t be explaining here, it’s all in the post.
There’s no need to know the semantics.
Of course there is, because you don’t say what None or nil are. Python has None with the semantics of your nil (well, not quite but close enough) and OCaml has None with semantics like your None. Which one is the one that you mean? How is the reader supposed to know? The only way to know what you mean is by knowing what you want to explain before reading your post to decipher which semantics you are assigning to None and nil.
Did you even read the post? I’m pretty sure it says what None and nil are.
There’s no need to know the semantics
Given that the point of the post is about the semantics of something, I dont' see how you can claim there is no reason to use them.
The post is clear about nil being a completely different thing from None and why.
What you do is show some random code and claim you can interpret it a certain way. Then you show another piece of random code and claim you cannot interpret it that way. I find that less than clear.
Anyways, as someone that has some experience in type systems, I’m telling you that your post is confusing and nonsensical because you have left out important context. Feel free to do what you want with information.
Exactly. Let’s just move on. I don’t see this discussion going anywhere. Some people will read stuff and understand and others won’t. It happens everyday with everybody (including me).
The problem is not that we can’t figure out your syntax; the problem is that we can’t figure out your semantics. Your post makes no sense if you’re talking about Python’s None or Scala’s Nil, or indeed if you’re talking about dynamically typed languages at all. It’s like when a five-year-old says “We’re going to Danna’s!” on the assumption that you and indeed everyone in the world share their context that gives meaning to “Danna”. That semantic context is provided by a programming language.
See my comment above to @apy.
def sum(a: Int, b: Int) -> Option<Int>
Some(a + b)
But None is a value as well as a type, and it is isomorphic to nil - they offer the same interface.
I see it as representing the same value in a more appropriate type. It’s like the difference between “firstname.lastname@example.org” and EmailAddress(email@example.com). Or the idea that it’s better to store phone numbers as strings, not numbers, because you don’t ever want to do arithmetic on them.