簡體   English   中英

無法找到參數的隱含值

[英]Could not find implicit value for parameter

我最近開始學習Scala隱含的“魔法”,並且我遇到了隱式Scala對象的麻煩。 我已經嘗試了所有可能的變種,但似乎沒有任何效果。

讓我們假設我有一個這樣的類,帶有一些solve()函數。 如果輸入a,b是Float,它應該返回2個Float值。 否則它應該返回另一個類型值:

class Solver[T](val a: T, val b: T) {
    def solve[A](implicit num: customNumeric[T]): Option[(T, T)] = {
        Option(
            num.f(num.g(a)),
            num.f(num.g(b)))
    }
}

讓我們假設另一個類型值是類的對象,如下所示:

class MyClass[T] (x: T, y: T)(implicit num: customNumeric[T]) {
    val field : T = num.f(x)
}

而且我們還假設我沒有基本Scala數字中需要的函數,所以我應該創建自己的自定義數字。

這就是我所做的:

我使用我的方法f()和g()以及一些隱式對象為我自己的customNumeric創建了一個抽象類,這些對象擴展了我的customNumeric用於某些值類型(例如Int,Float)和實現的方法:

abstract class customNumeric[T] {
    def f(x: T): T
    def g(x: T): T
}

object customNumeric {
    implicit object IntIsCustomNumeric extends customNumeric[MyClass[Int]] {
        def f(x: MyClass[Int]) = new MyClass[Int](x.field + 5)
        def g(x: MyClass[Int]) = new MyClass[Int](x.field - 5)
    }
    implicit object FloatIsCustomNumeric extends customNumeric[Float] {
        def f(x: Float): Float = x + 3
        def g(x: Float): Float = x - 3
    }
}

在我看來,Solver的solve()應該使用隱式的customNumeric對象來獲取基於Solver輸入值類型的solve()內引用的方法的實現。

但這不適用於編譯器說:

could not find implicit value for parameter num: customNumeric[Int]
        def f...

它也抱怨,因為在同一行的構造函數MyClass沒有足夠的參數。

我已經嘗試使用伴侶對象將IntMyClass

object Fraction {
    implicit def int2MyClass(x: Int): MyClass[Int] = new MyClass[Int](x, 1)
}

但這似乎也沒有用。 我試圖創建另一個隱式對象來實現我在customNumeric [MyClass [Int]]中使用的方法。

你有什么想法? 提前致謝!

問題是您正在嘗試使用本身需要相同隱式對象的類來定義隱式對象。

這意味着:

class MyClass[T] (x: T, y: T)(implicit num: CustomNumeric[T])

需要存在隱式CustomNumeric[T] 您無法使用該類型定義IntIsCustomNumeric

implicit object IntIsCustomNumeric extends customNumeric[MyClass[Int]]

實現IntIsCustomNumeric ,需要為Int類型實現它,而不是為MyClass[Int]類型實現它。 當你這樣做,即:

object CustomNumeric {
  implicit object IntIsCustomNumeric extends CustomNumeric[Int] {
    override def f(x: Int): Int = x
    override def g(x: Int): Int = x
  }
}

現在,您可以創建一個Solver[Int] ,它采用隱式CustomNumeric[Int]

def main(args: Array[String]): Unit = {
  import CustomNumeric._

  val solver = new Solver[Int](1, 2)
  println(solver.solve)
}

現在,創建從Int類型到創建MyClass[Int]的隱式轉換也更容易:

implicit object MyClassIsCustomNumeric extends CustomNumeric[MyClass[Int]] {
  override def f(x: MyClass[Int]): MyClass[Int] = new MyClass[Int](x.field + 5)
  override def g(x: MyClass[Int]): MyClass[Int] = new MyClass[Int](x.field + 3)
}

implicit def intToMyClass(i: Int) = new MyClass[Int](i)

你怎么看待這件事

object customNumeric {

  implicit object IntIsCustomNumeric extends customNumeric[Int] {
    def f(x: Int): Int = x + 3

    def g(x: Int): Int = x - 3
  }

  implicit object FloatIsCustomNumeric extends customNumeric[Float] {
    def f(x: Float): Float = x + 3

    def g(x: Float): Float = x - 3
  }

  implicit def int2MyClass(x: Int): MyClass[Int] = new MyClass[Int](x, 1)

  implicit object cn extends customNumeric[MyClass[Int]] {
    def f(x: MyClass[Int]) = x.field + 5

    def g(x: MyClass[Int]) = x.field - 5
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM