caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] troubles with polymorphic variant in class
@ 2004-06-13 16:05 François-Xavier HOUARD
  2004-06-13 17:59 ` skaller
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: François-Xavier HOUARD @ 2004-06-13 16:05 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2004-06-14 15:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-13 16:05 [Caml-list] troubles with polymorphic variant in class François-Xavier HOUARD
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

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).