
It seems to be hard to have something both abstract and concrete. http://merd.net/pixel/language-study/various/non-mutable-objects-and-subtypi... The first version (file haskell.listing) is quite simple, but the last one (file haskell3.listing) gets pretty hairy: ---------------------------------------- class Show a => Point a where translate :: a -> Double -> a my_length :: a -> Double add :: a -> a -> a pack :: a -> Packed_point unpack :: a -> Points pack = Packed_point data Packed_point = forall a. Point a => Packed_point a data Points = TPoint2d Point2d | TPoint3d Point3d data Point2d = Point2d(Double, Double) deriving Show data Point3d = Point3d(Double, Double, Double) deriving Show ---------------------------------------- is there a simpler solution? thanks! PS: please, don't give me the simple non-OO version, it's already there (haskell3bis.listing), and I give an explaination why i think it's not enough: non-OO Haskell and OCaml versions are the simplest, but do not allow precise enough typing: no distinction between Point2d and Point3d is done at compile time. So the only way to have a function working only on Point3d's is to raise an error when that function is called with a Point2d (see get_z) (of course, compared to dynamically typed languages versions, it's already much better)
participants (1)
-
Pixel