[erlang-questions] matching binaries directly into records

Torbjorn Tornkvist tobbe@REDACTED
Thu Jul 5 07:36:58 CEST 2007


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
> 
> 
> 




More information about the erlang-questions mailing list