17 Nov 2012 15:59
Parsing different types, same typeclass
José Lopes <jose.lopes <at> ist.utl.pt>
2012-11-17 14:59:59 GMT
2012-11-17 14:59:59 GMT
Hello everyone,
I was wondering if you could help me!
So, I have a typeclass "Action" which defines method "run":
class Action a where
run :: a -> Int
and two data types that are instances of this typeclass:
data A = A Int
deriving (Read, Show)
instance Action A where
run (A n) = n
data B = B Int
deriving (Read, Show)
instance Action B where
run (B n) = n
Now, I want to parse either "A" or "B" from a String.
I was thinking about something like this...
parseAction :: (Action a, Read a) => String -> a
parseAction str
| "(A " `isPrefixOf` str = (read :: String -> A) str
| "(B " `isPrefixOf` str = (read :: String -> B) str
(Continue reading)
RSS Feed