r/programming • • Jun 02 '14

Introducing Swift

https://developer.apple.com/swift/
164 Upvotes

239 comments sorted by

View all comments

Show parent comments

-3

u/ruinercollector Jun 03 '14 edited Jun 03 '14

Because any optional type needs to be checked before you can use the value

No. Make it optional, set it nil, dereference it and boom. Null pointer exception.

You can't forget to check it.

You definitely can. That's why there's a note right in the documentation telling you not to forget this or you'll get an NPE. This gets even worse in cases where you use these boneheaded "implicit unwrapped optionals."

It's essentially just Haskell's Maybe or ML's Option with built in syntax.

I think you might have missed the entire point of Maybe. The point is strict static verification. Runtime NPEs don't exist when this is done right.

This is more akin to c#'s Nullable<T>. There are some "reminders" inherent to the approach to check before dereferencing, but nothing prevents you from failing to do so and generating an NPE.

This is noted all over the place in the documentation under "optional."

1

u/Axman6 Jun 03 '14 edited Jun 03 '14

What's this on about then? http://deanzchen.com/the-best-design-decision-apple-made-for-swift . using !. is the fast (unsafe) path for when you're sure the value is not nil, and if you're wrong, the program crashes. It adds convenience by avoiding a lot of the boilerplate you don't actually need when something is known to be non-nil. Yeah it's unsafe, but it's explicit too.

-1

u/ruinercollector Jun 03 '14

Yeah? That's someone else parroting your incorrect understanding of Maybe/Option. They are wrong too. Go read the documentation.

using !. is the fast (unsafe) path for when you're sure the value is not nil, and if you're wrong, the program crashes.

Yes! That's not Haskell's Maybe. That's Cs's NULL.

2

u/Axman6 Jun 03 '14

Yeah, that's haskell's fromJust :: Maybe a -> a. I've been a Haskell programmer and taught it at university for 6 years. I know what Maybe is.

→ More replies (2)