caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Probleme avec des enregistrements
@ 2002-01-11 13:00 Frederic Tronel
  2002-01-11 19:41 ` Remi VANICAT
  2002-01-12  7:16 ` Bruno Pagano
  0 siblings, 2 replies; 3+ messages in thread
From: Frederic Tronel @ 2002-01-11 13:00 UTC (permalink / raw)
  To: caml-list

Bonjour,

J'utilise la version 3.04 de caml.
Je veux utiliser des enregistrements dont certains
des champs portent le meme nom. Cela ne fonctionne pas.
Ainsi:

<cime:479> ./ocaml
        Objective Caml version 3.04

# type t1={a:int ; b:int} ;;
type t1 = { a : int; b : int; }
# type t2 = {a:int} ;;
type t2 = { a : int; }
# {a=2} ;;
- : t2 = {a = 2}
# {a=1;b=2} ;;
The record field label b belongs to the type t1
but is here mixed with labels of type t2
#


Il n'y a pourtant aucune ambiguite sur le type de
{a=1;b=2} !
Est-ce un bug connu ?


Frederic Tronel.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Probleme avec des enregistrements
  2002-01-11 13:00 [Caml-list] Probleme avec des enregistrements Frederic Tronel
@ 2002-01-11 19:41 ` Remi VANICAT
  2002-01-12  7:16 ` Bruno Pagano
  1 sibling, 0 replies; 3+ messages in thread
From: Remi VANICAT @ 2002-01-11 19:41 UTC (permalink / raw)
  To: caml-list

Frederic Tronel <Frederic.Tronel@inrialpes.fr> writes:

> Bonjour,
> 
> J'utilise la version 3.04 de caml.
> Je veux utiliser des enregistrements dont certains
> des champs portent le meme nom. Cela ne fonctionne pas.
> Ainsi:
> 
> <cime:479> ./ocaml
>         Objective Caml version 3.04
> 
> # type t1={a:int ; b:int} ;;
> type t1 = { a : int; b : int; }
> # type t2 = {a:int} ;;
> type t2 = { a : int; }
> # {a=2} ;;
> - : t2 = {a = 2}
> # {a=1;b=2} ;;
> The record field label b belongs to the type t1
> but is here mixed with labels of type t2
> #
> 
> 
> Il n'y a pourtant aucune ambiguite sur le type de
> {a=1;b=2} !
> Est-ce un bug connu ?

ce n'est pas un bug. C'est dans les spécifications de ocaml. Mais
c'est en effet connu : la déclaration d'un champ de même nom qu'un
précédemment définit le cache .



-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Probleme avec des enregistrements
  2002-01-11 13:00 [Caml-list] Probleme avec des enregistrements Frederic Tronel
  2002-01-11 19:41 ` Remi VANICAT
@ 2002-01-12  7:16 ` Bruno Pagano
  1 sibling, 0 replies; 3+ messages in thread
From: Bruno Pagano @ 2002-01-12  7:16 UTC (permalink / raw)
  To: Frederic Tronel; +Cc: caml-list

> J'utilise la version 3.04 de caml.
> Je veux utiliser des enregistrements dont certains
> des champs portent le meme nom. Cela ne fonctionne pas.
> Ainsi:
> 
> <cime:479> ./ocaml
>         Objective Caml version 3.04
> 
> # type t1={a:int ; b:int} ;;
> type t1 = { a : int; b : int; }
> # type t2 = {a:int} ;;
> type t2 = { a : int; }
> # {a=2} ;;
> - : t2 = {a = 2}
> # {a=1;b=2} ;;
> The record field label b belongs to the type t1
> but is here mixed with labels of type t2
> #
> 
> 
> Il n'y a pourtant aucune ambiguite sur le type de
> {a=1;b=2} !
> Est-ce un bug connu ?
> 

Ce n'est pas un bug mais le prix a payer pour avoir la synthese de 
types dans le langage. En effet, si on ecrit la fonction suivante :

let access x = x.a ;;

Ocaml va deduire (inferer) que access est une fonction de type t2 -> int.
La seule information qu'il utilise est que ".a" signifie le champs 
"a" d'une valeur de type t2. Si cette information etait ambigue (t1 ou t2),
alors ocaml ne pourrait plus typer la fonction access ... ni grand chose
d'autre d'ailleurs.

C'est pour cette meme raison qu'il est peu de chance d'avoir un jour
de la surcharge de noms en ocaml.


Il existe cependant une possibilite pour separer les espaces de noms en 
utilisant des modules :
  si t1 et t2 sont declares dans des modules differents, ils
  peuvent avoir les memes identificateurs pour leurs champs.

module M1 = struct
  type t1 = { a : int ; b : int }
end

module M2 = struct
  type t2 = { a :int }
end

let v1 = { M1.a=1 ; M1.b=2 } ;;

(* ou encore *)

let access x = x.M2.a ;;

(* on reproduit l'erreur en ecrivant :  *)

{ M2.a = 1 ; M1.b =2 } ;;
The record field label M1.b belongs to the type M1.t1
but is here mixed with labels of type M2.t2


Cela peut paraitre contraignant de devoir donner des identificateurs
differents pour tous les champs d'un meme module; mais c'est une convention
de programmation qui me semble plutot saine, meme pour d'autres langages.

Bonne continuation avec caml.

Bruno Pagano
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2002-01-14 10:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-01-11 13:00 [Caml-list] Probleme avec des enregistrements Frederic Tronel
2002-01-11 19:41 ` Remi VANICAT
2002-01-12  7:16 ` Bruno Pagano

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