Skip to content

Unituitive behaviour of code interpolator #7216

Closed
@abgruszecki

Description

@abgruszecki

scala.compiletime.code interpolator shows the source of arguments passed to inline functions. When showing non-by-name arguments, it only displays them if they are singletons (or could be constant-folded to singletons):

scala> inline def showCode(any: Any): String = {
     |   import scala.compiletime.code
     |   code"$any"
     | }
def showCode(any: Any): String

scala> showCode("a".toUpperCase)
val res0: String = any

scala> showCode("a")
val res1: String = a

scala> showCode("a" + "b")
val res2: String = ab

Note that res0 is "any", the code of the interpolated value. If there is a fundamental reason why we cannot display non-by-name arguments, we should note so in the documentation. Otherwise, code should be able to display such arguments.

Another unintuitive part is that even for by-name arguments, constant-folding occurs:

scala> inline def showCode2(any: => Any): String = {
     |   import scala.compiletime.code
     |   code"$any"
     | }
def showCode2(any: => Any): String

scala> showCode2("a".toUpperCase)
val res3: String = "a".toUpperCase()

scala> showCode2("a" + "b")
val res4: String = ab

scala>   

After talking about this with @nicolasstucki, this seems to be a fundamental limitation of our compiler architecture. We should note so in the documentation of code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions