Proposal: disallow nil in favor of .None


(Paul Young) #1

Please forgive me if this has already been discussed, but I couldn't find
reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the
use of `.None` instead.

I would like to understand if there are any technical (or otherwise)
reasons for `nil` to remain, or if it simply a remnant of interop with
Objective-C.

Thanks,

Paul


(Chris Lattner) #2

Hi Paul,

There never was a “technical” reason for including nil - it is simply sugar for constructs that you could form some other way. It wasn’t related to Objective-C interop, and I’m not aware of other changes that would affect the balance of “nil" in Swift. If anything, “nil” is destined to become *more* simple, since only Optional will be NilLiteralConverible once the planned Swift 3 changes land ("abolish IOU" and “make nullability explicit for unsafe pointers”).

-Chris

···

On Apr 10, 2016, at 1:53 PM, Paul Young via swift-evolution <swift-evolution@swift.org> wrote:

Please forgive me if this has already been discussed, but I couldn't find reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the use of `.None` instead.

I would like to understand if there are any technical (or otherwise) reasons for `nil` to remain, or if it simply a remnant of interop with Objective-C.


(David Waite) #3

Paul,

Basically, nil is a literal and not a shortcut or alias for Optional.none.

Types which implement NilLiteralConvertible can have a new instance initialized by the ‘nil’ literal. There is an initializer on Optional that is used to generate an Optional.none in response to a nil literal when expecting an Optional type.

There are also similar Convertible protocols Boolean, String, Array, Dictionary, Floating point and Integer literals.

The use cases for the nil literal within the standard library are decreasing. However, I have used it for a few other rare cases (most notably a native JSON library).

-DW

···

On Apr 10, 2016, at 2:53 PM, Paul Young via swift-evolution <swift-evolution@swift.org> wrote:

Please forgive me if this has already been discussed, but I couldn't find reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the use of `.None` instead.

I would like to understand if there are any technical (or otherwise) reasons for `nil` to remain, or if it simply a remnant of interop with Objective-C.

Thanks,

Paul
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Brandon Knope) #4

Just to be a little pedantic (with a point though):

This will actually become .none in swift 3 which actually makes me support this idea even more.

nil -> .None looks goofy to me
nil -> .none looks just right

But this is a bold change and is something people are use to from other languages, so I'm sure this will be an uphill battle for you, but "none" does make more sense. Or even renaming nil to none would fit optionals even better

Brandon

···

Sent from my iPad

On Apr 10, 2016, at 4:53 PM, Paul Young via swift-evolution <swift-evolution@swift.org> wrote:

Please forgive me if this has already been discussed, but I couldn't find reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the use of `.None` instead.

I would like to understand if there are any technical (or otherwise) reasons for `nil` to remain, or if it simply a remnant of interop with Objective-C.

Thanks,

Paul
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Radek Pietruszewski) #5

Pretty sure it has been discussed already.

MHO: much of the power of Optionals in Swift is that they’re more than just a .None/.Some(T) enum. They’re tightly integrated into the language. You never say .Some(value) — values are just lifted to optionals automatically. And if we never directly say .Some(T), why should we say .None? Let’s treat optionals as part of the language, and the enum an implementation detail. Let’s use automatic lifting to optionals, all the `if let`s and other helpful syntax, and say `nil` instead of .None — after all, it’s an important enough part of the language to deserve its own keyword. (A keyword, that, as an additional benefit, will be more immediately understood by someone new to Swift)

— Radek

···

On 10 Apr 2016, at 22:53, Paul Young via swift-evolution <swift-evolution@swift.org> wrote:

Please forgive me if this has already been discussed, but I couldn't find reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the use of `.None` instead.

I would like to understand if there are any technical (or otherwise) reasons for `nil` to remain, or if it simply a remnant of interop with Objective-C.

Thanks,

Paul
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Brandon Knope) #6

What about adding a none keyword as a shortcut for .none?

Might be a little more consistent with optionals than nil which brings to mind pointers for most people

···

Sent from my iPad

On Apr 10, 2016, at 5:13 PM, Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org> wrote:

Pretty sure it has been discussed already.

MHO: much of the power of Optionals in Swift is that they’re more than just a .None/.Some(T) enum. They’re tightly integrated into the language. You never say .Some(value) — values are just lifted to optionals automatically. And if we never directly say .Some(T), why should we say .None? Let’s treat optionals as part of the language, and the enum an implementation detail. Let’s use automatic lifting to optionals, all the `if let`s and other helpful syntax, and say `nil` instead of .None — after all, it’s an important enough part of the language to deserve its own keyword. (A keyword, that, as an additional benefit, will be more immediately understood by someone new to Swift)

— Radek

On 10 Apr 2016, at 22:53, Paul Young via swift-evolution <swift-evolution@swift.org> wrote:

Please forgive me if this has already been discussed, but I couldn't find reference to it anywhere.

My proposal is pretty simple – disallow the use of `nil` and require the use of `.None` instead.

I would like to understand if there are any technical (or otherwise) reasons for `nil` to remain, or if it simply a remnant of interop with Objective-C.

Thanks,

Paul
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution