Getting "indiscriminately" from Prelude.read

I just started Haskell, and I was trying to create a simple Number data type that has one Int, for example:

data Number = Num Int deriving (Read, Show) 

Then I tried to make read "1234" :: Number , hoping it would give something like Num 1234 , but it seems that instead I get "Exception: Prelude.read: no parse". Is there anything else I am missing?

+5
source share
1 answer

The resulting instance will give read "Num 1234" = Num 1234 .

This is the behavior that is expected for Read instances, but if you really want a different behavior, you will have to implement Read for Number yourself (you can reuse Int 's, though).

+9
source

Source: https://habr.com/ru/post/1202482/


All Articles