我在 Scala 3 中定义了以下特征:
\ntrait A[T <: Tuple]\nRun Code Online (Sandbox Code Playgroud)\n然后,我使用 Scala 3 宏创建具有此特征的对象,对元组的实际类型执行进一步检查T;特别是,我想检查元组的所有类型(T_1,\xe2\x80\xa6,T_n)T是否是另一个给定类型的子类型B:
trait B\nprivate def allSubtypesOfB[T <: Tuple: Type](using quotes: Quotes): Boolean = {\n import quotes.reflect.*\n case '[Nothing] => false // I don't want nothing to be in T\n case '[head *: tail] if TypeRepr.of[head] <:< TypeRepr.of[B] => allSubtypesOfB[tail]\n case '[EmptyTuple] => true\n case _ => false\n}\n\ninline def createA[T <: Tuple] = ${ createAImpl[T] }\nprivate def createAImpl[T …Run Code Online (Sandbox Code Playgroud)