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: Pixel <pixel@m...>
Subject: [Caml-list] Is a Cow an Animal?
Based on

I made
and especially

Please help with some pbs!

- 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)

FYI the C++ version is:

void is_human_food(const Food &food) {
  if (dynamic_cast<const Carrot*>(&food) == NULL &&
      dynamic_cast<const Meat*>(&food) == NULL)
    mythrow((string) "human doesn't accept food " + typeid(food).name());

- I can't manage to create a "human" which would accept any type of "food", it
only accepts exact "food", needing an upcast from "carrot"... to "food".

using a parametric class for human:

class ['a] human energy = object 
  inherit [dead_human, 'a] animal energy (new dead_human) drop
  constraint 'a = #food 
  method foo_human  = () 

it seems that 'a is free and gets infered with every use, ie after:

  a_human#eat carrot ;

'a is instanciated to carrot = < eaten : int; foo_carrot : unit >

and refuses

  a_human#eat a_beef ;

is it possible to *set* 'a to #food as wanted?


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