question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opaque types are not usable in the REPL under Java 9+

See original GitHub issue

Compiler version

3.0.0-RC1

Minimized code

➜  scala3-repl
scala> opaque type Foo = Int
1 |opaque type Foo = Int
  |^^^^^^
  |Illegal start of statement: no modifiers allowed here

scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> import Module.*

scala> def bar(f: Foo) = println(f)
1 |def bar(f: Foo) = println(f)
  |           ^^^
  |           Not found: type Foo

scala> def bar(f: Module.Foo) = println(f)
1 |def bar(f: Module.Foo) = println(f)
  |           ^^^^^^^^^^
  |           type Foo is not a member of object Module

scala> :reset

scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |   def bar(f: Foo) = println(f)
     |   def baz = bar(42)
     |
// defined object Module

Expectation

The top-level definition bug is tracked here: https://github.com/lampepfl/dotty/issues/9879

Not sure what’s going on with (1) inability to reference an opaque type defined in an object and (2) lack of opaque type enforcement in the last Module definition. Feels like something about the REPL encoding scheme is at odds with the opaque type implementation scheme.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mpilquistcommented, Mar 24, 2021

Aha! It works for me when running RC1 REPL under Java 8 and fails on Java 9+

➜  java -version
openjdk version "9"
OpenJDK Runtime Environment (build 9+181)
OpenJDK 64-Bit Server VM (build 9+181, mixed mode)
➜  scala3-repl
scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> def bar(f: Module.Foo) = println(f)
1 |def bar(f: Module.Foo) = println(f)
  |           ^^^^^^^^^^
  |           type Foo is not a member of object Module

scala>
➜  setjdk 1.8
➜  java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.272-b10, mixed mode)
➜  scala3-repl
scala> object Module:
     |   opaque type Foo = Int
     |   object Foo:
     |     def fromInt(i: Int): Foo = i
     |
// defined object Module

scala> def bar(f: Module.Foo) = println(f)
def bar(f: Module.Foo): Unit

scala>
0reactions
mpilquistcommented, Mar 25, 2021

Wow, great find. I almost never use the word Module in Scala and just happened to do so here to work around #9879.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Definitions in `java.lang._` shadow definitions in REPL · Issue ...
The current error messages are also not helpful to understand that those definitions are ... Opaque types are not usable in the REPL...
Read more >
lampepfl/dotty - Gitter
When I try to use the library as it is in a Scala 2.13 project (org:lib_3.0.0-RC1:version) , it is not able to see...
Read more >
Opaque Types — The Swift Programming Language (Swift 5.7)
A function or method with an opaque return type hides its return value's type information. Instead of providing a concrete type as the...
Read more >
Algebras of opaque types and pattern matching
But there is a problem as we wish to design a developer friendly library, in that opaque types don't allow pattern matching. we...
Read more >
6 Objects, Collections, and OPAQUE Types - Oracle Help Center
This chapter discusses how Oracle SQLJ supports user-defined SQL types--namely objects (and related object references) and collections (variable arrays and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found