views:

100

answers:

3

Is this possible to reach? If yes, please correct my Foo declaration syntax.


class Foo (...) {
...
  def /* the nameless method name implied here */ (...) : Bar = new Bar (...)
...
}

class Bar (...) {
...
}

val foo : Foo = new Foo (...)

val fooBar : Bar = foo (...)


+3  A: 

I think the using 'apply' for you method name should work like this.

Felipe
+3  A: 

You can extend Function0[Bar] and implement def apply: Bar. See Function0:

 object Main extends Application {
   val currentSeconds = () => System.currentTimeMillis() / 1000L
   val anonfun0 = new Function0[Long] {
     def apply(): Long = System.currentTimeMillis() / 1000L
   }
   println(currentSeconds())
   println(anonfun0())
 }
eed3si9n
Just to clarify, there are two definitions shown here. `currentSeconds` here is an *anonymous function*, and is basically syntactic sugar for the definition shown by `anonfun0`
Kevin Wright
+11  A: 

You should use the apply method:

class Foo (y: Int) {
  def apply(x: Int) : Int = x + y
}


val foo : Foo = new Foo (7)

val fooBar  = foo (5)

println(fooBar)

Then run the code:

bash$ scala foo.scala 
12
olle kullberg