scala/scala3

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

Open

#13.416 geöffnet am 29. Aug. 2021

Auf GitHub ansehen
 (11 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)Scala (1.159 Forks)batch import
area:match-typeshelp wanteditype:enhancement

Repository-Metriken

Stars
 (6.247 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 18T 14h) (133 gemergte PRs in 30 T)

Beschreibung

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

Contributor Guide