geometric memory growth
Ulf Wiger (AL/EAB)
ulf.wiger@REDACTED
Fri Nov 25 14:45:27 CET 2005
Thomas Lindgren wrote:
>
> Hoisting the expression out of the fun is only safe if State
> is known to be a #state record at this point.
> Otherwise, you can get an exception in the wrong place.
It wasn't. Now it is. I try to consistently write patterns
like:
recv_fun(#channel{id = ChNo,
type = Type,
recv_transform = Transform,
decode = Decode,
on_receive = OnRecv}, #state{} = State) ->
SendF = fun(To, Msg) ->
msg(State#state.channel, ChNo, To, Msg)
end,
fun(Bin) ->
OnRecv(ChNo, SendF, Transform(Decode(Bin)))
end.
but in this particular case, I had been sloppy
(not that it would have mattered, I know.)
In Tallinn, a Haskell programmer was telling me how
the Haskell compiler can sometimes report a syntax error
as "missing semicolon", when the whole program had been
written without semicolon, relying on indendation alone.
I guess it's justified sometimes to risk confusing error
indications.
Of course, in this case, the choice would have been between
risking an exception in the wrong place, and performing an
optimization that may or may not have any noticeable effect
(even though in my case, the lack of optimization had
dramatic effect.)
BTW, I've found that people can get confused about
the #state{} = State pattern in function heads. The
confusion being what happens with default values.
E.g.
1> rd(person, {name, age, gender=male}).
person
2> rl(person).
-record(person, {name,
age,
gender = male}).
ok
3> Me = #person{name = "Ulf", age=38}.
#person{name = "Ulf",age = 38,gender = male}
4> #person{} = Me.
#person{name = "Ulf",age = 38,gender = male}
5> P = #person{}.
#person{name = undefined,age = undefined,gender = male}
6> P = Me.
=ERROR REPORT==== 25-Nov-2005::14:41:51 ===
Error in process <0.47.0> with exit value:
{{badmatch,{person,"Ulf",38,male}},[{erl_eval,expr,3}]}
** exited: {{badmatch,{person,"Ulf",38,male}},[{erl_eval,expr,3}]} **
The Erlang Reference Manual could possibly state more
clearly that #person{} is interpreted one way on the
right hand side, and another on the left hand side?
BR,
Uffe
More information about the erlang-questions
mailing list