Version française
Home     About     Download     Resources     Contact us    
Browse thread
Bug? Constraints get ignored in methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] Bug? Constraints get ignored in methods
Martin Jambon wrote:
> Use a classic list:
> 
> 
> class base = ...
> class derived = ... (* inherits base *)
> 
> type obj = Base of base | Derived of derived
> 
> let obj_list = [ Base (new base); Derived (new derived); ... ]
> 
> let iter_base f l =
>   List.iter (function Base x -> f x | Derived x -> f (x :> base)) l
> 
> let iter_derived f l =
>   List.iter (function Derived x -> f x | Base _ -> ()) l
> 
> ...

Or simply one list per type, each list containing all the elements compatible
with the type.

I would stay away from any solution that requires special methods in order to
solve the polymorphism/heterogeneity problem. Keep the modifications non-invasive.


Cheers,


Martin

-- 
http://mjambon.com/