Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generating a match case with wildcard types #19325

Unanswered
adamw asked this question in Metaprogramming
Discussion options

Hello,

given a TypeRepr which represents an applied generic type such as List[String], I'd like to generate a match case:

case List[_] =>

(to avoid unchecked warnings). I've got this working for non-generic types, but the wildcards pose a problem. Here's what I got so far:

myTypeRepr.classSymbol match
 case Some(sym) if myTypeRepr.typeArgs.nonEmpty => // is generic
 val wildcardTypeParameters: List[Tree] =
 List.fill(orType.typeArgs.length)(WildcardTypeTree(TypeBounds(TypeRepr.of[Any], TypeRepr.of[Nothing])))
 Some(CaseDef(Typed(Wildcard(), Applied(TypeIdent(sym), wildcardTypeParameters)), None, caseThen))
 // other cases

However, this leads to a compile-time error:

[error] |Term-dependent types are experimental,
[error] |they must be enabled with a `experimental.dependent` language import or setting

In other cases I'm using Typed(Wildcard(), TypeIdent(sym)), so I suspect this must be something with wildcardTypeParameters, but I'm a bit stuck trying to find exactly what.

Or maybe I'm trying to approach this wrong the wrong angle?

Thanks :)

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

I'm pretty sure the lower bound comes first and the higher bound comes second:

TypeBounds(TypeRepr.of[Nothing], TypeRepr.of[Any])
You must be logged in to vote
2 replies
Comment options

Ah yes, thanks :) Of course I swapped these ... still, getting the same error during compilation

Comment options

I remembered correctly that the order in the trailer company is hi-lo.

https://hilotrailer.com/home

Ah yes, see the "then" photo, that's what it looked like in childhood.

Comment options

Not sure what was wrong with the original one, but this one works:

myTypeRepr.classSymbol match
 case Some(sym) if myTypeRepr.typeArgs.nonEmpty => // is generic
 val wildcardTypeParameters: List[Tree] =
 List.fill(orType.typeArgs.length)(TypeBoundsTree(TypeTree.of[Nothing], TypeTree.of[Any]))
 Some(CaseDef(Typed(Wildcard(), Applied(TypeIdent(sym), wildcardTypeParameters)), None, caseThen))

this variant is based on the output of printing the tree for Nil match { case _: List[_ >: Nothing <: Any] => }, which is reported as:

Inlined(None, Nil, Block(Nil, Match(Ident("Nil"), List(CaseDef(Typed(Wildcard(), Applied(TypeIdent("List"), List(TypeBind(_$1, TypeBoundsTree(TypeIdent("Nothing"), TypeIdent("Any")))))), None, Block(Nil, Literal(UnitConstant())))))))

I don't have the TypeBind(...) wrapper, but it seems it's not necessary.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /