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.

Trait with type parameter does not specialize match result type

See original GitHub issue

Compiler version

3.0

Minimized code

sealed trait Ty {
  type T
}
case object TUnit extends Ty {
  type T = Unit
}
final case class TFun(dom: Ty, cod: Ty) extends Ty {
  type T = dom.T => cod.T
}

def default(ty: Ty): ty.T = ty match {
  case TUnit => ()
  case TFun(dom, cod) => { (x: dom.T) => default(cod) }
}

Output

Found:    Unit
Required: ty.T

Found:    dom.T => cod.T
Required: ty.T

Expectation

I expected the result type inside of the match to be specialized to Unit for TUnit and dom.T => cod.T for TFun, but this is not the case. It may be the case that I expect too much though.

With GADTs the result types are specialized, but I cannot type the x in the case of TFun, because I have no access to the type parameters of TFun:

sealed trait Ty[T]
case object TUnit extends Ty[Unit]
final case class TFun[A, B](dom: Ty[A], cod: Ty[B]) extends Ty[A => B]

def default[T](ty: Ty[T]): T = ty match {
  case TUnit => ()
  case TFun(dom, cod) => { x => default(cod) }
  /*
  Missing parameter type

  I could not infer the type of the parameter x.
  */
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
LPTKcommented, May 26, 2021

@liufengyun Cool, thanks for taking the time to look into this!

1reaction
liufengyuncommented, May 18, 2021

@liufengyun I seem to remember that this used to be different, wasn’t it? It would be nice if there was at least a special-case to allow refining what is matched to subtype the singleton type of the scrutinee, otherwise we lose too much information in type-heavy programming patterns.

I think it’s possible to do better here. I’ll give it a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specializing a method that takes type parameters
1 Answer 1 · This doesn't quite work because I'd like it to be possible for a single subclass to override the method...
Read more >
Templates - D Programming Language
If there is no type specialization for the parameter, the type of the parameter is set to the template argument. · If the...
Read more >
Generic associated types encode higher-order functions on ...
GATs allow type parameters to associated types in traits. This feature enables total type-level functions to be associated to structs.
Read more >
Implement a trait for another generic trait - Rust Users Forum
the type parameter 'TVoxel' is not constrained by the impl trait, self type, or predicates unconstrained type parameter. I have 2 questions :....
Read more >
0447-no-unused-impl-parameters - The Rust RFC Book
If the type parameters do not appear in the trait reference or self type, however, there is no basis on which to infer...
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