Manifest

本文探讨了Scala中的类型擦除问题,以及如何通过清单(Manifest)来解决在编译时未知类的情况下创建原生数组的问题。类型关系运算符被视为近似值,因为类型一致性在清单中并未完全体现。示例代码展示了如何使用Manifest进行数组创建,以及如何检查类型间的近似子类型关系。此外,还展示了如何获取方法的返回类型信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

清单[T]是类型T的晦涩描述符。它的支持用途是作为类实例提供对类型擦除的访问,如果类在编译时未知,这是创建原生数组所必需的。
A Manifest[T] is an opaque descriptor for type T. Its supported use is to give access to the erasure of the type as a Class instance, as is necessary for the creation of native Arrays if the class is not known at compile time.

类型关系运算符<:<和=:=应仅被视为近似值,因为类型一致性的许多方面在清单中尚未得到充分体现。
The type-relation operators <:< and =:= should be considered approximations only, as there are numerous aspects of type conformance which are not yet adequately represented in manifests.


scala> def arr[T] = new Array[T](0)   
<console>:11: error: cannot find class tag for element type T
       def arr[T] = new Array[T](0)
                    ^

scala> def arr[T](implicit m: Manifest[T]) = new Array[T](0)
arr: [T](implicit m: Manifest[T])Array[T]

scala> def arr[T: Manifest] = new Array[T](0)               
arr: [T](implicit evidence$1: Manifest[T])Array[T]

scala> def isApproxSubType[T: Manifest, U: Manifest] = manifest[T] <:< manifest[U]
warning: there was one deprecation warning; re-run with -deprecation for details
isApproxSubType: [T, U](implicit evidence$1: Manifest[T], implicit evidence$2: Manifest[U])Boolean

scala> isApproxSubType[List[String], List[AnyRef]]
res5: Boolean = true

scala> isApproxSubType[List[String], List[Int]] 
res6: Boolean = false

scala> def methods[T: Manifest] = manifest[T].runtimeClass.getMethods
 
methods: [T](implicit evidence$1: Manifest[T])Array[java.lang.reflect.Method]

scala> def retType[T: Manifest](name: String) =
   methods[T] find (_.getName == name) map (_.getGenericReturnType)
     | retType: [T](name: String)(implicit evidence$1: Manifest[T])Option[java.lang.reflect.Type]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BigDataMLApplication

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值