scalamacrosscala-macrosscala-3scala-quasiquotes

Replacement for `x.asType match { case '[type t <: S; t] => ...` in Scala 3.3 (LTS)


Do we have any replacement for the following Scala 3.7 code:

x.asType match { 
  case '[type t <: S; t] => 
    '{ apply[t] }.asExprOf[Any] 
  case _ => 
    report.errorAndAbort(s"Expected $x to be a sub-type of ${TypeRepr.of[S]}",Position.ofMacroExpansion) 
}

in Scala 3.3.6 (the latest LTS release)?


Solution

  • Try

    x.asType match {
      case '[t] => '{tag[t]} match {
        case '{type t1 <: S; tag[`t1`]} =>
          '{ apply[t1] }.asExprOf[Any]
      }
    

    for a dummy method

    // outside macro impl
    def tag[A] = ???
    
    x.asType match { 
      case '[t] => 
        '{ apply[t & S] }.asExprOf[Any] 
    
    type SBounded[T <: S] = T
    
    x.asType match {
      case '[SBounded[t]] =>
        '{ apply[t] }.asExprOf[Any]
    

    In scala 3.6.4, how to call a polymorphic method with type bounds from inside a quoted expression?