[Solved] Cannot use :paste in scala3-repl

Hi there, I am a big fan of scala and exciting about new scala 3. I already installed coursier and type this command $cs setup --jvm adopt:11 following this setup.

Then, I recognize that it is scala 2.13, so I use this command $cs install scala3-compiler and $cs install scala3-repl following dotty docs.

The issue is here, when I tried to use repl using $scala, the welcome prompt showed that is is scala version 2.13.6 which can use :paste just fine. But for repl scala 3 version using scala3-repl, it seems that I can’t use :paste as shown in the figure below. I wonder why?

Actually, I would like the default $scala command to access scala3 version repl like in this online book. How can I do this?

Thanks in advance everyone :smile:.

1 Like

:paste is not implemented but it’s also not really needed because multi-line pasting is supported by the scala 3 REPL.

2 Likes

Oh, Thanks a bunch!

Note that :paste isn’t that useful in Scala 2 anymore either. Since 2.13.2 the Scala 2 REPL has supported multiline paste as well.

However they don’t support newline without committing, as far as I know.

1 Like

They do. On MacOS it’s option-return, not sure what modifier key it is on Windows or Linux.

1 Like

That doesn’t seem to work by default though.

Probably keyboard - Using the Option key properly on Mac terminal - Super User ?

1 Like

Aha, configuring “left option key” as Esc+ in iTerm2 does the trick.

Eventually you could use a block definition
{
trait A
case object B extends A
}

Came here looking for paste mode: multi-line doesn’t seem to work properly.
Tried pasting in

 class Foo:

  val x = 123

  def bar = ???

  def baz = ??? 

and get

scala> class Foo:
     | 
     |   val x = 123
     | 
// defined class Foo

scala>   def bar = ???
def bar: Nothing

scala> 

scala>   def baz = ???

Multiline paste is not same as :paste . For example, how do we get companion object. People who have removed :paste, really have not used scala REPL. Poor replacement is using block

For example, how do we get companion object

Pasting code that includes a companion object works fine as far as I can tell. If it isn’t working for you, please be specific about what you’re doing and how it’s failing.

Here is a session log showing it working:

% scala3
Welcome to Scala 3.1.0 (1.8.0_292, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                        
scala> class C
     | object C
     | 
// defined class C
// defined object C
                                                                                                                        
scala> 

@luigip’s example also works for me in Scala 3.1.0.

Do we have two Scala 3 REPL? Because in my case, your snippet does not work (Logically should not work as well because why would REPL wait for next line when ‘class A’ has ended as per REPL. Also @luigip 's example is valid one as that also does not work)

Welcome to Scala 3.1.0 (1.8.0_65, Java Java HotSpot(TM) 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> class A
// defined class A

scala> class A
// defined class A

scala> object A
// defined object A

scala> class A{
     | };object A
// defined class A
// defined object A

scala> class A{
     | }
// defined class A

scala>

I’m at a loss to account for why this works for me and doesn’t work for you. But your experience apparently isn’t common, or we’d have had a ton of complaints about it.

I’m on macOS, for whatever it’s worth. Sometimes there are JLine peculiarities that are Windows-specific. Are you on Windows, perhaps?

Yes, Sir, you are right. I am at Windows and at ‘sbt console’ . Looks like then, we have a issue here in Windows. BTW, I am curious, how does scala REPL understand when to end user input in mac? If newline is not the end for logical block (eg in this example ‘class A’), then what is the logical end for mac scala repl? Is it one empty line?

Let me help you out here. I think @SethTisue is talking about actually pasting code into the REPL and @ndas1971 about typing code into the REPL but wanting multiple lines to be interpreted together.

You can enter multiple lines by using “soft return”. My best guess is that that would be Shift+Enter on Windows. There’s also a slight chance you have to configure something in your terminal application for that to work (like I had to do).

1 Like

Thanks for the Help, it is Esc+Enter in Windows. Could find out with all key combinations as google search only talks about Office products’ soft return.

1 Like

Scala 3 began as a fork and the REPL was written from scratch, so features need to be ported. :javap anyone?

Here is an alternative universe paste not working as expected under WSL. This is a “paste”.

Welcome to Scala 3.1.2-RC1-bin-SNAPSHOT-git-5ce25fe (17, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> class C
// defined class C

scala> object C:
     |   def apply(i: Int) = new C()

Showing that is also doesn’t work under Scala 2, who is nice enough to complain.

Welcome to Scala 2.13.7 (OpenJDK 64-Bit Server VM, Java 17).
Type in expressions for evaluation. Or try :help.

scala> class C
class C

scala> object C {
     |   def apply(i: Int) = new C()
     | }
object C
warning: previously defined class C is not a companion to object C.
Companions must be defined together; you may wish to use :paste mode for this.