[erlang-questions] Invoking function as record-member

Edwin Fine <>
Wed Nov 5 22:26:34 CET 2008


It's a precedence problem.

4> rd(test, {my_fun}).
test
5> MyFun = fun() -> "Hello" end.
#Fun<erl_eval.20.67289768>
6> MyRec = #test{my_fun = MyFun}.
#test{my_fun = #Fun<erl_eval.20.67289768>}
7> MyRec#test.my_fun().
* 1: syntax error before: '('
7> (MyRec#test.my_fun)().
"Hello"


2008/11/5 Jeroen Koops <>

> Hello,
>
> I have a record with a member to which I assign a function:
>
> $ cat test.hrl
> -record(test, { my_fun }).
> $
>
> and, from the Erlang shell:
>
> 18> rr("test.hrl").
> [test]
> 19> X=#test{my_fun = fun(X) -> X*2 end}.
> #test{my_fun = #Fun<erl_eval.6.49591080>}
>
> When I try to invoke the function, the shell complains with a syntax error:
>
> 20> X#test.my_fun(1).
> ** 1: syntax error before: '(' **
>
> Of course, it does work when first assigning the record-member to a
> variable:
>
> 21> Y=X#test.my_fun.
> #Fun<erl_eval.6.49591080>
> 22> Y(1).
> 2
>
> Is there some syntactical subtlety I'm missing out on, or is this something
> I should just not want?
>
> Thanks,
>
> Jeroen
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081105/c251a20c/attachment.html>


More information about the erlang-questions mailing list