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.

Methods with pattern-matching and dependent types

See original GitHub issue

Given:

  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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
julienrfcommented, Dec 19, 2019

I think we should re-open this issue, especially because the “little hacky workaround” doesn’t work anymore: https://scastie.scala-lang.org/l21lZJfsThiUMJlBeSuSsg

2reactions
abgruszeckicommented, Dec 25, 2019

I’ll be looking into extending GADT constraints to also work on singleton types. I’ll keep this issue on my radar.

Read more comments on GitHub >

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

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