Scala: checking if an object is Numeric

前端 未结 2 1457
刺人心
刺人心 2021-02-02 16:36

Is it possible for a pattern match to detect if something is a Numeric? I want to do the following:

class DoubleWrapper(value: Double) {
  override         


        
2条回答
  •  星月不相逢
    2021-02-02 17:17

    The original problem is not solvable, and here is my reasoning why:

    To find out whether a type is an instance of a typeclass (such as Numeric), we need implicit resolution. Implicit resolution is done at compile time, but we would need it to be done at runtime. That is currently not possible, because as far as I can tell, the Scala compiler does not leave all necessary information in the compiled class file. To see that, one can write a test class with a method that contains a local variable, that has the implicit modifier. The compilation output will not change when the modifier is removed.

提交回复
热议问题