Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Scala3] Unable to define extension methods in REPL #1281

Closed
tanishiking opened this issue Aug 16, 2022 · 1 comment · Fixed by #1282
Closed

[Scala3] Unable to define extension methods in REPL #1281

tanishiking opened this issue Aug 16, 2022 · 1 comment · Fixed by #1282

Comments

@tanishiking
Copy link
Contributor

❯ scala-cli repl --scala 3.1.3 --ammonite
Loading...
Welcome to the Ammonite Repl 2.5.4-11-4f5bf2aa (Scala 3.1.3 Java 17.0.3)
@ extension (foo: Int)
    def bar = foo

-- [E006] Not Found Error: cmd0.sc:1:11 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |           ^^^^^^^^^
  |           Not found: extension
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:1:22 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |                      ^^^
  |                      Not found: foo
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:2:12 ----------------------------------------
2 |  def bar = foo
  |            ^^^
  |            Not found: foo
  |
  | longer explanation available when compiling with `-explain`
Compilation Failed

It would be nice if we can define extension methods as normal REPL can do that

❯ scala-cli repl --scala 3.1.3
Welcome to Scala 3.1.3 (17.0.3, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> extension (foo: Int)
     |   def bar = foo
     |
def bar(foo: Int): Int

scala> 1.bar
val res0: Int = 1

It looks like the problem is, the preprocessor for Scala3 doesn't expect to have the extension keyword here

private val decls = Seq[(String, String, untpd.Tree) => Option[Expanded]](
ObjectDef, ClassDef, TraitDef, DefDef, TypeDef, VarDef, PatDef, Import, Expr
)
right?

I'm new to the Ammonite codebase, but I'm willing to contribute to this :)

@tanishiking tanishiking changed the title Unable to define extension methods in REPL [Scala3] Unable to define extension methods in REPL Aug 16, 2022
@alexarchambault
Copy link
Collaborator

It looks like the problem is, the preprocessor for Scala3 doesn't expect to have the extension keyword here , right?
I'm new to the Ammonite codebase, but I'm willing to contribute to this :)

@tanishiking I think that's the right part of the codebase that needs tweaking. Feel free to fix it, yes!

tanishiking added a commit to tanishiking/Ammonite that referenced this issue Aug 16, 2022
fix: com-lihaoyi#1281

Previously, ammonite REPL couldn't define extension methods
because of the lack of Preprocessor for extension method definition.

```
❯ scala-cli repl --scala 3.1.3 --ammonite
Loading...
Welcome to the Ammonite Repl 2.5.4-11-4f5bf2aa (Scala 3.1.3 Java 17.0.3)
@ extension (foo: Int)
    def bar = foo

-- [E006] Not Found Error: cmd0.sc:1:11 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |           ^^^^^^^^^
  |           Not found: extension
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:1:22 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |                      ^^^
  |                      Not found: foo
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:2:12 ----------------------------------------
2 |  def bar = foo
  |            ^^^
  |            Not found: foo
  |
  | longer explanation available when compiling with `-explain`
Compilation Failed
```

Now, this is possible to define extension methods in Ammonite REPL.

```
Welcome to the Ammonite Repl 2.5.4-14-dc4c47bc (Scala 3.1.3 Java 1.8.0_302)
@ extension (foo: Int)
    def x = 1
    def y = 1

defined extension methods

@ 100.x
res1: Int = 1
```
tanishiking added a commit to tanishiking/Ammonite that referenced this issue Aug 18, 2022
fix: com-lihaoyi#1281

Previously, ammonite REPL couldn't define extension methods
because of the lack of Preprocessor for extension method definition.

```
❯ scala-cli repl --scala 3.1.3 --ammonite
Loading...
Welcome to the Ammonite Repl 2.5.4-11-4f5bf2aa (Scala 3.1.3 Java 17.0.3)
@ extension (foo: Int)
    def bar = foo

-- [E006] Not Found Error: cmd0.sc:1:11 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |           ^^^^^^^^^
  |           Not found: extension
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:1:22 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |                      ^^^
  |                      Not found: foo
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:2:12 ----------------------------------------
2 |  def bar = foo
  |            ^^^
  |            Not found: foo
  |
  | longer explanation available when compiling with `-explain`
Compilation Failed
```

Now, this is possible to define extension methods in Ammonite REPL.

```
Welcome to the Ammonite Repl 2.5.4-14-dc4c47bc (Scala 3.1.3 Java 1.8.0_302)
@ extension (foo: Int)
    def x = 1
    def y = 1

defined extension methods

@ 100.x
res1: Int = 1
```
alexarchambault pushed a commit that referenced this issue Aug 18, 2022
fix: #1281

Previously, ammonite REPL couldn't define extension methods
because of the lack of Preprocessor for extension method definition.

```
❯ scala-cli repl --scala 3.1.3 --ammonite
Loading...
Welcome to the Ammonite Repl 2.5.4-11-4f5bf2aa (Scala 3.1.3 Java 17.0.3)
@ extension (foo: Int)
    def bar = foo

-- [E006] Not Found Error: cmd0.sc:1:11 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |           ^^^^^^^^^
  |           Not found: extension
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:1:22 ----------------------------------------
1 |val res0 = extension (foo: Int)
  |                      ^^^
  |                      Not found: foo
  |
  | longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: cmd0.sc:2:12 ----------------------------------------
2 |  def bar = foo
  |            ^^^
  |            Not found: foo
  |
  | longer explanation available when compiling with `-explain`
Compilation Failed
```

Now, this is possible to define extension methods in Ammonite REPL.

```
Welcome to the Ammonite Repl 2.5.4-14-dc4c47bc (Scala 3.1.3 Java 1.8.0_302)
@ extension (foo: Int)
    def x = 1
    def y = 1

defined extension methods

@ 100.x
res1: Int = 1
```
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants