[Haskell-beginners] Multiple declarations of value constructor
Brandon Allbery
allbery.b at gmail.com
Wed Dec 19 17:09:48 CET 2012
On Wed, Dec 19, 2012 at 10:52 AM, Emanuel Koczwara <
poczta at emanuelkoczwara.pl> wrote:
> If so, how are numbers defined? I can use 1 :: Int or 1 :: Integer. Why
> not Value1 :: MyType1 and Value1 :: MyType2?
>
Numbers are special-cased in the compiler, because they're polymorphic; in
effect, the literal is output as a call to "fromIntegral" or "fromRational"
as appropriate for the type, on some low level internal representation of
the literal. (There is an extension to enable this for strings as well,
but not for other types.) You can't do this with user defined types
because you can't make a constructor a typeclass function.
Note that various things, in particular pattern matching, rely on
constructor names unambiguously identifying types. (Again, numeric
literals are a special case; patterns involving them actually get rewritten
into guards. If you want to know the gory details, they're documented in
the Haskell Language Report.)
--
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121219/15474058/attachment-0001.htm>
More information about the Beginners
mailing list