[Haskell-beginners] "downcasting"

Emmanuel Touzery etouzery at gmail.com
Thu Oct 25 21:28:22 CEST 2012


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 <andres at well-typed.com> 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.htm>


More information about the Beginners mailing list