[Haskell-beginners] accessor function scope
Michael Mossey
mpm at alumni.caltech.edu
Tue Apr 7 03:01:12 EDT 2009
I'm coming from the OO world and trying to adapt.
I've noticed that if I declare two types like this
data Thing1 = Thing1 { itemPos :: Int }
data Thing2 = Thing2 { itemPos :: Int }
then I get the error "Multiple declarations of Main.itemPos"
However, I can successfully do this:
type Pos = ( Int, Int )
type Dimension = ( Int, Int, Int, Int )
data LayoutItem = StaffItem { itemPos :: Pos,
itemDim :: Dimension }
| TextItem { itemPos :: Pos,
itemDim :: Dimension }
d1 = TextItem ...
d2 = StaffItem ...
i1 = itemPos d
i2 = itemPos d2
This seems to define itemPos in a way that can sense whether it's dealing with a
StaffItem-type LayoutItem or a TextItem-type LayoutItem. I don't know if this would
be considered an overloaded operator. However, it does resemble derived class in OO.
For that matter, I don't know what the term is for a "StaffItem-type LayoutItem". The
type is clearly LayoutItem. "StaffItem" is the constructor. How do you refer to the
concept of a LayoutItem constructed via a StaffItem?
Again, StaffItem and TextItem resemble derived classes in OO.
Any clarification welcome.
-Mike
More information about the Beginners
mailing list