3 ms·
Type systems that incorporate "nullables" allow programmers to have the concept of "nothing was returned" but force them to check it whenever they test for data
by striking 12y ago
Type systems that incorporate "nullables" allow programmers to have the concept of "nothing was returned" but force them to check it whenever they test for data. Otherwise their programs don't compile (see the Maybe monad in Haskell). In a serious post about null, how could such a serious point be missed?
For example, in Haskell:
case objectMaybe of
Just object -> do
(your code that deals with `object` here)
Nothing -> do
(your code that deals with no object)
Without the Nothing clause in the matching statement, this code will not compile.
- VMG 12y agoNot quite. sh-4.3$ cat test.hs main = let x = Nothing in case x of Just x -> putStrLn "something" sh-4.3$ ghc test.hs sh-4.3$ ./test test: test.hs:(2,11)-(3,40): Non-exhaustive patterns in case
- striking 12y agoAh, so it will compile but break, thank you for mentioning that.
- dllthomas 12y agoAlternatively, sh-4.3~$ ghc -Wall -Werror test.hs [1 of 1] Compiling Main ( test.hs, test.o ) test.hs:1:1: Warning: Top-level binding with no type signature: main :: IO () test.hs:3:16: Warning: This binding for `x' shadows the existing binding bound at test.hs:1:12 test.hs:3:16: Warning: Defined but not used: `x' <no location info>: Failing due to -Werror.
- VMG 12y agoThis is where it gets weird on my system sh-4.3$ ghc --version The Glorious Glasgow Haskell Compilation System, version 7.8.3 sh-4.3$ ghc -Wall -Werror test.hs sh-4.3$ Arch Linux 64bit. It works as expected with runhaskell though Edit: see answer below, thanks dilthomas!
- dllthomas 12y agoTry touching test.hs (or add -fforce-recomp). By default, ghc won't rebuild if the object is newer than the source.
- luchs 12y agoBut it doesn't break because of the "Non-exhaustive patterns in case" that causes the runtime failure? These things look like they can be fixed without touching the case statement.
- wcummings 12y agoAmazing how close that is to valid Erlang
- lostcolony 12y agoSo as others noted; this code will compile, but with a warning. Turn on warnings for incomplete patterns (-fwarn-incomplete-patterns), allow warnings to break compilation (-Werror), and you've got compile time safety. https://www.haskell.org/ghc/docs/7.0.4/html/users_guide/options-sanity.html https://www.haskell.org/ghc/docs/7.0.4/html/users_guide/opti... lists out what the flags do.