Optimight
Optimight

Reputation: 3051

Why do I get an error when trying to `read` this value?

The .hs code :

data Person = Person { firstName :: String  
                     , lastName :: String  
                     , age :: Int  
                     } deriving (Eq, Show, Read)

Compilation :

*Main> :load "/home/optimight/baby.hs"  
[1 of 1] Compiling Main             ( /home/optimight/baby.hs, interpreted )  
Ok, modules loaded: Main.  

While testing immediately after compilation:

*Main> read "Person {firstName = \"Michael\", lastName \"Diamond\", age = 43}" :: Person  
*** Exception: Prelude.read: no parse

Please guide. Why this error is occurring and how to avoid such errors?

Upvotes: 1

Views: 99

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183873

lastName \"Diamond\"

is missing an equals sign.

read "Person {firstName = \"Michael\", lastName = \"Diamond\", age = 43}" :: Person

Upvotes: 7

Related Questions