caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Bruno Pagano <pagano@altribe.com>
To: Frederic Tronel <Frederic.Tronel@inrialpes.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Probleme avec des enregistrements
Date: Sat, 12 Jan 2002 08:16:53 +0100	[thread overview]
Message-ID: <20020112081653.B29465@osiris.net-nono.com> (raw)
In-Reply-To: <3C3EE202.8C306B0B@inrialpes.fr>; from Frederic.Tronel@inrialpes.fr on Fri, Jan 11, 2002 at 02:00:50PM +0100

> 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


      parent reply	other threads:[~2002-01-14 10:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-11 13:00 Frederic Tronel
2002-01-11 19:41 ` Remi VANICAT
2002-01-12  7:16 ` Bruno Pagano [this message]

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=20020112081653.B29465@osiris.net-nono.com \
    --to=pagano@altribe.com \
    --cc=Frederic.Tronel@inrialpes.fr \
    --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).