scastie
scastie copied to clipboard
Allow meta-comment to split the worksheet to enable macro examples in Scastie
I think it will be very beneficial if we could run macro examples in Scastie. It will be great if we can virtually split the file into two to allow for macro examples. The most simple way I though of was to add a meta-comment.
Meta-comment
Using a meta-comment /* $NEW_SHEET$ */
will tell scastie to split the sheet.
import scala.quoted.*
transparent inline def getCompanion[E]: AnyRef = ${ getCompanionMacro[E] }
def getCompanionMacro[E](using Quotes, Type[E]): Expr[AnyRef] =
import quotes.reflect.*
val companionSym = TypeRepr.of[E].typeSymbol.companionModule
Ref(companionSym).asExprOf[AnyRef]
/* $NEW_SHEET$ */
trait Foo
object Foo
enum MyEnum:
case Baz, Bar
val foo : Foo.type = getCompanion[Foo]
val myEnum : MyEnum.type = getCompanion[MyEnum]
I think that implementation-wise this may be the simplest solution. The only tricky thing is to translate the positioning errors across the split files.
@soronpo afair macros should be in a separate module for 2.13, was that relaxed to separate file in 3?
Yes, what I mean was that the separate file will run as a test, which is considered to be a separate module.
So for a given sbt project, src/main/first_sheet
and src/test/second_sheet
will be considered separate modules and thus the project can compile.
So maybe the proper term is actually a test separator and not a sheet separator. /* $TEST$ */
understood