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 types don't reduce on opaque types

See original GitHub issue

Compiler version

nightly: 3.0.1-RC1-bin-20210531-fa36577-NIGHTLY

Minimized code

object test {
  object opaques {
    opaque type @@[+S <: String & Singleton, +T] >: T = T
  }
  import opaques.*
  type FieldName[T] = T match {
    case @@[name, _] => name
    case _ => "other"
  }
  //neither of these compiles
  val a1: FieldName["a" @@ String] = "a" //doesn't  match the immediate case
  val a2: FieldName["a" @@ String] = "other" //doesn't go to other either
}

Expectation

a1 should compile

As far as I can tell, the above opaque type should never hit the rules for non-reducing. If the above opaque type is changed to the equivalent trait it behaves as expected

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
LPTKcommented, Jun 11, 2021

The problem is that given any T, you have the subtyping relationship T <: @@[S, T], where S can be anything. So the first case should always match, no matter what.

There is nothing to force the compiler to actually make the effort and see if there is a better way of matching the argument. In other words, in @@[A, B] <: @@[x, y], x = Nothing; y = @@[a, b] and x = a; y = b are two equally good solutions.

In general, there may be many solutions, but what usually keeps match types kind of unambiguous is that they are not usually made to work on “imaginary” types like @@, which is obviously (because of the bound) not a “real, physical type” but just a type system trick.

0reactions
rcanocommented, Jun 11, 2021

Thank you for the explanation, I think I know see where my misconception is: Your explanation makes all the sense to me if we were discussing type inference, maybe it’s the imperativeness of the term “match” that made me think this works more like a function on types, or the paragraph I quoted from the docs, but I was expecting the compiler to use the input I’m giving to the match case to make the effort to infer a “better response”, compared to just inferring a generally valid solution.

This makes me think that the documentation about match types should be rewritten because it conveys the wrong message. In any case I’m closing this issue. Thanks a lot for the help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Opaque Types — The Swift Programming Language (Swift 5.7)
The requirement to always return a single type doesn't prevent you from using generics in an opaque return type. Here's an example of...
Read more >
Inline methods and opaque types - Scala Contributors
Opaque types and inline defs limitation​​ Tried to use opaque types to create a compile time constrained Positive integer type: import ...
Read more >
Advanced Types in Elm - Opaque Types - Charlie Koster
It's not uncommon to see the constructor function name match the type name. ... Note that opaque types do not have any special...
Read more >
Opaque Type Aliases - Scala 3 - EPFL
Opaque types aliases provide type abstraction without any overhead. ... For that reason, the | extension method in Access does not cause infinite...
Read more >
why cant i return opaque types in my swift code? i am already ...
Function declares an opaque return type, but the return statements in its body do not have matching underlying types
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