Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Is a Cow an Animal?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] Is a Cow an Animal?
Pixel wrote:

>- since class equivalence is based on methods and not on class-name, I have to
>put dummy methods for ensuring the subtyping relation. 
>Is there a nicer/official way?
>- I know one can't downcast, but is there no way to have some RTTI? must it be
>done by hand? (adding a C++-typeinfo-like method)

You ask in the title "Is a cow an animal?". The answer is no.
Use a variant, give up on classes:

type Animal = Horse of horse | Cow of cow
type horse = Shetland of shetland | Arabian of arabian

That is a heterogenous tree structure modelling
the taxonomy. There is no polymorphism here.
There is no subtyping. It is pretty much the exact
opposite: this is unification, the aggregation of
utterly distinct types into a common type.

You can do a bit better sometimes, by recognising
some commonality:

type Animal = {

type animal_split = Horse of horse | Cow of cow

so that the horse type only contains features unique
to horses. But you should really forget abstraction,
and just build concrete data structures: its really
just a large in memory database, after all:
you really won't gain much hiding the representation here.

>John Max Skaller,
>snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: