caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Object typing
@ 2005-07-09 23:55 Matthew O'Connor
  2005-07-10  0:20 ` [Caml-list] " Pietro Abate
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Matthew O'Connor @ 2005-07-09 23:55 UTC (permalink / raw)
  To: caml-list

Hello all, I'm in the process of writing a cellular agent-based immune
system simulator.  It uses objects as the agents/entities.  I have a
hierarchy of different entity types (ie, entity, cell, bacteria, virus,
antigen, etc.).  I want to be able to determine, at run time, if a given
entity is of a given type.

My current solution is to use functors to instantiate a weak set for all
the types I care about and then have that type's initializer add itself
to the set.  Then given an object it is simple to determine if it is a
member of that set.  This solution works, but it seems to be rather
slow.  I want to speed this process up.

Here is a version of what I would like to be able to do, but can't or
don't know how.  I would like to use polymorphic variants to enable
easier extensibility of the hierarchy.

(* test.ml *)
class type ientity =
object
  method is_a : [> ] -> bool
end

class entity : ientity =
object
  method is_a v =
    if v = `Entity then true
    else false
end

class predator =
object
  inherit entity as super
  method is_a v =
    if v = `Predator then true
    else super#is_a v
end

let ent = new predator;;
ent#is_a `Predator;; (* returns true *)
ent#is_a `Entity;; (* returns true *)
ent#is_a `Chicken;; (* returns false *)

(* ocaml test.ml *)
File "test.ml", line 7, characters 0-72:
The class type object method is_a : [> `Entity ] -> bool end
is not matched by the class type ientity
The class type object method is_a : [> `Entity ] -> bool end
is not matched by the class type object method is_a : [>  ] -> bool end
The method is_a has type [> `Entity ] -> bool but is expected to have type
  'a. ([>  ] as 'a) -> bool

I'm at a loss as how to proceed.  Any other suggestions or ideas or
better solutions would be greatly appreciated.  Thank you.


Matt


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-07-11  9:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-09 23:55 Object typing Matthew O'Connor
2005-07-10  0:20 ` [Caml-list] " Pietro Abate
2005-07-10  6:19   ` Matthew O'Connor
2005-07-10 18:25 ` Stephane Glondu
2005-07-10 22:06 ` Christophe TROESTLER
2005-07-11  6:23   ` Matthew O'Connor
2005-07-11  7:18   ` Remi Vanicat
2005-07-11  8:22     ` Jacques Garrigue
2005-07-11  9:16       ` Marcin 'Qrczak' Kowalczyk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).