Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Vyskocil Vladimir <vyskocil@math.unice.fr>
To: Caml list <caml-list@inria.fr>
Subject: Ocaml et les objets
Date: Tue, 03 Jun 1997 16:09:36 +0200	[thread overview]
Message-ID: <339425A0.7A65285D@math.unice.fr> (raw)

Bonjour,

J'ai quelques questions sur la programmation objet avec objective caml
qui me pose probleme... Je commence a me rendre
compte qu'il faut oublier la vision C++ des objets car en caml les
notions d'heritage et de type ne sont pas tres lies mais justement ca
me pose des problemes. Par exemple si l'on veut creer une liste qui
contienne des objets derives d'un type parent il faut les "caster"
en ce type parent mais ensuite comment les recuperer en tant qu'objets
derives ? J'ai essaye le package Obj avec notamment la fonction
Obj.magic mais ca me parait un peu risque et je n'arrive pas a faire une
fonction qui puisse renvoyer un objet derive quelconque car justement il
n'ont pas des types compatibles, quand est t'il du type #class_parent
qui est sense etre compatible avec tout les objets descendants de la
classe class_parent ? En fait ce qui me perturbe le plus c'est a quoi
sert l'heritage si les objets derives ne sont pas compatible entre eux ?

Voici un exemple :

class object (class_name : string) =
  val name = class_name

  method get_name = name

and o_int (new_i : int) =
  inherit object "o_int"
  
  val i = new_i 
 
  method get_value = i

and o_float (new_f : float) =
  inherit object "o_float"
  
  val f = new_f 

  method get_value = f
end

let o1=new o_int 1;;
let o2=new o_float 2.5;;
let l = [(o1 :> object); (o2 :> object)];;

(* ces fonctions permettent de convertir un objet generique *)
(* en objet derive                                          *)
let o_int o = (Obj.magic o : o_int);;
let o_float o = (Obj.magic o : o_float);;

(* mais comment ecrire une fonction qui a partir du nom de  *)
(* de l'objet (methode get_name) renvoie soit un o_int soit *)
(* soit un o_float ?                                        *)


Merci d'avance si vous pouvez m'eclaircir les idees a ce sujet...

-- 
Vyskocil Vladimir
vyskocil@math.unice.fr
http://www.inria.fr/safir/WHOSWHO/Vladimir.html





             reply	other threads:[~1997-06-03 15:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-06-03 14:09 Vyskocil Vladimir [this message]
1997-06-05 10:08 ` Francois Rouaix

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=339425A0.7A65285D@math.unice.fr \
    --to=vyskocil@math.unice.fr \
    --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