Scala String 与 java.lang.String - 类型推断 [英] Scala String vs java.lang.String - type inference

查看:35
本文介绍了Scala String 与 java.lang.String - 类型推断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 REPL 中,我定义了一个函数.注意返回类型.

scala>def next(i: List[String]) = i.map {"0" + _} ::: i.reverse.map {"1" + _}下一个: (i: List[String])List[java.lang.String]

如果我将返回类型指定为字符串

scala>def next(i: List[String]): List[String] = i.map {"0" + _} :: i.reverse.map {"1" + _}下一个: (i: List[String])List[String]

为什么不同?我还可以将返回类型指定为 List[Any],所以我猜 String 只是 java.lang.String 的包装超类型.这会产生任何实际影响还是我可以安全地不指定返回类型?

解决方案

这是一个很好的问题!首先,我向您保证,您可以安全地指定返回类型.

现在,让我们研究一下……是的,当进行推理时,Scala 会推断 java.lang.String,而不仅仅是 String.因此,如果您在 ScalaDoc,你什么也找不到,这似乎表明它也不是 Scala 类.不过,它必须来自某个地方.

让我们考虑 Scala 默认导入的内容.你可以自己在 REPL 上找到:

scala>:进口1)导入java.lang._(155种,160项)2) 导入 scala._ (801 类型, 809 术语)3)import scala.Predef._(16种类型,167个术语,96个是隐式的)

前两个是包——事实上,String 可以在 java.lang 上找到!那是这样吗?让我们通过实例化该包中的其他内容来进行检查:

scala>val s: StringBuffer = 新的 StringBuffers: java.lang.StringBuffer =标度>val s: 字符串 = 新字符串s: 字符串 = ""

所以,似乎不是这样.现在,它不能在 scala 包内,否则在查找 ScalaDoc 时会被找到.那么让我们看看 scala.Predef,就是这样!

type String = String

这意味着 Stringjava.lang.String(之前导入的)的别名.这看起来像一个循环引用,但如果你检查 source,你会看到它是用完整路径定义的:

type String = java.lang.String

接下来,您可能要问为什么?好吧,我不知道,但我怀疑这是为了让如此重要的类对 JVM 的依赖性降低一点.>

In the REPL, I define a function. Note the return type.

scala> def next(i: List[String]) =  i.map {"0" + _} ::: i.reverse.map {"1" + _}
next: (i: List[String])List[java.lang.String]

And if I specify the return type as String

scala> def next(i: List[String]): List[String] = i.map {"0" + _} ::: i.reverse.map {"1" + _}
next: (i: List[String])List[String]

Why the difference? I can also specify the return type as List[Any], so I guess String is just a wrapper supertype to java.lang.String. Will this have any practical implications or can I safely not specify the return type?

解决方案

This is a very good question! First, let me assure you that you can safely specify the return type.

Now, let's look into it... yes, when left to inference, Scala infers java.lang.String, instead of just String. So, if you look up "String" in the ScalaDoc, you won't find anything, which seems to indicate it is not a Scala class either. Well, it has to come from someplace, though.

Let's consider what Scala imports by default. You can find it by yourself on the REPL:

scala> :imports
 1) import java.lang._             (155 types, 160 terms)
 2) import scala._                 (801 types, 809 terms)
 3) import scala.Predef._          (16 types, 167 terms, 96 are implicit)

The first two are packages -- and, indeed, String can be found on java.lang! Is that it, then? Let's check by instantiating something else from that package:

scala> val s: StringBuffer = new StringBuffer
s: java.lang.StringBuffer =

scala> val s: String = new String
s: String = ""

So, that doesn't seem to be it. Now, it can't be inside the scala package, or it would have been found when looking up on the ScalaDoc. So let's look inside scala.Predef, and there it is!

type String = String

That means String is an alias for java.lang.String (which was imported previously). That looks like a cyclic reference though, but if you check the source, you'll see it is defined with the full path:

type String        = java.lang.String

Next, you might want to ask why? Well, I don't have any idea, but I suspect it is to make such an important class a little less dependent on the JVM.

这篇关于Scala String 与 java.lang.String - 类型推断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆