`Nothing` causes type lambda to be inferred in case of bounded type parameter
See original GitHub issueCompiler version
3.1.1
Minimized code
//> using scala "3.1.1"
trait Foo[+F[_], +A]:
def flatMap[F2[x] >: F[x], B](f: A => Foo[F2, B]): Foo[F2, B]
def f(fa: Foo[Nothing, Int]): Foo[Nothing, Int] =
fa.flatMap(_ => fa)
Output
[error] ./p.scala:6:3: Found: Foo[[x] =>> Nothing, Int]
[error] Required: Foo[Nothing, Int]
[error] fa.flatMap(_ => fa)
[error] ^^^^^^^^^^^^^^^^^^^
Expectation
Nothing
to be inferred- Maybe
Nothing
should be equivalent to[x] =>> Nothing
?
Note if we use the trick from fs2 to define a special alias for Nothing
, then both of those expectations are met:
type Pure[A] <: Nothing
def g(fa: Foo[Pure, Int]): Foo[Pure, Int] =
fa.flatMap(_ => fa)
def h(fa: Foo[Pure, Int]) = // inferred as Foo[[x] =>> Pure, Int]
fa.flatMap(_ => fa)
def i(fa: Foo[Pure, Int]): Foo[Pure, Int] =
h(fa)
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
java - Why is this type inference not working with this Lambda ...
This allows the deferred-instantiation to infer that the type parameter should be Boolean instead of Object (see instantiated signature below).
Read more >Bounded Type Parameters - The Java™ Tutorials
This is what bounded type parameters are for. To declare a bounded type parameter, list the type parameter's name, followed by the extends...
Read more >PEP 695 – Type Parameter Syntax
Type parameter symbols defined in outer scopes cannot be bound with nonlocal statements in inner scopes. The lexical scope introduced by the ...
Read more >generics - Name of technique for inferring type arguments of a ...
Then we have an interface Iterable which has one method which will return an Iterator . // T has an upper bound of...
Read more >Type inference - Kotlin language specification
After the inference of statements inside the lambda is complete, these postponed type variables are inferred using an additional type inference step, which ......
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
Yes, it seems like that should be doable.
Just
type Pure[A] = Nothing
works too, I believe using <: was only needed to work around Scala 2’s “fear of Nothing”.I’d be wary of doing that, it means we could end up forging a term whose type is a type lambda e.g.
val x = ??? : [X] =>> Nothing
, which is likely to break tons of assumptions in the compiler.