5 ms·
Well, in Crystal Nil is a separate type that can be combined with others. But, say, a String is always a String, it doesn't implicitly have the Nil type. Same g
by asterite 11y ago
Well, in Crystal Nil is a separate type that can be combined with others. But, say, a String is always a String, it doesn't implicitly have the Nil type. Same goes with every other type.
Maybe you are thinking of Java/C#, where reference types can also be null, but this is not true in Crystal. It's also in a way similar (but not quite) to Swift, where optional types are different than types that can't be null.
- vanderZwan 11y agoDo you have a technical write-up of how Crystal does that? Or otherwise some links/papers that explain the principle in a language accessible for someone who is basically self-taught and lacks a formal CompSci education?
- wtetzner 11y agoIt's a nice approach, I like it. There is a difference between Option[T] and (T | Nil) that's worth mentioning, however. Option[T]'s are composable. For example, let's say we have a "get" method to get the value for a given key, whose type looks like: get :: String -> (T | Nil) If we were using Option[T]'s, it would look like: get :: String -> Option[T] So let's say we have a map, and want to lookup a key (syntax is made-up): let m: Map[String,(Int | Nil)] = make_some_map() let result: (Int | Nil) = m.get("some-key") If result is nil, was the value of the key nil, or was the key not in the map? With Option[T]: let m: Map[String,Option[Int]] = make_some_map() let result: Option[Option[Int]] = m.get("some-key") Here result will either be None, in which case the key wasn't in the map, or Some(None), which means the value of the key was None. So there is an observable and potentially useful difference between (T | Nil) and Option[T].
- lobster_johnson 11y agoSounds exactly like union types in Ceylon (http://ceylon-lang.org/documentation/1.1/spec/html/introduction.html#unionandintersectiontypes http://ceylon-lang.org/documentation/1.1/spec/html/introduct...), which is an interesting approach.