有沒有辦法LabelledGeneric用有關欄位注釋的資訊來標記生成的無形體?
例如
case class Example(@someAnnotation foo: Foo, bar: Bar)
我們能得到類似的東西嗎
FieldType["foo" with Annotations(someAnnotation :: HNil), Foo] :: FieldType["bar", Bar] :: HNil
?
uj5u.com熱心網友回復:
您可以擁有 HListsFieldType['foo, Foo] :: FieldType['bar, Bar] :: HNil并Some[someAnnotation] :: None.type :: HNil使用型別類shapeless.LabelledGeneric,shapeless.Annotations
implicitly[LabelledGeneric.Aux[Example,
FieldType[Witness.`'foo`.T, Foo] :: FieldType[Witness.`'bar`.T, Bar] :: HNil]]
implicitly[Annotations.Aux[someAnnotation, Example,
Some[someAnnotation] :: None.type :: HNil]]
請注意'foo,'bar(寫為Witness.`'foo`.T,Witness.`'bar`.T由于技術原因)是單型別的亞型Symbol,沒有String。
如果你真的需要交點(的HList with)或元組(fieldName, annotation),那么你可以使用型別類的一個拉鏈的HLists shapeless.ops.hlist.Zip,shapeless.ops.hlist.ZipOne,shapeless.ops.hlist.ZipWith。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394696.html
