[Haskell-cafe] Names in a record changes read behavior?

Jason Dagit dagitj at gmail.com
Mon Jul 27 22:27:28 UTC 2015


On Mon, Jul 27, 2015 at 1:25 PM, Mike Meyer <mwm at mired.org> wrote:

> Ok, this all works as expected:
>
> λ> data Point = Point Float Float deriving (Show, Read)
> λ> Point 1.0 1.0
> Point 1.0 1.0
> λ> read "Point 1.0 1.0"
> *** Exception: Prelude.read: no parse
> λ> read "Point 1.0 1.0" :: Point
> Point 1.0 1.0
>
> But now we add names to the values in the Point, and things change:
>
> λ> data Point = Point {x :: Float, y :: Float } deriving (Show, Read)
> λ> Point 1.0 1.0
> Point {x = 1.0, y = 1.0}
>

This works because the type of Point is Point :: Float -> Float -> Point.

You could have equally written:
> let foo = Point
> foo 1.0 1.0

The parser at the REPL is looking for arbitrary Haskell expressions, and
"Point 1.0 1.0" just happens to be a function application expression.

I hope that helps,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150727/61d307e2/attachment.html>


More information about the Haskell-Cafe mailing list