[Haskell-cafe] matching constructors

Vadim Zaliva lord at crocodile.org
Mon Mar 8 11:44:16 EST 2004

On Mar 8, 2004, at 11:17, Sven Panne wrote:

> Hmmm, using generics seems like massive overkill for option handling. 
> Could you
> describe what you are exactly trying to achieve?

I am doing command line options parsing. I've defined Flag type with 
for each possible option:

data Flag = Verbose |
             Input String  |
             Output String |
             Filter String
             deriving (Show, Typeable, Data)

getOpt returns me a list of such objects. Now I need to
look things up there by constructor. For example:

		doSomething fltflag
         (Filter fltflag) = findFlag (Filter "none") opts

To achieve this I've defined:

instance Eq Flag where
     x == y = toConstr x == toConstr y

findFlag :: Flag -> [Flag] -> Flag
findFlag f fs = fromMaybe f (find (==f) fs)


"La perfection est atteinte non quand il ne reste rien a ajouter, mais
quand il ne reste rien a enlever."  (Antoine de Saint-Exupery)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2388 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20040308/321e2841/smime-0001.bin

More information about the Haskell-Cafe mailing list