Take the 2-minute tour ×

If I have the following code:

val wows = Buffer[Wow]()

def yo(f: _ => Wow) { wows += f }

and I get an error when trying to add f. I wonder, how can I use f inside the method body, or more precisely, how should I refer to it since f or f() or f(_) do not work.


The type of f cannot be changed to f: => Wow because the functions of type _ => Wow passed to this method come from such a class:

object Wonderful {

    val wows = Buffer[Wow]()


class Joy[+R](fs: Buffer[_ => R]) {

    def exalt() {

and that buffer cannot be parametrized with => R, it shows an error.

UPDATE 2: Both of you have second-answered before I have finished the explanation of the second part! Thanks! That's the speed!

UPDATE 3: Basically, I am learning Scala and I am trying to try out all that I can think of. In this particular piece of code, there happens the following thing: I have 3 basic classes:

  • WorldObject - represents all game objects (has x, y and such).
  • Emitter - represents something that emits objects over time (extends WorldObject).
  • Funset - a set of functions that should produce WorldObjects when called. In future I wanted to make them partially applied functions with some prepared arguments, passed directly to the corresponding factory methods.
  • World - where everything takes place.

The main point is that a Funset's collection of "generative" functions can be edited during runtime, that is why it is represented as buffer. On every update cycle an Emitter passes each of Funsets functions to the World's creator functions that manifest the generated objects in the world.

I hope I have explained so it can be understood... maybe a little bizzare or a wrong architecture, but.. anyways, I have learned something about Scala now!

share|improve this question
Can you please add more detail about what you are trying to do here? I am still unable to understand. –  missingfaktor Feb 16 '12 at 19:48
see the third update %) –  noncom Feb 16 '12 at 20:05
add comment

2 Answers

up vote 2 down vote accepted

_ => Wow is a method that takes a single argument of a type that you don't know and returns a Wow. You won't be able to call it since you don't know what sort of argument to call it with!

I suspect you want a method with no arguments, which you could do like the following:

def yo( f: () => Wow ) { wows += f() }

Also you can do a by-name parameter which is a little bit more implicit:

def you( f: => Wow ) { wows += f }

Edit: The difference is in how you call it; a by-name parameter just evaluates an expression when it is used. Passing a function is actually just passing a function which you can call at will.

share|improve this answer
yep, see the other comment for missingfaktor, it explains the problem further, where I can parametrize a buffer with Buffer[_ => R] but cannot do it with Buffer[=>R]... –  noncom Feb 16 '12 at 19:26
I think you probably want Buffer[()=>R]. –  Calum Feb 16 '12 at 19:28
add comment

Is it by-name parameter you are after? If so, your syntax is a bit off. Here is the correct way:

scala> class Wow
defined class Wow

scala> val wows = collection.mutable.Buffer.empty[Wow]
wows: scala.collection.mutable.Buffer[Wow] = ArrayBuffer()

scala> def yo(f: => Wow) { wows += f }
yo: (f: => Wow)Unit
share|improve this answer
Exactly! Here you took off the _ sign that stands for parameters. My code has it. And I have to stick with it because from outside, the passed fs go from a class class Buddy[+R](Buffer[_ => R]) {...} and if I take off the _ here, it, again, is going to complain. –  noncom Feb 16 '12 at 19:24
@noncom, I think you want () => R. –  missingfaktor Feb 16 '12 at 19:29
add comment

Your Answer


