使用Shapeless进行泛型类型转换

时间:2016-05-20 21:07:32

标签: scala generic-programming shapeless

我目前开始尝试使用Shapeless。我的第一次尝试是以下代码示例。 Shapeless版本是2.3.0,Scala版本是2.11.7:

type Family
        name:: AbstractString
        value:: Int
        left:: Nullable{Family}
        right:: Nullable{HuffmanNodeA}  
        Family(name:: AbstractString, value::Int ) = new(name, value , Nullable{Family}(), Nullable{Family}())
        end

A = [Family("Julia", 24), ...]

function minimalnia() 
global Family family
min = A[1].value
for i in A
if(i.value < min )
mininmalny = i.value
family = i 
end 
end
println(i)
return family
end 

此代码无法编译,因为我没有为LabelledGenerics提供隐式参数lgen的值。因此编译错误是

import org.scalatest._
import shapeless._

sealed trait Dog {
  def favoriteFood: String
}

sealed trait Cat{
  def isCute: Boolean
}

sealed trait Green

sealed trait Blue[G <: Green]{
  def makeGreen(): G = {
    val blueGen = LabelledGeneric[this.type]
    val greenGen = LabelledGeneric[G]
    val blue = blueGen.to(this)
    val green = greenGen.from(blue)
    green
  }
}

case class BlueDog(override val favoriteFood: String) extends Dog with Blue[GreenDog]
case class GreenDog(override val favoriteFood: String) extends Dog with Green

case class GreenCat(override val isCute: Boolean) extends Cat with Green
case class BlueCat(override val isCute: Boolean) extends Cat with Blue[GreenCat]

class ShapelessExperimentsTest extends FlatSpec with Matchers {

  "Make green" should "work" in {
    val blueDog = new BlueDog("Bones")
    val greenDog: GreenDog = blueDog.makeGreen
    assert(greenDog.favoriteFood == "Bones")

    val blueCat = new BlueCat(true)
    val greenCat: GreenCat = blueCat.makeGreen
    assert(greenCat.isCute)
  }
}

...ShapelessExperimentsTest.scala:16: could not find implicit value for parameter lgen: shapeless.LabelledGeneric[Blue.this.type]

我的问题是我无法找到提供这些隐含的正确方法来使示例正常工作。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

可悲的是,在Scala中,每个&#34;泛型 -ness&#34;必须在调用站点修复,这基本上意味着:

  val blueGen = LabelledGeneric[this.type]
  val greenGen = LabelledGeneric[G]

函数体中没有编译,因为编译器无法确定&#34; this.type&#34;和&#34; G&#34;。幸运的是,语言中的含义完全可以解决这个问题:

def makeGreen[T](implicit blueGen: LabelledGeneric.Aux[this.type, T], greenGen: LabelledGeneric.Aux[G, T]):

(Aux只是一种对类型进行计算的模式,如果您不熟悉,我强烈建议this文章。

同样,这段代码可能无法编译,因为编译器无法推断this.type实际上是一个case类,并且无法找到隐式的LabelledGeneric实例。

相反,您可以将代码重构为以下内容:

import org.scalatest._
import shapeless._

sealed trait Animal

sealed trait Dog extends Animal {
  def favoriteFood: String
}

sealed trait Cat extends Animal {
  def isCute: Boolean
}

sealed trait Color
sealed trait Green extends Color
sealed trait Blue extends Color

trait GreenColorable[A <: Animal, G <: Green] {
  def makeGreen[T](animal: A)(implicit animalGen: LabelledGeneric.Aux[A, T], greenGen: LabelledGeneric.Aux[G, T]): G = {
    val blue = animalGen.to(animal)
    val green = greenGen.from(blue)

    green
  }
}

object Colorables {
  def GreenColorable[A <: Animal, G <: Green] = new GreenColorable[A, G] {}
}

case class BlueDog(override val favoriteFood: String) extends Dog with Blue
case class GreenDog(override val favoriteFood: String) extends Dog with Green

case class GreenCat(override val isCute: Boolean) extends Cat with Green
case class BlueCat(override val isCute: Boolean) extends Cat with Blue

class ShapelessExperimentsTest extends FlatSpec with Matchers {

  "Make green" should "work" in {
    val blueDog = new BlueDog("Bones")
    val greenDogColorable= Colorables.GreenColorable[BlueDog, GreenDog]
    val greenDog = greenDogColorable.makeGreen(blueDog)
    assert(greenDog.favoriteFood == "Bones")

    val blueCat = new BlueCat(true)
    val greenCatColorable = Colorables.GreenColorable[BlueCat, GreenCat]
    val greenCat: GreenCat = greenCatColorable.makeGreen(blueCat)
    assert(greenCat.isCute)
  }
}

这里,实际的转换被移动到一个单独的类型类中,它将实际的case类输入和输出类型作为参数。