[swift-evolution] Discussion: Why is "nil" not "none"

Антон Жилин antonyzhilin at gmail.com
Wed Jun 8 10:54:41 CDT 2016


The difference between  nil  and  .none  is that the former is more
"generic" than the latter.

NilLiteralConvertible protocol expresses types that can contain "null" as
legal values. `nil` does not have a type, it's just a token that is casted
to whatever NilLiteralConvertible type is expected. It is used in JSON
libraries, for example:

let tree: JSON = ["name": "Alex", "age": 20, "email": nil]

Here, nil would be inferred to have our custom JSON type.
The same example with  none  would look a bit more weird:

let tree: JSON = ["name": "Alex", "age": 20, "email": none]

None of what type? Of String? Of Int? Of JSON? There are no optionals in
this code. And a "null" present in JSON is different than no JSON in Swift.

Optional is the most significant example of NilLiteralConvertible, but
there are other users that would suffer from renaming. We could remove
NilLiteralConvertible at all, but look at the example in this case:

let tree: JSON = ["name": "Alex", "age": 20, "email": JSON.nullValue]

That would hurt immersion of the DSL.

I think some Core team member told that they intentionally keep two ways of
handling Optional: one using nil, and the other treating it like other
enums.

- Anton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160608/3bdf9ad8/attachment.html>


More information about the swift-evolution mailing list