How tot define wildcard patterns in 3.0.x #12472
-
Compiler version3.0.0 RC3 Minimized example /*
'sel' is an 'Expr' returning an 'Int'.
We need to convert this 'Expr' into a 'Tree' subtype.
In our context, 'sel' is a 'Term'.
'sel' will be the selector for the match expression we want to synthetize.
*/
val selector = sel.asTerm
/*
We need a sequence of case block statement to synthetize the match block expression.
We want to use the array parameter to define this sequence dynamicaly at runtime.
We use the 'map' method to create this case sequence by itterating over the array 'arr'.
Each itteration produce a 'CaseDef' instance.
QUESTION ??? : How to use WildcardTypeTreeModule to implemnt the default 'CaseDef'.
*/
val dynCases: List[CaseDef] = arr.toList.map{ i =>
/*
CaseDef statement :
- Pattern : Match an array element x (as a literal).
- Guard : Expect an 'Option'. Use 'None' as we don't use this feature.
- RHS : return 'x + 1' as an 'Int'. x is the array element used to create the above pattern.
'IntConstant' is a subtype of 'Constant'
'Literal' produce a 'Term' from a 'Constant'
*/
val pattern = Literal(IntConstant(i))
val guard = None
val rhs = Literal(IntConstant(333))
// Literal(IntConstant(333)))
// Literal(IntConstant(i + 1))
CaseDef(pattern, guard, rhs)
}
//val defaultPattern = Literal(IntConstant(10))
//val defaultPattern = Ident(_).underlying()
//val defaultPattern : Tree = Ident(Symbol("_")).asInstanceOf[Tree]
//val defaultPattern: Tree = Ident(TermRef(TypeRepr.of[Any],"_")).asInstanceOf[Tree]
//val defaultPattern: Tree = Ident("_")
//val defaultPattern = Literal(IntConstant(10))
//val wildcard = Symbol.newVal(Symbol.noSymbol, "_", TypeRepr.of[fontInt], Flags.EmptyFlags, Symbol.noSymbol)
//val defaultPattern = Typed(Ident(TermRef(TypeRepr.of[Int], "_")), TypeIdent(Symbol.classSymbol("scala.Int"))).asInstanceOf[Tree]
//Ident(wildcard)
val defaultPattern = Typed('{_:Int}.asTerm, TypeIdent(Symbol.classSymbol("scala.Int"))).asInstanceOf[Tree]
val defaultGuard = None
val defaultRhs = Literal(IntConstant(10))
//val defaultRhs = Block( List( ValDef( Symbol.newVal( Symbol.noSymbol,"y",TypeRepr.of[Int],Flags.EmptyFlags,Symbol.noSymbol), Some( Literal( IntConstant( 40))))), Typed.apply( TermRef.apply( TypeRepr.of[Int], "y" ).asInstanceOf[Term] , TypeTree.of[Int] ) )
val defaultCase = List(CaseDef(defaultPattern, defaultGuard, defaultRhs))
val cases = dynCases ::: defaultCase
/*
We can now build the match expression using the selector and the case sequence.
We do that using TASTY low level API ('Tree' object).
Then we convert into higher level 'Expr' returning an 'Int'.
This 'Expr' is the function return.
*/
Match(selector, cases).asExprOf[Int] ExpectationHello,
Do you have any idea how the default case could be implemented ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
We need a wildcard but that is blocked by issue #12200. The solution for now is to add a Bind pattern as las pattern that binds to anything and just ignore the binding. case x =>
// Instead of
case _ => |
Beta Was this translation helpful? Give feedback.
-
Hello @nicolasstucki val intTypeTree = TypeTree.of[Int]
val bind = Symbol.newBind(Symbol.spliceOwner, "_", Flags.EmptyFlags, intTypeTree.tpe)
val defaultPattern = Bind(bind, Typed(Ref(bind),intTypeTree))
val defaultGuard = None
val defaultRhs = Literal(IntConstant(10))
val defaultCase = List(CaseDef(defaultPattern, defaultGuard, defaultRhs)) I can generate and execute this below code at runtime
The last casedef works as a default one. I went to the other discussion and i have to admit that i have the same issue trying to understand Ref , Ident and Select. +- TypeRepr -+- NamedType -+- TermRef I see we can use Symbol to represent langage token used in deffinition ( val, var, def ). But not for type ? And then we need TypeRepr and TypeRef instead ? But what is the point of TermRef. I am trying to write concrete example like below for each module in the low level API : Thank for your help and explanation. |
Beta Was this translation helpful? Give feedback.
We need a wildcard but that is blocked by issue #12200.
The solution for now is to add a Bind pattern as las pattern that binds to anything and just ignore the binding.