Dotty: Self types with underscore

Created on 10 Jun 2017  路  3Comments  路  Source: lampepfl/dotty

The following code compiles in scala 2.12.2:

Welcome to Scala 2.12.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_112).
Type in expressions for evaluation. Or try :help.
tr
scala> trait A
defined trait A

scala>

scala> trait B {_:A => }
defined trait B

But fails in dotty:

Welcome to Scala.next (pre-alpha, git-hash: 05aeeb5)  (OpenJDK 64-Bit Server VM, Java 1.8.0_112).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A
defined trait A
scala> trait B {_:A =>}
-- [E016] Syntax Error: <console>:1:9 ------------------------------------------
1 |trait B {_:A =>}
  |         ^^^
  |         unbound placeholder parameter; incorrect use of `_`

longer explanation available when compiling with `-explain`
rewriting tool

Most helpful comment

I agree that your version is preferable. So I don't care too much if this is explicitly disallowed.
But I think the error message could use some improvement.
Maybe explicitly mention it in the explanation part?


For reference this is the current explaination

dotr -explain
Welcome to Scala.next (pre-alpha, git-hash: 05aeeb5)  (OpenJDK 64-Bit Server VM, Java 1.8.0_121).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A
defined trait A
scala> trait B{ _: A => }
-- [E016] Syntax Error: <console>:1:9 ------------------------------------------
1 |trait B{ _: A => }
  |         ^^^^
  |         unbound placeholder parameter; incorrect use of `_`

Explanation
===========
The `_` placeholder syntax was used where it could not be bound.
Consider explicitly writing the variable binding.

This can be done by replacing `_` with a variable (eg. `x`)
and adding x => where applicable.

Example before:

{ _ }

Example after:

x => { x }

Another common occurrence for this error is defining a val with `_`:

val a = _

But this val definition isn't very useful, it can never be assigned
another value. And thus will always remain uninitialized.
Consider replacing the val with var:

var a = _

Note that this use of `_` is not placeholder syntax,
but an uninitialized var definition.
Only fields can be left uninitialized in this manner; local variables
must be initialized.

All 3 comments

I'd have a tendency to close this. There's a much more legible alternative which does the same thing:

trait B { this: A => ... }

Do we really need another use for the underscore?

I agree that your version is preferable. So I don't care too much if this is explicitly disallowed.
But I think the error message could use some improvement.
Maybe explicitly mention it in the explanation part?


For reference this is the current explaination

dotr -explain
Welcome to Scala.next (pre-alpha, git-hash: 05aeeb5)  (OpenJDK 64-Bit Server VM, Java 1.8.0_121).
Type in expressions to have them evaluated.
Type :help for more information.
scala> trait A
defined trait A
scala> trait B{ _: A => }
-- [E016] Syntax Error: <console>:1:9 ------------------------------------------
1 |trait B{ _: A => }
  |         ^^^^
  |         unbound placeholder parameter; incorrect use of `_`

Explanation
===========
The `_` placeholder syntax was used where it could not be bound.
Consider explicitly writing the variable binding.

This can be done by replacing `_` with a variable (eg. `x`)
and adding x => where applicable.

Example before:

{ _ }

Example after:

x => { x }

Another common occurrence for this error is defining a val with `_`:

val a = _

But this val definition isn't very useful, it can never be assigned
another value. And thus will always remain uninitialized.
Consider replacing the val with var:

var a = _

Note that this use of `_` is not placeholder syntax,
but an uninitialized var definition.
Only fields can be left uninitialized in this manner; local variables
must be initialized.

It looks like the UnboundPlaceholderParameter error is being issued incorrectly here. It should probably be its own instance of Message.

Was this page helpful?
0 / 5 - 0 ratings