Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
soronpo opened this issue Oct 30, 2021 · 5 comments

Comments

@soronpo
Copy link

soronpo commented Oct 30, 2021

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.

@OlegYch
Copy link
Contributor

OlegYch commented Nov 9, 2021

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

@soronpo
Copy link
Author

soronpo commented Nov 9, 2021

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
Copy link
Author

soronpo commented Nov 9, 2021

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

@OlegYch
Copy link
Contributor

OlegYch commented Nov 9, 2021

understood

@tmccombs
Copy link

Any progress on this? Or is there another workaround for using scala 3 macros?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants