0

It seems you can not initialize an Array with a SAM syntax. When I try the following...

trait A {
  def num(): Int
}

trait B extends A

trait C extends A

val nums: Array[A] = Array(() => 5)

I get the following error...

<console>:12: error: type mismatch;
 found   : () => Int
 required: A
       val nums: Array[A] = Array(() => 5)

Is this behavior expected?

uh_big_mike_boi
  • 3,350
  • 4
  • 33
  • 64

1 Answers1

1

It works in Scala 2.12, but SAM support in Scala 2.11 is incomplete (that's why it's under -Xexperimental). For Scala 2.11 you can use

Array[A](() => 5)

as a workaround.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487