Maybe this is not a bug, maybe it is a limitation or I'm thinking wrong, but for me it is not entirely clear why it depends on the key type whether I can match it. I asked in metaprogramming on discord and nobody seemed to know either, so here we are.
Also, thanks for your fantastic work on scala <3
Workaround
If somebody finds this ticket, whether it is a bug or not, and needs a quick workaround: Match on the TypeRepr:
val tpe: TypeRepr = ???
val mapTycon = Symbol.requiredClass("scala.collection.immutable.Map").typeRef
tpe.asMatchable match
case AppliedType(tycon, args) if tycon =:= mapTycon =>
val keyRepr = args(0)
val valueRepr = args(1)
keyRepr.asType match
case '[k] =>
valueRepr.asType match
case '[v] => ...
Now you have both k and v matched and it works even if k is an OrType, which you can then deconstruct further
Compiler version
3.5.1
Minimized code
A simple macro that has a quoted type pattern for a
'Map[k, v]
:Macro.scala:
Output
Expectation
Notes
Maybe this is not a bug, maybe it is a limitation or I'm thinking wrong, but for me it is not entirely clear why it depends on the key type whether I can match it. I asked in metaprogramming on discord and nobody seemed to know either, so here we are.
Also, thanks for your fantastic work on scala <3
Workaround
If somebody finds this ticket, whether it is a bug or not, and needs a quick workaround: Match on the
TypeRepr
:Now you have both
k
andv
matched and it works even ifk
is anOrType
, which you can then deconstruct further