- 
  Notifications
 
You must be signed in to change notification settings  - Fork 1.1k
 
Compile the file at runtime #12854
-
I'd like to test, whether a source code passed as a string at runtime is a valid scala program.
My first attempt was to parse the input String to an Expr[] and then execute it with staging.run, but I haven't found any exposed parser API to transform a String into an Expr[], did I missed something?
My second attempt was to use scala.compiletime.testing.typeChecks inside a block passed to scala.quoted.staging.withQuotes, but typeChecks requires a statically known String so it also doesn't work for me. Is it possible to workaround it with the multi-stage programming?
The current approach is based on the Vulpix Framework and uses dotty.tools.dotc.Driver to compile source files (e.g. https://github.com/softwaremill/macwire/blob/300e109f2df670a8ec3178a83af73a50ea983f01/test-util/src/main/scala-3/com/softwaremill/macwire/CompileTestsSupport.scala#L92), but it seems that the Vulpix would fulfil my use case, have you considered publishing Vulpix as a separate library?
Or maybe I missed something and there is another way to compile a file at runtime?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
Compiling a new source file at runtime is not in the scope of the metaprogramming API as it cannot be type-checked att compile-time by the nature of the problem.
typeChecks will not work at runtime as it evaluates while typing the code which is too early for the run.
The way to compile a source file is by using the compiler directly. This could compile code that contains typeChecks to generate code in the event of compilation failure.
Beta Was this translation helpful? Give feedback.
All reactions
-
Ok, thank you for the quick answer.
So as far as I understand, I should stick with dotty.tools.dotc.Driver#process.
Nevertheless, I am also interested in the Vulpix Framework, is it also meant to be used outside of the dotty tests?
Beta Was this translation helpful? Give feedback.
All reactions
-
Vulpix was designed for internal use only. It might not be simple to use from outside the dotty project. We would also not be able to guarantee the stability of the API.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hopefully there would be a name change before exposing it as API.
This is a common ask. I mean "test for whether it compiles", not the framework name.
Beta Was this translation helpful? Give feedback.
All reactions
-
scala.compiletime.testing.typeChecks("class Foo") and typeCheckErrors exists to that end, in scala3-library.
Beta Was this translation helpful? Give feedback.