caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: tim@fungible.com (Tim Freeman)
To: caml-list@inria.fr
Subject: [Caml-list] How to downcast in OCAML
Date: Sat, 21 Sep 2002 08:09:44 -0700	[thread overview]
Message-ID: <20020921173035.00D507F5B@lobus.fungible.com> (raw)


For what it's worth (which may not be much), it is possible to write
classes in OCAML that are downcastable.  Here's how.

-- 
Tim Freeman       
tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 

module type Foo = sig
  class type superclass = object
    method data: exn
  end
            
  class subclass_1: string -> object
    inherit superclass
    method s: string
  end
                    
  exception Subclass_1 of subclass_1
    
  class subclass_2: int -> object
    inherit superclass
    method i: int
  end
                    
  exception Subclass_2 of subclass_2
    
  val x: superclass
end

module Foo: Foo = struct

  class type superclass = object
    method data: exn
      (* You can put more methods here if you like. *)
  end
            
  class subclass_1_impl (s: string) (makeexn: subclass_1_impl -> exn) =
  object (self: 'self)
    constraint 'self = #superclass
    method data: exn = makeexn (self :> subclass_1_impl)
    method s: string = s
  end
    
  exception Subclass_1 of subclass_1_impl
    
  class subclass_1 (s: string) = subclass_1_impl s (fun sc -> Subclass_1 sc)
    
  class subclass_2_impl (i: int) (makeexn: subclass_2_impl -> exn) =
  object (self: 'self)
    constraint 'self = #superclass
    method data: exn = makeexn (self :> subclass_2_impl)
    method i: int = i
  end
    
  exception Subclass_2 of subclass_2_impl
    
  class subclass_2 (i: int) = subclass_2_impl i (fun sc -> Subclass_2 sc)
    
  let _ = Random.self_init ()

  let x: superclass =
    if Random.bits () mod 2 = 0 then
      (new subclass_1 "blort" :> superclass)
    else
      (new subclass_2 17 :> superclass)
      
  let _ =
    match x#data with
        Subclass_1 s1 -> Format.printf "s is %s.\n@?" s1#s
      | Subclass_2 s2 -> Format.printf "i is %d.\n@?" s2#i
      | _ -> failwith "Downcast failed"
end
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2002-09-21 17:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-21 15:09 Tim Freeman [this message]
2002-09-23  9:49 ` Andreas Rossberg
2002-09-25  7:10 ` Jean-Christophe Filliatre

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=20020921173035.00D507F5B@lobus.fungible.com \
    --to=tim@fungible.com \
    --cc=caml-list@inria.fr \
    /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).