[erlang-questions] Parse-transforming !! to a function call

Robert Virding <>
Sun Aug 26 03:59:16 CEST 2007


On 25/08/07, Jeff Rogers <> wrote:
>
> Robert Virding wrote:
>
> >> Could a parse transform recognize the case where the value A ! B is not
> >> discarded and change those cases into some kind of synchronous
> call?  So
> >> that
> >>
> >> A = spawn(...),
> >> A ! whatever,
> >> ok.
> >>
> >> is a async call but
> >>
> >> A = spawn(...),
> >> B = A ! whatever,
> >> B.
> >>
> >> is a synchronous call.  The return value of ! is normally just the
> value
> >> that was sent, which doesn't seem very useful.  The only places where
> it
> >> would not be immediately evident if the return value is ignored is when
> >> a function or Fun evaluates to a send.  Even so, ! normally looks like
> >> an imperative operator more than a functional one so this seems to make
> >> it more functional (but also lazy in some respects, which is
> >> anti-erlang/soft-rt)
> >
> >
> > That would result in forcing people to write some really weird code. I
> could
> > never for example just end a function with a normal async send as that
> would
> > be interpreted as sync  send.
>
> I think the opposite would be true - since the compiler (or rather,
> parse transformer) can only see the immediate surroundings it can make
> no assumption about other scopes; since it would be new semantics the
> safe assumption would always be that the old semantics are the default.
>   So you would not be able to end your function with a synchronous send
> because it would be interpreted as async;  you would need to jump
> through a hoop to send with a sync call.
>
> From my very limited understanding of how the AST works, it would need
> a very simple transform like
> {match,Ln,L0,{op,SLn,'!',L1,R1}} ->
> {match,Ln,L0,{call,SLn,{atom,SLn,sync_send},[L1,R1]}}
> to change  "A = B ! C" into "A = sync_send(B,C)" and that wouldn't touch
> any other syntax.

> Generally the idea of having the semantics of something change depending
> on
> > what comes after seems a little off.
>
> It would be what comes before it, not after it.  I would agree that
> radically changing the semantics of the expression based on external
> surrounding syntax is odd at best, but I think it could be useful and
> not terribly confusing (other than the issues that have already been
> raised about how to specify the behavior of the synchronous call) .


The problem is not whether it is difficult to do in the compiler but rather
if we want to do it. Having the semantics of :

    A ! Msg

and

    B = A ! Msg

being different would be a first in the language and I personally think
completely horrendous.
Also the semantic difference between sending an async message and doing a
sync rpc is VERY large.
Also if you were to add one end of a sync message passing then you should
also add the other end as well, both receiving the message and returning the
reply.
If instead it was defined to translate into a certain function call which
you yourself then had to provide then that would also be a first, and I
honestly don't see the benefit of it. Why not just write the function call
yourself?
Also there are people who use the return value of a send, not just as a
side-effect. They won't be happy.

Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070826/11282a67/attachment.html>


More information about the erlang-questions mailing list