Getting java.lang.NoSuchMethodException in structural typing of AnyVal

前端 未结 2 1776
猫巷女王i
猫巷女王i 2021-01-15 06:19

I have the following snippet that (I think) defines a method addNumber1(x:T):T on a generic type T which is a subtype of AnyVal and h

2条回答
  •  猫巷女王i
    2021-01-15 07:12

    I think the problem has nothing to do with AnyVal, AnyRef Or Any.

    addNumber1(new Foo("1")) 
    

    This works because you indeed defined a Foo class that provides an implementation of def +(s:Int):T.

    addNumber1(1)
    

    This doesn't work because Integer class doesn't provide it, as is mentioned in the exception:

    java.lang.NoSuchMethodException: java.lang.Integer.$plus(int)
    

提交回复
热议问题