Scala implicits resolution mechanism is declaration order dependent?

前端 未结 1 1051
Happy的楠姐
Happy的楠姐 2021-01-15 04:22

During daily Scala coding I faced an issue that Scala implicits resolution depends on declaration order. A simple example:

object example extends App {
  tra         


        
1条回答
  •  南方客
    南方客 (楼主)
    2021-01-15 04:52

    A possible workaround keeping the order you wished:

    object A {
        implicit val aFoo: FooTypeClass[A] = new FooTypeClass[A] {
          def foo: A = new A {
            override def toString = "a"
          }
        }
      }
    

    I keep on seeking for the explanation why object (instead of val) doesn't fit.

    0 讨论(0)
提交回复
热议问题