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]     [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon university    KF8NH



_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to