-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add eval (-e) expression evaluation to command line #14263
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
add eval (-e) expression evaluation to command line #14263
Conversation
@@ -149,6 +149,13 @@ object MainGenericRunner { | |||
process(remainingArgs, settings) | |||
case (o @ colorOption(_*)) :: tail => | |||
process(tail, settings.withScalaArgs(o)) | |||
case "-e" :: expression :: tail => | |||
val tempScript = writeFile(s"@main def main(args: String *): Unit =\n ${expression}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would omit the newline character as a needless complication.
I don't remember whether scala 2 has to write a file, but I bet it doesn't. Is it necessary to write a file?
Tired recently of deleting scala temp files from my WSL /tmp due to running scala
and dotty tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, possibly newline and indent matters for braceless syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would omit the newline character as a needless complication.
Sounds good.
Tired recently of deleting scala temp files from my WSL /tmp due to running
scala
and dotty tests.
Totally agree. I reviewed the scala2 code, and it doesn't create a file, although it's has different Runners available.
I will poke around a bi more and see if I can find something.
A new class dotty.tools.scripting.StringDriver provides a way to compile and run source code from a String, rather than from a file. The code it shares with |
end match | ||
end detectMainClassAndMethod | ||
|
||
def pathsep = sys.props("path.separator") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a previous effort to avoid dependencies on packages that might become modules in 2.13, which did not happen. I'm conflicted about sys.props
because OT1H it's just a Map
interface for System.getProperty
or ies
, but OTOH it is not so trivial that it didn't harbor a couple of bugs.
I guess the convenience here is java.io.File.pathSeparator
. No big deal, I just happened to have recently seen the Scala 2 commit about not using sys.props
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware of the discussion around packages that might become modules, but java.io.File.pathSeparator
is a good alternative in any case.
It turns out that pathsep
is defined identically in dotty.tools.scripting.Main, so the definition maybe belongs in dotty.tools.scripting.Util.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
fix ExpressionTest#verifyImports test compile error introduced in #14263
Implements a command line expression evaluator.
A temporary script file is created and executed, with a prefix inserted:
The -save option is disabled, to avoid uselessly creating a jar.