[Haskell-beginners] Bool newtype
Michael Alan Dorman
mdorman at jaunder.io
Sun Aug 7 12:49:33 UTC 2016
Imants Cekusins <imantc at gmail.com> writes:
> for a Bool-like newtype:
>
> newtype B = B Bool
>
> , is there an easy way to use this newtype B in place of Bool?
>
> e.g.
>
> let b1 = B True
> in if b1 then 1 else 0
>
> ?
Imants,
You can create a function that converts a B to a Bool:
toBool :: B -> Bool
toBool B b = b
And then use the result of applying that function to your variable:
let b1 = B True
in if toBool b1
then 1
else 0
To do anything like this automatically would seem to me to defeat the
purpose of having distinct types, which is to clearly articulate what
values are appropriate in particular contexts.
Mike.
More information about the Beginners
mailing list