Closures parsed as self-type annotation
See original GitHub issueI’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:
- Created 7 years ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I was also concerned on unbound, I’ll probably use “declaration” rather than “definition”.
Agreed, especially since the grammar already excludes this (which I missed) and the current parser is already nontrivial. I’ll close this as duplicate.