[swift-evolution] Making pointer nullability explicit (using Optional)

Jordan Rose jordan_rose at apple.com
Fri Mar 18 11:40:35 CDT 2016


> On Mar 17, 2016, at 20:21 , FĂ©lix Cloutier <felixcca at yahoo.ca> wrote:
> 
>> Fortunately, when a type has a single invalid value for which no operations are valid, Swift already has a solution: Optionals.
> 
> To me, this makes it sound like dereferencing an unsafe pointer is unsafe only if the pointer is nil. (Nil does have the merit that it's generally one of the only addresses known to be invalid, though.)

Okay, you're right, it was a bad description of the problem. UnsafePointer remains "unsafe" for a lot of other reasons. :-)


> 
> One thing that I would worry about, though, is the case where dereferencing address 0 is legal. My understanding is that Swift is aimed to be a systems language, and this case is relatively frequent on embedded devices.

Good point. Technically in the C standard, there must be some pointer value that cannot be the address of anything valid, whether or not it has a bit pattern of 0; the intent was that Swift's 'nil' would follow whatever C did for that platform rather than being 0. But I imagine a lot of embedded C code doesn't actually follow this rule (i.e. "NULL" will still give you the 0 address).

I'm not very familiar with this space at all, but I'll look into it some more. If you know what existing compilers do here that'd be great to read.

Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160318/0dbcc81e/attachment.html>


More information about the swift-evolution mailing list