[erlang-questions] FW: Any wisdom to offer on "tagged return values"

David Mercer <>
Wed May 6 19:24:59 CEST 2009


You could always extract the element you want of a tuple using element/2
thus allowing chaining.  E.g.:

	element(2, {ok,_} = dict:find(...))

which is roughly equivalent to dict:fetch/2.

Less pretty, but a work-around...

> -----Original Message-----
> From:  [mailto:erlang-questions-
> ] On Behalf Of Ulf Wiger
> Sent: Wednesday, May 06, 2009 10:37 AM
> To: Joe Armstrong
> Cc: 
> Subject: Re: [erlang-questions] Any wisdom to offer on "tagged return
> values"
>
> Joe Armstrong wrote:
> >
> > I would always prioritize clarity over efficiency, unless your code
> > just has to be blindingly fast - so you might break this rule if you
> > want to write faster but less clear code.
>
> Luckily in this case, using fetch() when appropriate results
> in code that is both faster and clearer.  ;-)
>
> One can achive reasonably similar clarity by using
>
> {ok, Value} = dict:find(...)
>
> But doing so, means opting out of composition
> (at least /convenient/ composition.)
>
> Yesterday, as I was trying to figure out why edoc
> crashed when trying to write a processed file to disk,
> I wrote the following in the shell:
>
> 31> string:chr(tl(v(23)),8211).
> 1240
> 32> string:substr(tl(v(23)),1230,20).
> [45,75,101,121,32,112,97,105,114,32,8211,32,97,32,116,117,
>   112,108,101,32]
> 33> [45,75,101,121,32,112,97,105,114,32].
> "-Key pair "
>
> (The character following looked like ~ in emacs, but
> became ASCII 8211 in the output, which io:put_chars() had
> issues with.)
>
> Not exactly revolutionary, nor especially elegant, but
> it's extremely convenient to be able to chain functions
> together without having to insert unwrapping code.
>
> BR,
> Ulf W
>
> --
> Ulf Wiger
> CTO, Erlang Training & Consulting Ltd
> http://www.erlang-consulting.com
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions





More information about the erlang-questions mailing list