[Haskell-cafe] Trouble understanding records and existential types
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Wed Jan 24 19:56:49 EST 2007
On Jan 24, 2007, at 19:34 , John Ky wrote:
> class Node -- yadda yadda
>
> data Branch = Branch { name :: String, description :: String,
> children :: [AnyNode] }
> data Leaf = Leaf { name :: String, value :: String }
>
> The problem here is I can't use the same 'name' field for both
> Branch and Leaf. Ideally I'd like the name field in the Node
> class, but it doesn't seem that Haskell classes are for that sort
> of thing.
I'm probably missing something, but:
(a) Why not:
data ANode = Branch { name :: String, description :: String,
children :: [AnyNode] }
| Leaf { name :: String, value :: String } -- this reuse
is legal
-- leaving Node available if you still need it
(b) I think you *can* do this with a class:
class Node a where
name :: a -> String
data Branch = Branch { brName :: String, ... }
data Leaf = Leaf { lName :: String, ... }
instance Node Branch where
name = brName
instance Node Leaf where
name = lName
--
brandon s. allbery [linux,solaris,freebsd,perl] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
More information about the Haskell-Cafe
mailing list