LWN.net Logo

RE: nullable pointers

RE: nullable pointers

Posted Apr 1, 2011 8:48 UTC (Fri) by jezuch (subscriber, #52988)
In reply to: RE: nullable pointers by HelloWorld
Parent article: GCC 4.6.0 released

> You forgot the one that matters: they may fail to compile.

Yup. You learn Haskell and/or ML (and/or ...) and the case against null pointers becomes so obvious you don't even know how to explain it.


(Log in to post comments)

RE: nullable pointers

Posted Apr 1, 2011 9:12 UTC (Fri) by paulj (subscriber, #341) [Link]

So in Haskell you never have to do things like have a separate case for an empty list?

If you specifically mean SEGVs, well that's very easy to explain: Haskell and functional languages generally do not have pointers.

RE: nullable pointers

Posted Apr 1, 2011 21:01 UTC (Fri) by mathstuf (subscriber, #69389) [Link]

You still have to case on the empty list (e.g., you can't get the first value from it, so if you need it, you need to do /something/ different for the empty list unless you want exceptions about incomplete pattern matchings). An example:

safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (a:_) = Just a

When calling safeHead, the caller must* check to see if a value was actually returned before using it. This can be done with fromMaybe:

fromMaybe sensibleDefault $ safeHead someList

* The caller can just use fromJust which raises an exception on a Nothing value, but that's the caller's burden at that point.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds