-
Notifications
You must be signed in to change notification settings - Fork 1.1k
_ => _
should be a valid way to write Function1[_, _]
#1396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note: I'm thinking that we could leave this one open for a bit and others like it for new contributors in the spirit of https://starters.servo.org/ |
It looks like you cannot have existentials within infix types. E.g. this won't work either: val x: Int Either _ = Left(10) |
I suppose this is caused by an unrelated issue, but when I run the initial example using
|
I've investigated some more, and I believe that at the core of this problem (and also of #1403) is an incomplete attempt of rejecting unbound wildcard types at the grammar level. val f: (_) => (_) // compiles
val a: (_) = 1 // #1403. compiler hangs, then cryptic error message/oom
def wut(x: (_)): Int = 1 // compiles
wut(???) // compiler hangs, then oom I haven't yet tried to figure out whether there is a good way to fix the grammar to prevent this. It does seem like it may require some amount of contextual knowledge, so it might be more appropriate to do this in a mini phase. Would be interesting to find out how scalac solves this. (Out of curiosity, is there a reason that makes unbound wildcard types inherently unsound, or do we just prevent them because they are pointless/annoying? While they are largely useless and currently break dotty in various ways, it does seem like they could have meaningful semantics. There also exists (somewhat) legitimate Scala code that will give you values that are essentially wildcard typed, e.g. |
Nice analysis!
The expression
I don't think there's anything unsound about unbound wildcards, in Scala 2 you can write
As far as I can tell this is all done in the parser: https://github.com/scala/scala/blob/20dd825ec6b5d4015ce36cf4373ba1c1d917da94/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala#L463-L521 |
Something like this might actually fix the grammar, but it makes
For the tuple case in
Edit: Hmm, things like List[(_)] wouldn't work anymore though. Will have to give this some more thought. |
I think what |
Yes, that sounds much cleaner. I'll start working on a patch. |
I have a basic prototype now. Is there any documentation for the tests? Specifically, I would like to know where should I put my test files, and how to check that some code will result in a specific compile error. I also found another amusing edge case. The following compiles in scalac (with type val lolwut: List[_ <: _ <: _] = List(10) Do we want to mimic this behavior (i.e. allow wildcards in type bounds), or should we just reject this from now on? (scalac does already reject constructs like |
@cswinter There are three main test directories:
Hope this helps. By the way, the curried lambda problem you noted on gitter should be fixed in #1409. |
I lean towards reject. In: val lolwut: List[_ <: _ <: _] = List(10) I would classify bound types as top-level types, which cannot be wildcards. |
My code is essentially ready for review now, there is just one remaining issue. When you compile a program that contains unbound wildcard types (e.g. my tests), the parser emits the expected syntax errors (using I'm not quite sure about the desired behaviour here. Do we want to modify later compiler stages to cope with unbound wildcard types? Or is there a way to issue a "hard" syntax error and just abort? One hack that I've tried and which seems to work pretty well is returning |
@cswinter: I suggest you first make a PR with just your fix and a neg test that demonstrates that the code does not compile, if the compiler just takes more time to finish that test than expected, that's OK, we already have a few tests like this, and we need a general solution to that problem (the long time spent in implicit search that I mentioned above) |
Also you might have seen this already but I just realized that our parser has a |
So the problem is that the tests don't even pass because the compiler crashes with an assertion error. What I can do is comment out the tests and create a new issue to track that problem separately, but it might make more sense to wait for a fix/include it with this changeset. I don't think we should try to reuse |
Uh oh!
There was an error while loading. Please reload this page.
This works in scalac but not in dotty:
Real-world usage: https://github.com/functional-streams-for-scala/fs2/blob/series/0.9/core/src/main/scala/fs2/Chunk.scala#L321
The text was updated successfully, but these errors were encountered: