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

scala – 被视为序列的字符串

发布时间:2020-12-16 18:14:29 所属栏目:安全 来源:网络整理
导读:丹尼尔在这篇文章的回答中: What are Scala context and view bounds? 提出了一种将String作为Scala集合处理的方法. handling String and Array,which are Java classes,like they were Scala collections. For example: def f[CC % Traversable[_]](a: CC,
丹尼尔在这篇文章的回答中:
What are Scala context and view bounds?

提出了一种将String作为Scala集合处理的方法.

handling String and Array,which are Java classes,like they were Scala >collections. For example:

def f[CC <% Traversable[_]](a: CC,b: CC): CC = if (a.size < b.size) a else b

我想知道在Scala标准库中我可以在哪里找到这个功能.

与上述帖子相关的另一个简单问题:

我一直看到使用的简写“ev”,特别是当与上下文边界或视图边界示例相关时:

def g[A](a: A)(implicit ev: B[A]) = h(a)

它代表什么?

先感谢您.
干杯

解决方法

I would like to know were I can find this functionality in the Scala
standard libs.

Scala提供了一个名为WrappedString的java.lang.String包装器:

final class WrappedString(val self: String) extends AbstractSeq[Char]
                                            with IndexedSeq[Char] 
                                            with StringLike[WrappedString]

当你运行:

f("he","hello")

编译器通过Predef.wrapString隐式地将字符串文字转换为WrappedString的实例:

f[String]("he","hello")({
  ((s: String) => scala.this.Predef.wrapString(s))
});

反过来,WrappedString扩展了IndexedSeq [Char],这就是它遵循视图边界请求可转换为可遍历的原因.

I keep seeing the shorthand “ev” used,especially when related to
context bounds or view bounds,what does it stand for?

它是“证据”的缩写.如果你考虑一下,当你请求某种隐式参数在范围内时,编译器要求你提供操作可能发生的证据.

(编辑:李大同)

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

    推荐文章
      热点阅读