How do you create an implementation of the abstract attribute Function1

Scala is new here. I am very confused about why the code below throws an exception. I know I Function1[Int, Int]have an abstract applytype method Int => Intthat needs to be defined. Doesn't that make it lower oddfunc? Why x(3)doesn't it call the specific method applydefined in oddfunc?

Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait oddfunc extends Function1[Int,Int] {
 | def apply(a: Int): Int = a+3
 | }
defined trait oddfunc

scala> val x = new oddfunc{}
x: oddfunc = <function1>

scala> x(3)
java.lang.AbstractMethodError: $anon$1.apply$mcII$sp(I)I
  at oddfunc$class.apply(<console>:8)
  at $anon$1.apply(<console>:8)
  ... 43 elided
+4
source share
1 answer

This is similar to a bug that was introduced into the Scala compiler between versions 2.11.4 and 2.11.5. I dropped to Scala 2.11.4 to make sure it fixed and it happened.

Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait oddfunc extends Function1[Int,Int] {
     | def apply(a: Int): Int = a+3
     | }
defined trait oddfunc

scala> val x=new oddfunc{}
x: oddfunc = <function1>

scala> x(3)
res0: Int = 6
+2
source

All Articles