#1030 Confused about the meaning of a line of Fantom code.

tcolar Wed 17 Mar 2010

Could you explain in detail the exact meaning of this (I'm working on type resolution and want to make sure I understand that right):

a := Actor(ActorPool()) |msg| { echo(msg); return msg }

I understand that a is going to be an actor, and I think the rest is a closure you pass to it, however I don't quite understand this syntax where you pass a closure at construction time ...

Most likely it's syntax sugar, in which case maybe you can give me the "long" version, so it can be clear to me what this does.

Thanks.

andy Wed 17 Mar 2010

If the last argument to a function is a closure, it can be specified "outside". So in this case Actor.make takes an optional closure - so these statements are equivalent:

Actor(ActorPool()) |msg| { echo(msg); return msg }
Actor(ActorPool(), |msg| { echo(msg); return msg })

tactics Wed 17 Mar 2010

It works the same with List.each, for example:

["Tom", "Dick", "Harry"].each |name| 
{
  echo(name)
}

It's prettier than

["Tom", "Dick", "Harry"].each(|name| 
{
  echo(name)
})

(Reminds me of Javascript...)

msl Wed 17 Mar 2010

Take a look at line 43 of http://hg.fantom.org/repos/fan-1.0?f=04dbaf32c675;file=src/sys/java/fan/sys/Actor.java

As Andy said - you're passing a closure to the constructor, which is then stored for later use when a message is received.

EDIT Sorry - line 47

Login or Signup to reply.