caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Didier Le Botlan <didier.le-botlan@polytechnique.org>
To: Nobuyuki Tomizawa <tomizawa@ccm.cl.nec.co.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Question: 'instanceof'-like like primitive in OCaml
Date: Thu, 05 Apr 2001 21:09:54 +0200	[thread overview]
Message-ID: <3ACCC302.3FE661E5@polytechnique.org> (raw)
In-Reply-To: <FNEDLLDONJLKENCGLOOIKENGCJAA.tomizawa@ccm.cl.nec.co.jp>

Hello,

Because of strong typing, heterogeneous lists are not allowed (which is
actually good).

As far as I understand, what you need is 
 - some kind of "instanceof"
 - some kind of dynamic cast (depending on the result of "instanceof").

You have many ways to get instanceof :

For example you can declare a virtual method "instanceOfSpecific : bool"
in base class, and each subclass must define it. 

Dynamic cast is not possible here because a base object cannot be seen
as a "derived2" object in general.
You can declare a method "specific" which raises an exception (default
behaviour) and which is overriden in class "derived2" to do what
expected.

Thus, if you incorrectly call "specific" method on a "derived1" object,
an exception is raised. This corresponds somehow to an incorrect dynamic
cast in Java.

Finally, here is a short example :

class virtual base =
object
  method common x = x+1
  method virtual isSpecific : bool
    
  (* 'specific' returns a string. failwith is polymorph, we must help
type checker. *)
  method specific = ((failwith "specific is not defined !!!") : string)
end

class derived1 =
object
  inherit base
  method isSpecific = false
end
  
class derived2 message =
object
  inherit base
  method isSpecific = true
  method specific = "Hello " ^ message
end

let example =
  
  print_endline "Starting...";
  let obj1 = new derived1
  and obj2 = new derived2 "World"
  and obj3 = new derived2 "Folks" in
    
  let list = [ obj1 ; obj2 ; obj3; obj2 ; obj1 ] in
    
  let apply_specific obj =
    if obj#isSpecific then print_endline obj#specific
    else print_endline "No Specific"
  in
    
    List.iter apply_specific list

compile :
ocamlc -o exam exam.ml

and try...

Nobuyuki Tomizawa wrote:

> I'm a novice OCaml programmer and have a question about heterogeneous
> list and "downward cast".
> 
> Here is a pseudo Java code (I have):
> 
>      class base {
>         void common() {...};
>      }
> 
>      class derived1 extends base {
>      }
> 
>      class derived2 extends base {
>         void specific() {..};
>      }
> 
> and what I want to do are:
> 
>      * make the list which typed as "base list".
>      * call `derived2#specific' method if the element in the list is
>      an instance of 'derived2'.
> 
> But, OCaml seems not to have Java's `instanceof'-like primitive and/or
> downward-cast primitive.
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  parent reply	other threads:[~2001-04-08 20:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-04 13:03 Nobuyuki Tomizawa
2001-04-05 18:15 ` Brian Rogoff
2001-04-05 19:09 ` Didier Le Botlan [this message]
2001-04-06  9:14 ` Gerd Stolpmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ACCC302.3FE661E5@polytechnique.org \
    --to=didier.le-botlan@polytechnique.org \
    --cc=caml-list@inria.fr \
    --cc=tomizawa@ccm.cl.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).