Using summonInline[T] with summon[T] for resolving implicits in inline methods


Recently I was contributing code to Kittens, and they’re using inline methods as an alternative of passing a lot of implicits (since all givens from outer scope are available using summonInline). However I’ve realised that implicit resolution does not work since it seems to be using summon and not summonInline.

Scastie example: Scastie - An interactive playground for Scala.

I have question if it’s by design or in the future implicit resolution will use both summonInline and summon for inline methods - I couldn’t find any bug reports on dotty’s GitHub.

I agree that it’s pretty annoying that you have to explicitly pass around all implicits with summonInline in these cases. It can also take a while to figure out what’s going wrong if you forget one.

Perhaps as a compromise we could have a compileTimeOnly method scala.compiletime.summoningInline that enables summonInline as the default summoning method in a limited scope.

inline def printTC3Inline(): Unit = summoningInline {
1 Like

I made an example here of how you can summon inline and then introduce a new given in scope:

(It may create a case class as a helper, but constant folding will remove any allocation)

the usage looks like:

summonInlineWith[TC3[String]] {