Re: Utilisation de

Jun P. Furuse (furuse@kaba.or.jp)
Sat, 06 Jul 1996 02:54:31 +0900

Message-Id: <199607051749.CAA28373@choshi.kaba.or.jp>
To: henry@kelenn-gw.univ-brest.fr (licence informatique)
Subject: Re: Utilisation de
Date: Sat, 06 Jul 1996 02:54:31 +0900
From: "Jun P. Furuse" <furuse@kaba.or.jp>

> J'utilise CamlTk 4.0 (version mai 96)
>
> Je souhaite utiliser une fonction retournant une valeur avec l'option Command
> dans un menu;
>
> par exemple :
> menu__add_command m [Label "Essai"; Command (let t = f(n))];
>
> Il s'avere que cette instruction ne fonctionne pas car, l'option Command
> accepte que les fonctions de type unit -> unit;
>
> Est-il possible de resoudre ce probleme sans utiliser des variables globales
> mutables ?

I am sorry that my answer is in English. I just started learning French,
and I probably understand your question, but it is difficult to answer
in French for me ;-).

I think this is impossible. In general event driven programming, you
can not define values in callback functions and export them out of the
functions.

let g = fun t -> t
in
menu__add_command m [Label "Essai"; Command (let t = f(n))];
...;
g t

Of course it is wrong syntax. But even if it were correct, the value
't' will not be defined until the command is invoked. So the
application of t for g will not be evaluated correctly unless the
command is called before it.

You must use mutable values to export values out of callbacks. For
example:

let rt = ref None in
let g = function Some t -> ....
| None -> ....
in
menu__add_command m [Label "Essai"; Command (rt := Some (f n))];
...;
g !rt

Not only references, you can also use mutable record fields or Tk's
textvariables as mutable values. But if you do not want to such
mutable values, you must do everything which needs t in the callback
function.

Yours sincerely,
-----------------------------------------------------------------------------
Jun P. Furuse | Research Institute for Mathematical Sciences
(furuse@kurims.kyoto-u.ac.jp) | Kyoto University, Japan