Scala 给定类型,创建包含该类型的选项类型
给定一个名为inner的类型,我想创建一个新类型,它表示一个包含与inner相同类型的选项 例如,执行此操作的方法的签名可能类似于:Scala 给定类型,创建包含该类型的选项类型,scala,reflection,Scala,Reflection,给定一个名为inner的类型,我想创建一个新类型,它表示一个包含与inner相同类型的选项 例如,执行此操作的方法的签名可能类似于: def createOptionType(inner: Type): Type = { typeOf[Option[inner]] // this line is pseudocode } 部分帮助,但使用了泛型方法,而在本例中,我传入了一个具体类型,使泛型无法使用。您可以使用scala.reflect.runtime.universe.appliedTyp
def createOptionType(inner: Type): Type = {
typeOf[Option[inner]] // this line is pseudocode
}
部分帮助,但使用了泛型方法,而在本例中,我传入了一个具体类型,使泛型无法使用。您可以使用scala.reflect.runtime.universe.appliedType:
import scala.reflect.runtime.universe._
def createOptionType(inner: Type): Type =
appliedType(typeOf[Option[_]], inner)
您可以使用scala.reflect.runtime.universe.appliedType进行以下操作:
import scala.reflect.runtime.universe._
def createOptionType(inner: Type): Type =
appliedType(typeOf[Option[_]], inner)