scastie icon indicating copy to clipboard operation
scastie copied to clipboard

Allow meta-comment to split the worksheet to enable macro examples in Scastie

Open soronpo opened this issue 3 years ago • 4 comments

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 avatar Oct 30 '21 12:10 soronpo

@soronpo afair macros should be in a separate module for 2.13, was that relaxed to separate file in 3?

OlegYch avatar Nov 09 '21 15:11 OlegYch

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.

soronpo avatar Nov 09 '21 16:11 soronpo

So maybe the proper term is actually a test separator and not a sheet separator. /* $TEST$ */

soronpo avatar Nov 09 '21 18:11 soronpo

understood

OlegYch avatar Nov 09 '21 20:11 OlegYch