2015-12-23 108 views
0
package com.coryklein.lct.model 

import org.scalatest.FlatSpec 
import language.implicitConversions 

class VertexTest extends FlatSpec { 

    case class Vertex(x: Double, y: Double) 

    implicit def tupleWrapper(tuple: (Double, Double)): Vertex = 
    new Vertex(tuple._1, tuple._2) 

    "A Vertex" should "be implicitly created from a tuple" in { 
    val v: Vertex = (0, 0) 
    } 

} 

在這段代碼時要轉換的類型,(0,0)不會被隱式轉換成Vertex如我所料。爲什麼?隱不會導致預期

回答

3

請參閱您的編譯器錯誤:

Error:(17, 21) type mismatch; found : (Int, Int) required: VertexTest.this.Vertex 
    val v: Vertex = (0, 0) 
        ^

類型的(0,0)(Int,Int)不創建隱含的是(Double,Double)的定義相匹配,因此您的默許不能適用。

要解決您的問題,請更改類型以匹配或定義新的隱式轉換。

相關問題