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

Add a setting/API to run a code snippet/script in the REPL on startup #21242

Open
1 of 2 tasks
Gedochao opened this issue Jul 22, 2024 · 1 comment · Fixed by #22206
Open
1 of 2 tasks

Add a setting/API to run a code snippet/script in the REPL on startup #21242

Gedochao opened this issue Jul 22, 2024 · 1 comment · Fixed by #22206

Comments

@Gedochao
Copy link
Contributor

Gedochao commented Jul 22, 2024

What needs to be done

We need a way to pass a code snippet or Scala source file that'd be run on REPL startup.
This is very similar to Ammonite REPL's (https://github.com/com-lihaoyi/Ammonite or --amm passed to Scala CLI) -c/--code option.

Why do we need this?

Requirements

I'd imagine we need:

  • a way to pass a code snippet or source file to the REPL main class to be executed at REPL startup
    • something like this:
    java
    -Dscala.usejavacp=true
    -classpath (...)
    dotty.tools.repl.Main
    -classpath (...)
    --init-script 'println("Hello from the init script in REPL")'
    # Welcome to Scala 3.4.2 (17, Java OpenJDK 64-Bit Server VM).
    # Type in expressions for evaluation. Or try :help.
    # 
    # Hello from the init script in REPL
    # scala> 
  • a way to skip the interactive mode and execute the script inside of the REPL, similar to what the -e flag does outside of the REPL
    • this would make testing integrations a lot easier
    • Ammonite equivalent:
    scala-cli repl --power --amm --ammonite-arg -c --ammonite-arg 'println("Hello from the init script in REPL")'
    # Compiling ~/Downloads/(console)
    # Hello from the init script in REPL

Context

Past discussions:

noti0na1 added a commit that referenced this issue Jan 9, 2025
Partial fix #21242: Add REPL init script setting

Test with flag at REPL startup: 

```scala
> ./bin/scalaQ --repl-init-script 'println("Hello from init script!"); val i = 2 * 2'
[warning] MainGenericRunner class is deprecated since Scala 3.5.0, and Scala CLI features will not work.
[warning] Please be sure to update to the Scala CLI launcher to use the new features.
[warning] Check the Scala 3.5.0 release notes to troubleshoot your installation.
Hello from init script!
val i: Int = 4
Welcome to Scala 3.6.4-RC1-bin-SNAPSHOT-nonbootstrapped-git-5ea7c13 (17.0.12, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                            
scala> 

```

Test inside REPL:

```scala
sbt:scala3> repl
Welcome to Scala 3.6.4-RC1-bin-SNAPSHOT-nonbootstrapped-git-20e6f11 (17.0.12, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                            
scala> :reset --repl-init-script:'println("Hello from init script!")'
Resetting REPL state with the following settings:
  --repl-init-script:println("Hello from init script!")

Hello from init script!
                                                                                                                                            
scala> 
```

scala-cli can use this flag to passing init code to REPL.
@noti0na1 noti0na1 reopened this Jan 9, 2025
@noti0na1
Copy link
Member

noti0na1 commented Jan 9, 2025

The second point will be implemented in a separate PR.

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

Successfully merging a pull request may close this issue.

3 participants