Array SAM Scala not allowed

40 Views Asked by At

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?

1

There are 1 best solutions below

0
Alexey Romanov On BEST ANSWER

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.