[erlang-questions] matching binaries directly into records

Tony Rogvall <>
Thu Jul 5 09:35:19 CEST 2007


But it would be nice to be able to write:

-record(thing, {
	first : 16,
	second : 16
}).

binary_to_thing(Data) ->
	<<Thing/#thing >> = Data,
	Thing.

Compact  and it gives some type info hints too ;-)

/Tony


On 5 jul 2007, at 07.36, Torbjorn Tornkvist wrote:

> Richard Andrews wrote:
>> Couldn't find an answer to this in the archives.
>>
>> I'd like to match a binary directly into a record; eg. syntax  
>> something like
>> the following:
>>
>> -record( thing, {first=0, second=0} ).
>>
>> binary_to_thing( Data ) ->
>>     <<R#thing.first:16, R#thing.second:16>> = Data,
>>     R.
>
> You need to do something like:
>
> binary_to_thing(<<First:16, Second:16>>) ->
>     #thing{first = First, second = Second}.
>
> --Tobbe
>
>>
>> ----
>> The above doesn't work, but the required functionality can be  
>> achieved by
>> binding to temporaries and then populating R from the temporaries  
>> as a second
>> step. My problem is that as the size of the record becomes large  
>> there is a lot
>> of clutter associated with the temporary bindings and I'd like to  
>> remove it if
>> possible.
>>
>> Will the temporaries will lead to inefficiency; or whether I can  
>> expect them to
>> be effectively optimised away?
>>
>> What is the best way to do this conversion from binary to record?
>>
>> --
>>   Rich
>>
>>
>>
>>        
>> _____________________________________________________________________ 
>> _______________ Yahoo!7 Mail has just got even bigger and better  
>> with unlimited storage on all webmail accounts.
>> http://au.docs.yahoo.com/mail/unlimitedstorage.html
>>
>>
>>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list