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.

Closures parsed as self-type annotation

See original GitHub issue

I’m not sure I care and I can’t conceive a use case (at least, not without involving some weird macro), but the following is valid for scalac and rejected (at parse-time) by dotty:

class Foo {
  (a: Int, b: String) => a
}

The reason is that dotty parses (what looks like) an expression, then sees => and “reinterprets” the parsed expression as a self-type annotation.

I’m not so concerned about the feature, I’m more concerned about aligning the grammar with the implementation.

I noticed this while trying to document errors for self-type annotations.

So this gives something like:

> run -explain tests/Foo.scala
[...]
[info] Running dotty.tools.dotc.Main -explain tests/Foo.scala
-- Error: tests/Foo.scala ----------------------------------------------------------------------------------------------
2 |  (a: Int, b: String) => a
  |  ^^^^^^^^^^^^^^^^^^^
  |  not a legal self type clause
-- [E006] Missing Identifier Error: tests/Foo.scala --------------------------------------------------------------------
2 |  (a: Int, b: String) => a
  |                         ^
  |                         not found: a

Explanation
===========
An identifier for `a` is missing. This means that something
has either been misspelt or you're forgetting an import


two errors found
java.lang.RuntimeException: Nonzero exit code returned from runner: 1

BTW I don’t like “missing identifier”, I think it’s trying to say “unbound identifier” (I have a local patch I’ll send at some point).

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
oderskycommented, Nov 9, 2016

I think we should not try to overengineer fixing this. The scalac code doing this is quite impenetrable. I think “not a legal self type clause” is perfectly fine as an error message.

0reactions
Blaisorbladecommented, Nov 9, 2016

BTW I don’t like “missing identifier”, I think it’s trying to say “unbound identifier” (I have a local patch I’ll send at some point).

I don’t like that either. But “unbound” is probably too academic for some. How about

No definition for a was found. ?

I was also concerned on unbound, I’ll probably use “declaration” rather than “definition”.

I think we should not try to overengineer fixing this. The scalac code doing this is quite impenetrable. I think “not a legal self type clause” is perfectly fine as an error message.

Agreed, especially since the grammar already excludes this (which I missed) and the current parser is already nontrivial. I’ll close this as duplicate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I type hint a method with the type of the enclosing class?
Basically, this typing hinting tells the type checker that the return type of __add__() and copy() are the same type as self ....
Read more >
Parse error on type-hints · Issue #1461 · psf/black - GitHub
Describe the bug Black fails to parse a valid python3.6 file due to a ... return "distracting type annotation" def a_method(self): """ Some ......
Read more >
Receiver Closures - Pitches - Swift Forums
Receiver Closures Hello, I would like to get some feedback on a pitch idea. Idea allow specifying the receiver of a closure.
Read more >
Type inference in closures - help - Rust Users Forum
I am trying to create a trait to accept some sort of callback API but I am running into a problem that Rust...
Read more >
0968-closure-return-type-syntax - The Rust RFC Book
As a simple fix, this RFC proposes that if a return type annotation is supplied, the body must be enclosed in braces: ||...
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