1

我开始使用 Scala 中的通用编程,我正在尝试设计一种灵活的可构建模式类型(ADT 的通用描述),它可以转换为第三方序列化(例如,circe、upickle)或模式(例如,tap ) 类型类实例。在为产品和副产品构建模式时,它必须能够解析子组件的模式。

我想实现这一点的方法是使用Provider类型类,它将通过解析 aDeriver或解析现有的隐式实例来“提供”给定类型的实例。通过使派生提供程序成为优先级较低的方法,它应该更喜欢实例而不是派生。

下面是我的解决方案的一个版本,它应用于一个更简单的案例:生成一个NamesFor[T]包含提取的 T 类型字段名称的实例。

import shapeless._
import shapeless.labelled.FieldType

trait NamesFor[ T ] {
    type Names

    def names : Names
}

object NamesFor {
    type Aux[ T, Names0 ] = NamesFor[ T ] { type Names = Names0 }
}

trait Deriver[ From, To ] {
    def derive : To
}

object Deriver {
    implicit def deriveNameFromSymbol[ S <: Symbol ](
        implicit wit : Witness.Aux[ S ],
    ) : Deriver[ S, String ] =
        new Deriver[ S, String ] {
            override def derive : String = wit.value.name
        }

    implicit def deriveNameFromFieldType[ K <: Symbol, T ](
        implicit deriver : Deriver[ K, String ],
    ) : Deriver[ FieldType[ K, T ], String ] = new Deriver[ FieldType[ K, T ], String ] {
        override def derive : String = deriver.derive
    }

    // Deriving HList
    implicit val deriveFromHnil : Deriver[ HNil, HNil ] =
        new Deriver[ HNil, HNil ] {
            override def derive : HNil = HNil
        }

    implicit def deriveNameHListFromHList[ Head, Tail <: HList, Res <: HList ](
        implicit
        headDeriver : Lazy[ Deriver[ Head, String ] ],
        tailDeriver : Deriver[ Tail, Res ],
    ) : Deriver[ Head :: Tail, String :: Res ] = new Deriver[ Head :: Tail, String :: Res ] {
        override def derive : String :: Res = headDeriver.value.derive :: tailDeriver.derive
    }

    // Here's the deriver we're interested in
    implicit def deriveNamesFromLabelledGeneric[ T, R <: HList, Names0 <: HList ](
        implicit
        lGenEv : LabelledGeneric.Aux[ T, R ],
        rDer : Deriver[ R, Names0 ],
    ) : Deriver[ T, NamesFor[ T ] ] = new Deriver[ T, NamesFor[ T ] ] {
        override def derive : NamesFor.Aux[ T, Names0 ] = new NamesFor[ T ] {
            type Names = Names0

            override def names : Names = rDer.derive
        }
    }
}

trait Provider[ T ] {
    def provide : T
}

object Provider {
    implicit def provideInstance[ T ](
        implicit inst : T,
    ) : Provider[ T ] = new Provider[T] {
        override def provide : T = inst
    }
}

trait LowPriorityProvider {
    implicit def provideDerivation[ From, To ](
        implicit
        deriver : Deriver[ From, To ],
    ) : Provider[ To ] = new Provider[ To ] {
        override def provide : To = deriver.derive
    }
}

我可以通过以下两种方式派生实例:

1:现有NamesFor[T]实例的隐式提供者

case class Test( int : Int )

implicit val namesInst : NamesFor[ Test ] = new NamesFor[Test] {
    override type Names = String :: HNil

    override def names : String :: HNil = "INT_FIELD" :: HNil
}

val provider = implicitly[ Provider[ NamesFor[ Test ] ] ]

println( provider.provide.names ) // output: INT_FIELD :: HNil

NamesFor[T]2: from的隐式派生者T

case class Test( int : Int )

val deriver = implicitly[ Deriver[ Test, NamesFor[ Test ] ] ]

println( deriver.derive.names ) // output: int :: HNil

但是当我尝试解析范围内NamesFor[T]没有隐式NamesFor[T]实例的提供者时,它失败了。也就是说,它无法通过以下方式解析实例provideDerivation

case class Test( int : Int )

val provider = implicitly[ Provider[ NamesFor[ Test ] ] ]

// Err: could not find implicit value for parameter e: Provider[NamesFor[Test]]
println( provider.provide.names )

关于如何使这一切正常工作的任何想法?

4

0 回答 0