Trait with type parameter does not specialize match result type
See original GitHub issueCompiler 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:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top 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 >
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 Free
Top 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
@liufengyun Cool, thanks for taking the time to look into this!
I think it’s possible to do better here. I’ll give it a try.