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.

`Nothing` causes type lambda to be inferred in case of bounded type parameter

See original GitHub issue

Compiler 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:open
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
smartercommented, Mar 8, 2022

Yes, it seems like that should be doable.

1reaction
smartercommented, Mar 8, 2022

type Pure[A] <: Nothing

Just type Pure[A] = Nothing works too, I believe using <: was only needed to work around Scala 2’s “fear of Nothing”.

Maybe Nothing should be equivalent to [x] =>> 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.

Read more comments on GitHub >

github_iconTop 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 >

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