Caching implicit resolution

后端 未结 2 1485
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 22:30

To reduce compile times of my project, I\'m caching certain type classes that are resolved by implicit lookups. This appears somewhat cumbersome though, because the straight for

相关标签:
2条回答
  • 2021-02-05 23:03

    Shapeless provides a cachedImplicit macro with an implementation that's very similar to yours (it uses shadowing to avoid the recursion, and the fact that it's a macro means the usage can be cleaner).

    There are some limitations to be aware of, and you may not want to take on a new dependency for this single method, but the implementation is pretty concise, and it's at least a good starting point.

    0 讨论(0)
  • 2021-02-05 23:07

    Just for the sake of completeness: the shapeless macro in the accepted answer shadows its own definition in a way I didn't come up with. My particular problem could therefore be solved this way:

    implicit val x: String = {
        def x = ???
        implicitly[String]
    }
    
    0 讨论(0)
提交回复
热议问题