Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: anonymous record types in variants
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: anonymous record types in variants
> But why? Natural semantic for anonymous types is the following: each
> anonymous record or algebraic types declaration introduces new type
> declaration in the current scope with some temporary type name. I.e.
> 
> 	type ('a, 'b) foo = A of 'a * {l : 'b; l2: int}
> 
> is a shortcut for:
> 
> 	type ('a, 'b) temp_name = {l1 : 'b; l2: int}
> 	and  ('a, 'b) foo = A of 'a * ('a, 'b) temp_name
> 
> Regards, 
> Anton Moscal

I proposed once (10 years ago ?) to have an ``as'' construct in data
type definitions: you could introduce a new datatype definition anywhere
in a datatype definition by writing

 (type_definition_body as type_name)

 with type_definition_body being one of the following:
  [ | C1 {of t1} | C2 {of t2} | ...] for sum types
  { lab1 : t1; lab2 : t2; ... } for record types
  type_expression for abbreviation types

the semantics you expect would then have been written as:

 	type ('a, 'b) foo = A of 'a * ({l : 'b; l2: int} as 'b temp_name)

This way the user could control the naming and number of type
arguments of the ``anonymous'' types defined, which is mandatory for
error messages from the typechecker, documentation purposes and clean
interfaces of modules.

As you mentioned it, such a scheme poses no problems at all to the
compiler, since it is just a shorthand for existing data type
definitions. On the other hand, it is conceivable to let the programmer
to expand this construct by hand, since it is just a few more lines to
write. That's the actual situation in the Caml light and Objective
Caml compilers.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/