Match type syntax does not allow writing type patterns for type members directly
See original GitHub issueMinimized code
trait RDF {
type Triple
}
object RDF {
type Triple[R <: RDF] = R match {
case (RDF { type Triple = t }) => t
}
type GetTriple[T] = RDF { type Triple = T }
}
Output
Not found: type t
Expectation
Expected to be able to extract a variable type from inside a type refinement.
This syntax limit can be worked around by introducing a “pattern type”:
trait RDF {
type Triple
}
object RDF {
type Triple[R <: RDF] = R match {
case GetTriple[t] => t
}
type GetTriple[T] = RDF { type Triple = T }
}
Why write something like that? Apparently this pattern of extracting a field using a match type can substitute for some usages of generalized type projections and is enough to port some of the libraries using type projections, e.g. https://github.com/bblfish/banana-play
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Pattern matching and type safety in TypeScript
We can now simulate pattern matching with a switch statement. The function allows us to match on a tag of a value of...
Read more >Type declarations and pattern matching
First of all we present pattern matching over the predefined types, and then go on to describe the various ways to declare structured...
Read more >Match Types - Scala 3 - EPFL
Conditional types only reduce if both the scrutinee and pattern are ground, whereas match types also work for type parameters and abstract types....
Read more >OCaml for the Skeptical: Pattern Matching
The pattern-matching part of the match is a sequence of clauses, each one of the form: pattern -> expr, separated by vertical bars...
Read more >Pattern Matching in C# 8.0
Finally, I have the tuple pattern, which leverages the positional pattern and allows me to compose a tuple on which to run my...
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
@bblfish you can recover the sub typing with intersection types and recursion:
see the example here https://scastie.scala-lang.org/lvxYeEJqT0e8t6QPZMbriA
@OlivierBlanvillain
As I see it, the bigger issue is that the lack of syntax makes it seem as if it’s impossible to do this in the first place.
In case of value matches, extracting a type member into a variable is redundant, since you could instead refer to it with a selection
The equivalent ability for types has been removed in Scala 3 😉