caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "François-Xavier HOUARD" <IdeFX@Iname.com>
To: caml-list@inria.fr
Subject: [Caml-list] troubles with polymorphic variant in class
Date: Sun, 13 Jun 2004 18:05:57 +0200	[thread overview]
Message-ID: <1087142756.10383.50.camel@Bess> (raw)

English version:

Hi everyone

I'm working on an ocaml GUI library -more information about it later, on
the list ;)

I'm now working on an introspecting side of this GUI library; in a GUI,
every composant can have some child, and have a father.
The choice I'v made is to have a class for every type of composant, and
an object for every composant, for example, I have a window class, a
frame class (inheriting window), a button class, a panel class, etc.

So, as every composant can have different type of father, every object
is parametrized by the type of his father, for example, a button in a
panel in a window is, for the moment, typed as 
window panel button
as the button class looks like that:
class ['a] button (father:'a)
....
and the panel too,etc

I find it nice, cause every widget has got his "genealogic tree" in his
type.

What I wan't now is to store every widget child in a list.
as every child should be from a different type, and as I wan't the user
to be able to add his own widget-class later, I first thought of the use
of polymorphic variant, like that:

class ['a] button (father:'a) = 
object(self)
  val f = father
  val mutable childs = [`Whatever]
  method add_child (x) = childs <- x::childs
  method get_variant  = `A self
end;;

but caml tells me that:
The method add_child has type ([> `Whatever ] as 'a) -> unit where 'a
is unbound

This could be solved by adding a 'b parameter to my class:

class ['a, 'b] button (father:'a) = 
object(self)
  val f = father
  val mutable childs = [`Whatever]
  method add_child (x:'b) = childs <- x::childs
  method get_variant :'b = `A self
end;;

but this may make my class type unreadable:

[> 'Whatever | `Window of window ] window [> 'Whatever | `Window of
window | `Panel of panel ] panel [> 'Whatever | `Window of window |
`Panel of panel | `Button of button ] button

So, I have 2 questions:
_ Why should an open type as [>`Whatever] be parameter of a class ?? As
far as I know it, [>`Whatever] is equivalent to [>`Whatever | `Foo of
bar | `Foobar of foobar ], isn't it ?? so what problem would it make for
the inheritage stuff (which are the reason of the use of parameter)
_Is there another way to make it ??? (yes, i know coca-ml, but, another
way ??)

Thanks for all

FX Houard

Version Française:

Bonjour tous le monde

je travail sur une librairie de GUI en Caml -plus d'infos plus tard sur
la liste ;)

Je travail sur la partie introspective de cette librairie de GUI; dans
une GUI, chaque composant peut avoir des enfants, et a un père.

Le choix que j'ai fais est d'avoir une class pour chaque type de
composant, et un objet pour chaque composant; par exemple, j'ai une
class de "fenetre", in class de "frame", une de panel, une de bouton,
etc...

Donc, comme chaque composant peut avoir un père de type différent,
chaque objet est paramétré par le type de son père, par exeample, un
bouton dans un panel dans une fenetre a le type:
window panel button
comme la classe bouton ressemble à ça:
class ['a] button (father:'a)
....

et la classe panel aussi, etc..

je trouve ça plutôt chouette, puisque chaque widget a son arbre
généalogique dans son type

Ce que je voudrais maintenant, c'est stocker les enfants de chaque
widget dans une liste.
Comme chaque enfant peut être d'un type différent, et comme je veux que
l'utilisateur puisse rajouter ses propres classes de widget, j'ai
d'abord pensé aux variants polymorphes: 

class ['a] button (father:'a) = 
object(self)
  val f = father
  val mutable childs = [`Whatever]
  method add_child (x) = childs <- x::childs
  method get_variant  = `A self
end;;

Mais caml me réponds
The method add_child has type ([> `Whatever ] as 'a) -> unit where 'a
is unbound (je vais pas traduire ça, non plus !!;)

Celà pourrait être résolu en ajoutant le paramètre 'b à ma classe:

class ['a, 'b] button (father:'a) = 
object(self)
  val f = father
  val mutable childs = [`Whatever]
  method add_child (x:'b) = childs <- x::childs
  method get_variant :'b = `A self
end;;

Mais ça devrait rendre mes types illisibles:

[> 'Whatever | `Window of window ] window [> 'Whatever | `Window of
window | `Panel of panel ] panel [> 'Whatever | `Window of window |
`Panel of panel | `Button of button ] button

J'ai donc 2 questions:
_Pourquoi un type ouvert comme [>`Whatever] devrait être un paramètre de
ma classe ? d'autant que je le sache, [>`Whatever] est équivalent à 
[>`Whatever | `Foo of bar | `Foobar of foobar ], non ??
Alors quel problème pour l'héritage (qui est la raison de l'utilisation
de paramètre) celà ferait il ??
_Y aurait il une autre façon de faire ce que je veux (je connais certe
coca-ml, mais, une autre façon existe peut être) ?

Merci pour tous

FX Houard

-------------------
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:[~2004-06-13 16:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-13 16:05 François-Xavier HOUARD [this message]
2004-06-13 17:59 ` skaller
2004-06-14  9:29   ` François-Xavier HOUARD
2004-06-14  9:48     ` Damien Doligez
2004-06-14 15:49     ` skaller
2004-06-14  9:37 ` Richard Jones
2004-06-14 14:48 ` nakata keiko

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=1087142756.10383.50.camel@Bess \
    --to=idefx@iname.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).