gpt4 book ai didi

scala - 列出所有可见的隐式

转载 作者:行者123 更新时间:2023-12-03 02:02:26 24 4
gpt4 key购买 nike

如果可能的话,如何使用反射和宏列出所有可见的隐式?我需要它能够分析可用的类型类实例。

一个用例

case class Artist (name: String, genres: Set[Genre])
case class Genre (name: String)

object CatalogueDB extends MySQL {
implicit val artistEntity = new Entity[Artist] {...}
implicit val genreEntity = new Entity[Genre] {...}
}

trait MySQL {
// Typeclasses let me restrict methods on `MySQL` to work only for types,
// which are associated with its instance.
// E.g., the following method will only compile when either an `Artist` or
// a `Genre` is passed to it when called on `CatalogueDB`:
def save[e : Entity](e : e) ...
// But also I need to be able to get a list of those types and
// instances of `Entity` typeclass to get the information
// needed to generate the database schema and
// to perform other tasks like that.
// So, the following method should somehow be able to have
// the `Entity` instances for `Artist` and `Genre` at its disposal,
// when called on `CatalogueDB`.
def generateSchema ...
}

最佳答案

我认为目前没有办法完全做到这一点。但是,列出给定类的所有成员并检查它们是否是隐式的应该相对容易 - 无论是在编译时还是在运行时。

关于scala - 列出所有可见的隐式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20722053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com