Polymorphic record question

Andrew Lentvorski bsder@REDACTED
Mon Jun 19 10:16:44 CEST 2006

I define some basic packet records.  Every packet has a sequence number, 
and I would like to be able to pull out that sequence number regardless 
of packet type like so:



-record(u_P2PGen, {sequenceNumber}).
-record(u_P2PSyn, {sequenceNumber, synData}).
-record(u_P2PFin, {sequenceNumber, finData}).

test() ->
     Q0 = queue:new(),
     Q1 = queue:in(#u_P2PSyn{sequenceNumber=1, synData=foo}, Q0),
     Q2 = queue:in(#u_P2PFin{sequenceNumber=9, finData=bar}, Q1),
     io:format("Q2: ~p~n", [Q2]),

     {{value, P0}, Q3} = queue:out(Q2),
     io:format("P0: ~p~n", [P0]),
     io:format("SN: ~p~n", [P0#u_P2PGen.sequenceNumber]).

Obviously, with different types of packets hitting the queue, I don't 
know a priori which packet type it is.  Thus why I use a generic packet 
to get at the sequence number.

My question is: Is this "downcast" the only/best way of doing this kind 
generic handling?


More information about the erlang-questions mailing list