# Algebras of opaque types and pattern matching

**URL:** <https://contributors.scala-lang.org/t/algebras-of-opaque-types-and-pattern-matching/5245>\
**Category:** Language Design\
**Created:** [August 31, 2021, 2:07pm UTC](https://contributors.scala-lang.org/t/algebras-of-opaque-types-and-pattern-matching/5245 "2021-08-31T14:07:27Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![bblfish](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/bblfish/32/1575_2.png) [@bblfish](https://contributors.scala-lang.org/u/bblfish)\
**Post date:** [September 6, 2021, 10:32am UTC](https://contributors.scala-lang.org/t/algebras-of-opaque-types-and-pattern-matching/5245/3 "2021-09-06T10:32:56Z")

</div>

Thanks @kai for the idea of using pattern matching on types and the TypeTest idea.  
TypeTests do allow me to now pattern match on types as [shown here](https://github.com/bblfish/banana-play/blob/8679bdae63f91c64a472eccf2f9ea75a62e2d788/src/test/scala/ideas/MatchTypes.scala#L418)

```scala
bKt.asMatchable match
	case Triple(sub: URI,rel,obj: URI) => ...

```

For simple types such as URIs it could be well argued that this is all that is needed.  
For more complex types such as RDF literals, which are elements of the disjunctive type `String + String⨉Lang + String⨉URI` we may want to pattern match inside the pattern. But if I try to change [the type test here](https://github.com/bblfish/banana-play/blob/8679bdae63f91c64a472eccf2f9ea75a62e2d788/src/test/scala/ideas/MatchTypes.scala#L445) to this

```scala
t.asMatchable match
   case Triple(Bbl,Name,Literal(lit)) => ...

```

I get the error message

```auto
case Triple(Bbl,Name,Literal(lit)) =>
      			     ^^^^^^^^^^^^

```

> pattern selector should be an instance of Matchable, but it has unmatchable type rdf.Node instead

Ideally I would want to go one step further with something functionally equivalent to this, but more elegant:

```scala
t.asMatchable match
   case Triple(Bbl,Name,Literal(LangLit("Henry",en))) => ...

```

but that does not compile either.

---

_[View the full topic](https://contributors.scala-lang.org/t/algebras-of-opaque-types-and-pattern-matching/5245)._
