Version française
Home     About     Download     Resources     Contact us    
Browse thread
Label Names Space - philosophy or implementation?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: pbrisset@e...
Subject: Label Names Space - philosophy or implementation?

John Gerard Malecki writes:
> 
> A co-worker has (vehemently) pointed out to me that record label names
> cannot be shared.  For example, the following fails
> 
> 	type foo = { name : string; x : int }
> 	let    a = { name = "foo";  x = 1 }
> 
> 	type bar = { name : string; y : float }
> 	let    b = { name = "bar";  y = 9. }
> ...
> field with a standardized label.  This makes it easier for him to
> write printers for his data-structure.  Does anyone have a
> recommendation to make to him?

	type 'a foobar = { name : string; x : 'a}
	let print_name {name = s} = print_string s
	print_name { name = "foo"; x = 1 }
	print_name { name = "bar";  x = 9. }

or using classes and inheritance (I do not like it but ...) :

	class named (s:string) =
	  val private name = s
	  method name = name
	end
	class foo n (x:int) =
	  inherit named n
	  val private x = x
	  method x = x
	end
	class bar n (y:float) =
	  inherit named n
	  val private y = y
	  method y = y
	end
	let print_name x = print_string x#name
	print_name (new foo "foo" 1)
	print_name (new bar "bar" 9.)

My 0.02 euros...

--Pascal