Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: "downcasting" (Emmanuel Touzery)
----------------------------------------------------------------------
Message: 1
Date: Thu, 25 Oct 2012 21:28:22 +0200
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] "downcasting"
To: [email protected]
Message-ID:
<CAC42RenBJPCaiY=tkae6ypbmmq6orcdlbi_muxbzmoktuo6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thank you.
To be honest I'll have to return to this point later to properly "digest"
it because I don't complely get it yet.
It seems a bit crazy to me to give to the data JSObject the same name as
with the newtype JSObject. It is very confusing for me. But I'll get it, it
must just sink in.
Thanks a lot, because I would never understand that one on my own...
Emmanuel
On Thu, Oct 25, 2012 at 12:15 AM, Andres L?h <[email protected]> wrote:
> Hi Emmanuel.
>
> It's somewhat confusing that in Haskell, data constructors and
> datatypes can have the same name. They're nevertheless different
> beasts. A data constructor is a way to construct values of a datatype,
> or to destruct them via pattern matching. Such constructors themselves
> are *not* types.
>
> The definition of JSValue looks as follows:
>
> > data JSValue
> > = JSNull
> > | JSBool !Bool
> > | JSRational Bool{-as Float?-} !Rational
> > | JSString JSString
> > | JSArray [JSValue]
> > | JSObject (JSObject JSValue)
> > deriving (Show, Read, Eq, Ord, Typeable)
>
> So there are six different ways to construct a JSValue, the last one
> is the JSObject constructor. It contains one item which is of *type*
> JSObject JSValue. This time, JSObject refers to a datatype, also
> defined in the library:
>
> > newtype JSObject e = JSONObject { fromJSObject :: [(String, e)] }
> > deriving (Eq, Ord, Show, Read, Typeable )
>
> Now to your functions: if you write
>
> > getObject :: JSValue -> JSObject JSValue
> > getObject x@(JSObject _) = x
>
> or
>
> > getObject :: JSValue -> JSObject JSValue
> > getObject (JSObject x) = JSObject x
>
> you are writing essentially the identity function, but trying to
> assign a more specific type to the value. This doesn't quite work in
> Haskell. There's no subtyping between different datatypes.
>
> Instead, what you should do is perform the pattern match once and
> extract its contents:
>
> > getObject :: JSValue -> JSObject JSValue
> > getObject (JSObject x) = x
>
> Now you have the stuff that was "inside" the constructor, and isolated
> the point of failure.
>
> Cheers,
> Andres
>
> --
> Andres L?h, Haskell Consultant
> Well-Typed LLP, http://www.well-typed.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121025/bd87b138/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 52, Issue 30
*****************************************