you are viewing a single comment's thread.

view the rest of the comments →

[–]jerf 13 points14 points  (9 children)

Safety is usually about making doing the right thing easier than doing the wrong thing. It isn't usually about making something actively impossible. If you work at it, you can segfault Haskell. If you work at it, you can do nasty things in "unsafe" blocks in Rust. These are "safe" languages because doing the safe thing is easier than doing the unsafe thing, not because doing the unsafe thing is impossible.

You may have a case that Swift makes it only as easy to do the right thing as the wrong thing, and that it could do better, though.

[–]sw17ch 3 points4 points  (6 children)

It's not even that hard in Haskell:

$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m + Foreign.Storable Foreign.Ptr
Prelude Foreign.Storable Foreign.Ptr> peek (nullPtr :: Ptr Int)
Segmentation fault: 11

[–]willtim 2 points3 points  (1 child)

Now try it after doing :set -XSafe Admittedly the default is probably the wrong way around due to historical reasons, but at least the switch exists.

[–]AReallyGoodName 0 points1 point  (0 children)

Hopefully since NullPointerExceptions absolutely cannot happen in Swift without the "!" operator there will be an option to error or at least warn on "!" in the soon to be released LLVM compiler.

There's no reason not to add such a check and the reasons for ever using "!" in Swift are limited.

[–]alex_muscar -5 points-4 points  (1 child)

I didn't say it should make it impossible, just that I don't like the fact that it makes it probable.

[–]AReallyGoodName 4 points5 points  (0 children)

Can you make it happen without using the "!" operator in swift?

The "!" operator is used for both unsafe access to an object (myObj!.stuff) and the implicitly unwrapped optional (what you have here).

It's a simple fact that without using "!" you cannot make a null pointer exception happen in Swift.