加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 为什么productIterator返回类型Iterator [Any]?

发布时间:2020-12-16 18:32:40 所属栏目:安全 来源:网络整理
导读:为什么 scala Tuple productIterator返回类型Iterator [Any]? 例如,如果Tuple3或Product3 productIterator定义如下 def productIterator[T1:X,T2:X,T3:X,X] = Iterator(_1,_2,_3) 以下Expression可以返回Iterator [java.lang.Number] (BigInt(1),new java.l
为什么 scala Tuple productIterator返回类型Iterator [Any]?

例如,如果Tuple3或Product3 productIterator定义如下

def productIterator[T1<:X,T2<:X,T3<:X,X] = Iterator(_1,_2,_3)

以下Expression可以返回Iterator [java.lang.Number]

(BigInt(1),new java.lang.Long(2),new java.lang.Float(3)).productIterator

但目前的scala版本(2.9.1)并非如此.有什么理由吗?

解决方法

% scala3 -Xexperimental 
Welcome to Scala version 2.10.0.rdev-4056-2011-12-21-g69ed0a9 (Java HotSpot(TM) 64-Bit Server VM,Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class Foo[T1 <: java.lang.Number,T2 <: java.lang.Number](s: T1,t: T2)
defined class Foo

scala> Foo[java.lang.Integer,java.lang.Long](5,5L)
res1: Foo[Integer,Long] = Foo(5,5)

scala> res1.productIterator _
res2: () => Iterator[Number] = <function0>

UPDATE

它是产品元素(未删除)类型的最小上限.例如,在Product [T,U,V]中,它与表达式的类型相同

if (cond1) x1: T else if (cond2) x2: U else x3: V

或者列表的类型

List(x1: T,x2: U,x3: V)

在repl中查看f in的推断类型

def f[T] = List(null: List[T],null: Set[T],null: Iterator[T])

看看我的意思.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读