« Return to Thread: concise function literal as foreach argument

Re: concise function literal as foreach argument

by Chris Lewis-8 :: Rate this Message:

Reply to Author | View in Thread

Yeah that's it. PaulP explained this in the IRC channel. Getting used to all the magic takes a little time :)

thanks
chris

Daniel Sobral wrote:
Your problem is that "println" does not pass any arguments when it calls what you pass to it.

Or, in other words, you wrote:

tracks\\"track" foreach { println(x => x "name" text) }

And you wanted:

tracks\\"track" foreach { x => println(x \"name" text) }

When you use "_", the scope is almost aways the closest enclosing parenthesis.

On Sun, Jul 5, 2009 at 2:16 PM, Chris Lewis <chris@...> wrote:
Hello list,

I'm having a strange type resolution error in a function literal.
Given this:

tracks\\"track" foreach { track => println(track\"name" text) }

where tracks is a NodeSeq, I can iterate over each "track" element and
dump its "name" text node. The literal syntax doesn't have a type
declaration, and so I figured this could be further reduced:

tracks\\"track" foreach { println(_\"name" text) }

However the compiler falls over with:

error: missing parameter type for expanded function ((x$1) =>
x$1.$bslash("name").text)
   tracks\\"track" foreach { println(_\"name" text) }

That doesn't make sense, give that a) it knows the type of the
collection (inferred to NodeSeq) and b) the working example merely
names the variable, but doesn't declare its type. Am I missing
something here?

Sincerely,
Chris




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

 « Return to Thread: concise function literal as foreach argument