From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA19470 for caml-redistribution; Mon, 14 Jun 1999 22:49:38 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA21529 for ; Mon, 14 Jun 1999 22:48:20 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id WAA12224; Mon, 14 Jun 1999 22:48:18 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA24106; Mon, 14 Jun 1999 22:48:19 +0200 (MET DST) From: Pierre Weis Message-Id: <199906142048.WAA24106@pauillac.inria.fr> Subject: Re: Constante NIL To: Xavier.Leroy@inria.fr (Xavier Leroy) Date: Mon, 14 Jun 1999 22:48:19 +0200 (MET DST) Cc: caml-list@inria.fr In-Reply-To: <19990602210210.48838@pauillac.inria.fr> from "Xavier Leroy" at Jun 2, 99 09:02:10 pm X-Mailer: ELM [version 2.4 PL24 ME8] MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: weis > > N'est t'il pas concevable de rajouter a Objectif Caml une constante > > NIL, UNDEF ou je sais pas trop comment la nommer dont la valeur serait > > '_a. Ceci permettrait d'initialiser "par defaut", par exemples, > > les variables d'instances d'une classe lorsque l'on ne possede aucune > > valeur par defaut du bon type. Je sais qu'il y'a toujours la possibilite > > d'utiliser le type 'a option mais ca devient lourd a gerer... As Xavier pointed out a nil constant is fairly difficult and runtime consuming to implement in Caml. On the type level, you cannot assign it the ``for all 'a. 'a'' type scheme, since otherwise you would get as many ``bus errors'' as you want (nil can be considered a function with such a type scheme). Uses of Obj.magic has also to be prohibited for the same reasons (fairly too difficult to avoid bus error). > Le problème de la constante "nil" est d'assurer qu'elle n'est pas > utilisée comme une valeur "normale" du type en question. Right. So why not considering a nil construct that IS always a value of the corresponding type ? You may consider to add a special construct ``any'' to the language, known by the typechecker and the compiler. The typechecker would ensure that ``any'' is never used in a (fully) polymorphic way, and the compiler may generate the code to build a value of the type assigned to ``any'' by the typechecker. This way, you would get for instance: #(any : int);; 0 : int #(any : string);; "" : string #(any : 'a list);; [] : 'a list But still, any is not a ``nil'' polymorphic value: #any;; Compilation Failed: cannot create a value of type 'a And this could work just fine for more complicated (and polymorphic) data structures: #type 'a dlist = {mutable hd : 'a dlist; mutable tl : 'a dlist};; Type dlist is defined #let l = (any : 'a dlist);; Value l is dlist0 where dlist0 = {hd=dlist0; tl=dlist0} : '_a dlist Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/