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

scala -e not executing like in a REPL #11859

Open
dwijnand opened this issue Jan 22, 2020 · 7 comments
Open

scala -e not executing like in a REPL #11859

dwijnand opened this issue Jan 22, 2020 · 7 comments
Labels
Milestone

Comments

@dwijnand
Copy link
Member

@dwijnand dwijnand commented Jan 22, 2020

The usage text (--help) specifies:

 -e <string>  execute <string> as if entered in the REPL

However, that's not always true:

15:41:12 $ qscala -Yrepl-use-magic-imports -Xprint:typer -e 'class C'
[[syntax trees at end of                     typer]] // scalacmd2321235821301307669.scala
package <empty> {
  class C extends scala.AnyRef {
    def <init>(): C = {
      C.super.<init>();
      ()
    }
  }
}
15:41:22 $ qscala -Yrepl-use-magic-imports -Xprint:typer
Welcome to Scala 2.13.2-20200122-145334-329fe56 (OpenJDK 64-Bit Server VM, Java 11.0.1).
Type in expressions for evaluation. Or try :help.

scala> class C
[[syntax trees at end of                     typer]] // <console>
package $line3 {
  object $read extends _root_.scala.AnyRef {
    def <init>(): type = {
      $read.super.<init>();
      ()
    };
    object $iw extends scala.AnyRef {
      def <init>(): type = {
        $iw.super.<init>();
        ()
      };
      class C extends scala.AnyRef {
        def <init>(): C = {
          C.super.<init>();
          ()
        }
      }
    }
  }
}

class C

As a side note, funnily when running with -debug, to see how java is invoked in the scala runner, the -e argument gets processed by echo so there's just a blank line where -e should be:

15:42:02 $ qscala -debug -Yrepl-use-magic-imports -Xprint:typer -e 'class C'

java
  # blah blah blah...
scala.tools.nsc.MainGenericRunner
-Yrepl-use-magic-imports
-Xprint:typer

class C
@dwijnand dwijnand added the repl label Jan 22, 2020
@dwijnand

This comment has been minimized.

Copy link
Member Author

@dwijnand dwijnand commented Jan 22, 2020

Btw, the workaround in bash is to use <<<:

15:46:23 $ qscala -Yrepl-use-magic-imports -Xprint:typer <<< 'class C'
Welcome to Scala 2.13.2-20200122-145334-329fe56 (OpenJDK 64-Bit Server VM, Java 11.0.1).
Type in expressions for evaluation. Or try :help.

scala> class C
[[syntax trees at end of                     typer]] // <console>
package $line3 {
  object $read extends _root_.scala.AnyRef {
    def <init>(): type = {
      $read.super.<init>();
      ()
    };
    object $iw extends scala.AnyRef {
      def <init>(): type = {
        $iw.super.<init>();
        ()
      };
      class C extends scala.AnyRef {
        def <init>(): C = {
          C.super.<init>();
          ()
        }
      }
    }
  }
}

class C

scala> :quit

15:46:37 $

(the :quit was automatic, because the stdin was drained)

@som-snytt

This comment has been minimized.

Copy link

@som-snytt som-snytt commented Jan 22, 2020

See scala/scala#5872

➜  ~ scala -Vprint:typer -e "class C"
[[syntax trees at end of                     typer]] // scalacmd4041926974562844504.scala
package <empty> {
  class C extends scala.AnyRef {
    def <init>(): C = {
      C.super.<init>();
      ()
    }
  }
}

➜  ~ scala -howtorun:repl -Vprint:typer -e "class C"
[[syntax trees at end of                     typer]] // <console>
package $line3 {
  object $read extends _root_.scala.AnyRef {
    def <init>(): $line3.$read.type = {
      $read.super.<init>();
      ()
    };
    object $iw extends scala.AnyRef {
      def <init>(): type = {
        $iw.super.<init>();
        ()
      };
      object $iw extends scala.AnyRef {
        def <init>(): type = {
          $iw.super.<init>();
          ()
        };
        class C extends scala.AnyRef {
          def <init>(): C = {
            C.super.<init>();
            ()
          }
        }
      }
    }
  }
}

defined class C

cf the obviously scripted:

➜  ~ scala -Vprint:typer -e "val n = 42"
[[syntax trees at end of                     typer]] // scalacmd7299894770982359480.scala
package <empty> {
  object Main extends scala.AnyRef {
    def <init>(): Main.type = {
      Main.super.<init>();
      ()
    };
    def main(args: Array[String]): Unit = {
      final class $anon extends scala.AnyRef {
        def <init>(): <$anon: AnyRef> = {
          $anon.super.<init>();
          ()
        };
        private[this] val n: Int = 42;
        <stable> <accessor> private def n: Int = $anon.this.n
      };
      {
        new $anon();
        ()
      }
    }
  }
}

➜  ~ scala -Vprint:typer -e "val n = 42" -howtorun:repl
[[syntax trees at end of                     typer]] // <console>
package $line3 {
  object $read extends _root_.scala.AnyRef {
    def <init>(): $line3.$read.type = {
      $read.super.<init>();
      ()
    };
    object $iw extends scala.AnyRef {
      def <init>(): type = {
        $iw.super.<init>();
        ()
      };
      object $iw extends scala.AnyRef {
        def <init>(): type = {
          $iw.super.<init>();
          ()
        };
        private[this] val n: Int = 42;
        <stable> <accessor> def n: Int = $iw.this.n
      }
    }
  }
}

n: Int = 42
@som-snytt

This comment has been minimized.

Copy link

@som-snytt som-snytt commented Jan 22, 2020

-howtorun:repl didn't make it onto the help page.

@dwijnand

This comment has been minimized.

Copy link
Member Author

@dwijnand dwijnand commented Jan 23, 2020

Why isn't it the default behaviour of -e? I guess we can just change the description of -e to something like "code to run", without claiming anything about the repl?

@SethTisue

This comment has been minimized.

Copy link
Member

@SethTisue SethTisue commented Jan 28, 2020

I would have expected it to be like the script runner rather than like the REPL.

@dwijnand

This comment has been minimized.

Copy link
Member Author

@dwijnand dwijnand commented Jan 28, 2020

Would everyone agree to just amending the text, then?

- -e <string>  execute <string> as if entered in the REPL
+ -e <string>  execute <string> as if it were in a source file

Suggestions welcome.

@som-snytt

This comment has been minimized.

Copy link

@som-snytt som-snytt commented Jan 28, 2020

I'm afraid that if I ever run scala -help, the glare of the sun off the polished text will blind me.

@lrytz lrytz added this to the Backlog milestone Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.