Skip to content

allow to run a snippet when invoking the REPL #1854

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

Closed
bjornregnell opened this issue Feb 13, 2023 · 4 comments
Closed

allow to run a snippet when invoking the REPL #1854

bjornregnell opened this issue Feb 13, 2023 · 4 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@bjornregnell
Copy link
Contributor

Version(s)
$ scala-cli version
Scala CLI version: 0.1.20
Scala version (default): 3.2.2

Describe the bug
Running a provided --script-snippet or --scala-snippet at the start of a repl session should work. (It works fine for run.)

To Reproduce

$ scala-cli repl --script-snippet 'println("hello")'
Compiling project (Scala 3.2.2, JVM)
Compiled project (Scala 3.2.2, JVM)
Welcome to Scala 3.2.2 (17.0.4.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                                                       
scala> 

Nothing is printed above, but it works for run:

$ scala-cli run --script-snippet 'println("hello")'
Compiling project (Scala 3.2.2, JVM)
Compiled project (Scala 3.2.2, JVM)
hello

Expected behaviour
The repl session should start by interpreting the snippet.

Use case: This can be used to e.g. import my own things to a repl session:

$ scala-cli repl . --script-snippet 'import mypack.*'
Welcome to Scala 3.2.2 (17.0.4.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                                                       
scala> MyThing  // should be visible here as mypack is imported by the snippet
@bjornregnell bjornregnell added the bug Something isn't working label Feb 13, 2023
@Gedochao
Copy link
Contributor

Okay, so --script-snippet doesn't work as you assume for the repl sub-command.
There is a separate ticket for actually running something at the beginning of repl being invoked: #604

However, as pointed out in #604 (comment), there is a workaround for Scala 3 in the export syntax.

So I believe you can achieve what you want like this:

▶ scala-cli --scala-snippet 'object ToExport { def hello: String = "Hello" }; export ToExport.*'
# Compiling project (Scala 3.2.2, JVM)
# Compiled project (Scala 3.2.2, JVM)
# Welcome to Scala 3.2.2 (17.0.5, Java OpenJDK 64-Bit Server VM).
# Type in expressions for evaluation. Or try :help. 
                                                                                                                                                      
scala> hello
# val res0: String = Hello

Now, --script-snippet actually does work correctly for the repl.
However, that doesn't mean it's run in the repl, but simply added to the classpath (consistently with how --scala-snippet and other -*-snippet options work).

▶ scala-cli repl --script-snippet 'println("Hello")'
Compiling project (Scala 3.2.2, JVM)
Compiled project (Scala 3.2.2, JVM)
Welcome to Scala 3.2.2 (17.0.5, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                                       
scala> snippet_sc.main(Array.empty)
Hello

@bjornregnell
Copy link
Contributor Author

bjornregnell commented Feb 13, 2023

Aha. Thanks. So I was not really understanding this from the docs...

The use case of allowing things to be visible from your top-level package etc when firing up the repl is quite nice to have.

Perhaps include ỳour nice workaround above in docs at least? But export does not work for packages, only objects...

@bjornregnell
Copy link
Contributor Author

So if it's a feature and not a bug then the docs should be updated and eagerly looking forward to #604 :)

@Gedochao Gedochao added duplicate This issue or pull request already exists enhancement New feature or request and removed bug Something isn't working labels Feb 14, 2023
@Gedochao Gedochao changed the title --script-snippet does not work for the repl sub command allow to run a snippet when invoking the REPL Feb 14, 2023
@Gedochao
Copy link
Contributor

I wonder if the export syntax shouldn't be expanded to cover exporting top level packages, rather than working around it with Scala CLI...
well, regardless how it'll be handled, let's track it under #604, I'm closing this as duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

2 participants