Polymorphic record question

Bengt Kleberg bengt.kleberg@REDACTED
Tue Jun 20 09:51:03 CEST 2006


On 2006-06-20 08:17, Andrew Lentvorski wrote:
> Richard A. O'Keefe wrote:

...deleted
>> I'm a little surprised that the "downcasting" hack worked at all,
>> and I wouldn't expect it to _keep_ working in future releases.
> 
> Yeah, I was a bit surprised that it worked, too.  Presumably the 
> compiler just does a mapping from record field to tuple position number 
> and doesn't bother checking the first field to make sure that the names 
> match.

if you want to discover errors when using records it is best to avoid 
''.'' to access the members. see example below:

-module(t).

-export([main/1]).

-record(a,{data=a}).
-record(b,{data=b}).

main(_) ->
	A = #a{},
	B = #b{},
	should_not_work(B, A),
	will_fail(B, A),
	init:stop().

should_not_work(A, B) ->
	A_data = A#a.data,
	B_data = B#b.data,
	io:fwrite( "A_data ~w~n", [A_data] ),
	io:fwrite( "B_data ~w~n", [B_data] ).
	
will_fail(A, B) ->
	#a{data=A_data} = A,
	#b{data=B_data} = B,
	io:fwrite( "A_data ~w~n", [A_data] ),
	io:fwrite( "B_data ~w~n", [B_data] ).


bengt
-- 
    EPO guidelines 1978: "If the contribution to the known art resides
    solely in a computer program then the subject matter is not
    patentable in whatever manner it may be presented in the claims."



More information about the erlang-questions mailing list