Subtyping
Ashley Yakeley
ashley@semantic.org
Sat, 18 Aug 2001 01:05:17 -0700
At 2001-08-14 03:46, Marcin 'Qrczak' Kowalczyk wrote (on the Libraries
list):
>> I've noticed that the more I use Haskell, the less I miss
>> subtyping... classes and "data" unions are almost always sufficient.
>
>They are sometimes a better match than subtyping (e.g. parametrizing
>a gcd algorithm by a type) and sometimes they are not capable at all
>(e.g. heterogeneous collections when the set of possible types is
>open). I'm a big fan of Haskell's typing and I'm not a fan of OOP,
>but Haskell is sometimes not as expressible as the OO way.
One possible solution would be to extend Haskell with "open" algebraic
data types. For instance:
data SomeObject = SomeInt Int | SomeString String | _
...later...
data SomeObject |= SomeReal Double
The "_" would mark a data type as 'open', and the "|=" syntax would be
used to extend it in another module.
This seems to me straightforward, but perhaps not very useful unless
there's a way of declaring "generic" functions on these types that can be
extended somehow later:
same :: SomeObject -> SomeObject -> Bool
same (SomeInt a) (SomeInt b) = a == b
same (SomeString a) (SomeString b) = a == b
same _ _ = False
...later...
same (SomeReal a) (SomeReal b) = a == b -- this case needs to be
"inserted" somehow
But would this sort of thing solve your problem?
--
Ashley Yakeley, Seattle WA