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.

Match type syntax does not allow writing type patterns for type members directly

See original GitHub issue

Minimized 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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
bishaboshacommented, Oct 24, 2022

@bblfish you can recover the sub typing with intersection types and recursion:

type URI[R <: RDF] <: Node[R] = R match 
  case GetURI[u] => u & Node[R]

see the example here https://scastie.scala-lang.org/lvxYeEJqT0e8t6QPZMbriA

2reactions
neko-kaicommented, Sep 27, 2021

@OlivierBlanvillain

I think having to define a type alias here isn’t the end of the world.

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.

Scala already has many oddities with types patterns, match types naturally inherits some of them:

In case of value matches, extracting a type member into a variable is redundant, since you could instead refer to it with a selection

new {} match { case t: { type T } => summon[t.T] }

The equivalent ability for types has been removed in Scala 3 😉

Read more comments on GitHub >

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

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