Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Utilisation de
@ 1996-07-04 15:17 licence informatique
  1996-07-05 17:44 ` Francois Rouaix
  1996-07-05 17:54 ` Jun P. Furuse
  0 siblings, 2 replies; 3+ messages in thread
From: licence informatique @ 1996-07-04 15:17 UTC (permalink / raw)
  To: caml-list



Bonjour

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 ?

	Merci.

-- David HENRY --- Licence Informatique --
----------------------------- UBO BREST --







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

* Re: Utilisation de
  1996-07-04 15:17 Utilisation de licence informatique
@ 1996-07-05 17:44 ` Francois Rouaix
  1996-07-05 17:54 ` Jun P. Furuse
  1 sibling, 0 replies; 3+ messages in thread
From: Francois Rouaix @ 1996-07-05 17:44 UTC (permalink / raw)
  To: licence informatique; +Cc: caml-list


Cher usager,

> 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; 

De toutes facons elle est syntaxiquement incorrecte.
Les callbacks (Command ou autres) ne retournent jamais de valeur, pour
la simple raison que leur appelant est la boucle mainLoop de Tk. Ca n'a donc
pas de sens de renvoyer une valeur, sauf si la specification du callback
demande une valeur de retour (ce qui n'est jamais le cas pour la partie
standard de Tk). D'ou le -> unit pour tous les types de callbacks.

> Est-il possible de resoudre ce probleme sans utiliser des variables globales 
> mutables ?
Les callback sont par essence des fonctions qui produisent des effets de bord.
On utilise effectivement assez souvent des references ou des valeurs mutables
dans ce type de situation. La reference n'a pas forcement besoin d'etre
definie globalement bien sur, il suffit qu'elle ait la bonne portee.


----
Dear user,

[The question was about the inability to return values from callbacks]

Anyway, the example above is syntactically incorrect.
Callbacks (Command or others) never return values, for the simple reason
that their caller is the Tk mainLoop. Returning a value has no meaning,
except if the callback specification required a return value, which is
never the case in the standard Tk. Thus the -> unit for any callback type.

--f





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

* Re: Utilisation de
  1996-07-04 15:17 Utilisation de licence informatique
  1996-07-05 17:44 ` Francois Rouaix
@ 1996-07-05 17:54 ` Jun P. Furuse
  1 sibling, 0 replies; 3+ messages in thread
From: Jun P. Furuse @ 1996-07-05 17:54 UTC (permalink / raw)
  To: licence informatique; +Cc: caml-list


> 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





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

end of thread, other threads:[~1996-07-05 19:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-04 15:17 Utilisation de licence informatique
1996-07-05 17:44 ` Francois Rouaix
1996-07-05 17:54 ` Jun P. Furuse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox