为什么字符串文字符合 Scala Singleton [英] Why do String literals conform to Scala Singleton

查看:38
本文介绍了为什么字符串文字符合 Scala Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明智与否,我正在编写一个我只想接受 Scala 单例的方法,即通过对象"而不是类或特征的构造实例实现的对象.它应该接受任何类型的 Scala 单例,所以MySingleton.type"不会.

Wisely or not, I'm writing a method that I'd like to accept only Scala singletons, i.e. objects implemented via "object" rather than constructed instances of a class or trait. It should accept Scala singletons of any type, so "MySingleton.type" won't do.

我发现了一个非常奇怪的构造scala.Singleton",它没有在 api 文档中记录,但似乎可以解决问题:

I came upon the very strange construct "scala.Singleton", which is not documented in the api docs, but seems to do the trick:

scala> def check( obj : Singleton ) = obj
check: (obj: Singleton)Singleton

scala> check( Predef )
res0: Singleton = scala.Predef$@4d3e9963

scala> check ( new java.lang.Object() )
<console>:9: error: type mismatch;
 found   : java.lang.Object
 required: Singleton
              check ( new java.lang.Object() )

scala> check( Map )
res3: Singleton = scala.collection.immutable.Map$@6808aa2d

scala> check( Map.empty[Any,Any] )
<console>:9: error: type mismatch;
 found   : scala.collection.immutable.Map[Any,Any]
 required: Singleton
              check( Map.empty[Any,Any] )

然而,令人费解的是(对我来说),字符串文字被接受为单例,而显式构造的字符串不是:

However, rather inexplicably (to me), String literals are accepted as Singletons while explicitly constructed Strings are not:

scala> check( "foo" )
res7: Singleton = foo

scala> check( new String("foo") )
<console>:9: error: type mismatch;
 found   : java.lang.String
 required: Singleton
              check( new String("foo") )

为什么字符串文字符合单例?我是否误解了 Singleton 类型应该指定的内容?

Why do String literals conform to Singleton? Am I misunderstanding what the Singleton type is supposed to specify?

推荐答案

首先,什么是单例类型?如果您认为类型是一组值,那么单例类型就是只有一个元素的集合.

Firstly, what is a singleton type? If you take the view that a type is a set of values, a singleton type is a set with exactly one element.

最常见的是,顶级对象可以包含在这样的集合中.

Most commonly, a top level object can inhabit such a set.

scala> object X
defined module X

scala> X: X.type
res41: X.type = X$@131d1cb

scala> res41: Singleton
res42: Singleton = X$@131d1cb

更一般的,stable值可以形成单例类型.

More generally, and stable value can form a singleton type.

scala> object X { val y: String = "boo" }
defined module X

scala> X.y: X.y.type
res44: X.y.type = boo

scala> res44: Singleton
res45: Singleton = boo

如果 ydefvar,它不再符合条件,因为随着时间的推移值可能不一样,所以编译器不能保证单例类型对一且一的值进行分类.

If y is a def or a var, it no longer qualifies, as the value might not be the same over time, so the compiler can't guarantee that the singleton type classifies one-and-only-one value.

scala> object X { def y: String = "boo" }
defined module X

scala> X.y: X.y.type
<console>:12: error: stable identifier required, but X.y found.
              X.y: X.y.type
                     ^

scala> object X { var y: String = "boo" }
defined module X

scala> X.y: X.y.type
<console>:12: error: stable identifier required, but X.y found.
              X.y: X.y.type
                     ^

还有一个限制:AnyVals 不能形成单例类型,因为语言规范特别将它们限制为 AnyRef.

One more limitation: AnyVals can't form singleton types, because the language specification specifically restricts them to AnyRef.

Paul Phillips 一直在策划一个 branch,它允许您为文字.

Paul Phillips has been curating a branch which allows you to express a singleton type for literals.

val xs: Stream[0.type](0)
val ys: Stream[0.type](0, 1) // does not compile
val x = xs.head // inferred type is 0.type, we statically know that this can only be 0!

这篇关于为什么字符串文字符合 Scala Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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