When defining a lazy field, there is no exception, until you print it.
> data T = T Int deriving (Show)
> let t = T undefined
> t
T *** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
undefined, called at <interactive>:3:7 in interactive:Ghci3
With strict field (!Int
), I thought undefined
would be evaluated right away, which will cause exception, but actually, it's still no evaluated until you print it. Why is that?
> data X = X !Int deriving (Show)
> let x = X undefined
> x
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
undefined, called at <interactive>:6:11 in interactive:Ghci5
Because let
itself defines a lazy binding -- let
never evaluates anything by itself (unless BangPatterns
are used).
ghci> let x = undefined
ghci> x
*** Exception: Prelude.undefined
You can tell the difference between the strict and lazy constructors like so:
ghci> T undefined `seq` ()
()
ghci> X undefined `seq` ()
*** Exception: Prelude.undefined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With