2017-04-16 6 views
3

を上一致する私は私が働いていた場合は、このコードは罰金だったでしょう型崩れライブラリパターンは、単純な型崩れHLIST

import shapeless.LabelledGeneric 
case class Icecream(name: String, numberOfCherries: Int, inCone: Boolean) 

object ShapelessRecordEx2 extends App { 
    val gen = LabelledGeneric[Icecream] 
    val hlist = gen.to(Icecream("vanilla", 2, false)) 
    hlist match { 
     case h :: _ => println(h) 
    } 
} 

を使用して、この単純なコードを書いたしかし、たとえ

Error:(12, 14) constructor cannot be instantiated to expected type; 
found : scala.collection.immutable.::[B] 
required: shapeless.::[String with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("name")],String],shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("numberOfCherries")],Int],shapeless.::[Boolean with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("inCone")],Boolean],shapeless.HNil]]] 
     case h :: _ => println(h) 

をコンパイルしていません普通のリスト

+2

'shapeless。::'をインポートする必要があります。 '::'は 'scala.immutable.collection :: :: 'と表示されません。 – Marth

+1

ありがとう!それはうまくいった。 –

答えて

3

デフォルトでインポートが必要なのはscala.Predef::演算子をscala.collection.immutable.Listからインポートします。

import shapeless.LabelledGeneric 
import shapeless.:: 
case class Icecream(name: String, numberOfCherries: Int, inCone: Boolean) 

object ShapelessRecordEx2 extends App { 
    val gen = LabelledGeneric[Icecream] 
    val hlist = gen.to(Icecream("vanilla", 2, false)) 
    hlist match { 
     case h :: _ => println(h) 
    } 
} 

ListCompat._をインポートする別のオプションがあります。

import shapeless.HList.ListCompat._ 

object ShapelessRecordEx2 extends App { 
    val gen = LabelledGeneric[Icecream] 
    val hlist = gen.to(Icecream("vanilla", 2, false)) 
    hlist match { 
    case h #: _ => println(h) 
    } 
} 
関連する問題