3

I notice when declaring a type alias in the REPL, a line break causes a statement to succeed:

this works:

scala> type a
     | 3 == 3
defined type alias a
res32: Boolean = true

This does not:

scala> type a 3 == 3              ^
       error: `=`, `>:`, or `<:` expected
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Rob
  • 3,333
  • 5
  • 28
  • 71

1 Answers1

6

It's legal to have an abstract type as shown.

The REPL is in a special parsing mode where it knows if the current line is a complete syntax production or if more input is required, so that's why it goes to a second line, even though technically it should just accept type a.

scala> object X { type a }
defined object X

scala> object X { def x: Int }
                      ^
       error: only traits and abstract classes can have declared but undefined members

scala> type a
     | 42
defined type alias a
res0: Int = 42

scala> def x: Int
     | 42
           ^
       error: only traits and abstract classes can have declared but undefined members

The REPL doesn't import the abstract typedef into the current expression because it knows it is abstract.

The type maybe not be very useful, but it is a thing:

scala> object X { type a }
defined object X

scala> val x: X.a = null.asInstanceOf[X.a]
x: X.a = null

scala> def x: X.a = ???
x: X.a

scala> def x: X.a = null
                    ^
       error: type mismatch;
        found   : Null(null)
        required: X.a
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • I tried using `:paste` to get around the REPL special way of parsing. Funnily enough, typing only `type a` results in `The pasted code is incomplete! ...but compilation found no error? Good luck with that.` which, though funny, does seem like an unhandled edge-case :) (note: I only have access to a 2.11.12 REPL ATM). **EDIT**: not that that contradicts/invalidates your answer in any way. – Marth Feb 25 '20 at 20:34
  • @Marth oh man, they changed it! `No error found in incomplete source.` – som-snytt Feb 25 '20 at 20:41
  • 1
    @Marth sorry, I changed the text. I also deleted the excellent punning comment, `// Remembrance of Things Pasted`, if you're a fan of Proust. – som-snytt Feb 25 '20 at 21:33