Methods with pattern-matching and dependent types
See original GitHub issueGiven:
sealed trait F { type A }
case object FI extends F { type A = Int }
case object FS extends F { type A = String }
This snippet:
def test1a(f: F): f.A =
f match {
case FI => 1
case _: FS.type => ""
}
fails with:
-- [E007] Type Mismatch Error: <console>:9:19 ----------------------------------
9 | case FI => 1
| ^
| found: Int(1)
| required: f.A
-- [E007] Type Mismatch Error: <console>:10:27 ---------------------------------
10 | case _: FS.type => ""
| ^^
| found: String("")
| required: f.A
But I can prove it works with a little hacky workaround:
def test1b(f: F): f.A = {
def dirtyHack[B](f: F { type A = B }): B =
f match {
case FI => 1
case _: FS.type => ""
}
dirtyHack[f.A](f)
}
Notice that the f match { ... }
is the original implementation verbatim.
It would be great if the first snippet worked.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Pattern Matching with Dependent Types
This note deals with notation in type theory. The de nition of a function by pattern matching is by now common, and quite...
Read more >MoreDep - Adam Chlipala
A dependent pattern match is a match expression where the type of the overall match is a function of the value and/or the...
Read more >Eliminating Dependent Pattern Matching - Google Research
Abstract. This paper gives a reduction-preserving translation from Co- quand's dependent pattern matching [4] into a traditional type theory [11].
Read more >Equations: A Dependent Pattern-Matching Compiler - Inria
Internal vs. external approaches There exist two main approaches to adding dependent pattern matching to a dependent type theory. One is to bake...
Read more >Dependently Typed Pattern Matching
Abstract: The mechanism for declaring datatypes to model data structures in pro- gramming languages such as Standard ML and Haskell can offer both ......
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
I think we should re-open this issue, especially because the “little hacky workaround” doesn’t work anymore: https://scastie.scala-lang.org/l21lZJfsThiUMJlBeSuSsg
I’ll be looking into extending GADT constraints to also work on singleton types. I’ll keep this issue on my radar.