Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 将某个特征限制为对象?_Scala_Traits_Singleton Type - Fatal编程技术网

Scala 将某个特征限制为对象?

Scala 将某个特征限制为对象?,scala,traits,singleton-type,Scala,Traits,Singleton Type,有没有办法限制某个特征,使其只能混合到对象中?例如 trait仅适用于对象{ 这个:…=> } 对象Foo仅扩展到对象//-->确定 类栏仅扩展对象//>编译错误 是!还有一些晦涩难懂且大多未记录的scala.Singleton: scala> trait OnlyForObjects { this: Singleton => } defined trait OnlyForObjects scala> object Foo extends OnlyForObjects def

有没有办法限制某个特征,使其只能混合到对象中?例如

trait仅适用于对象{
这个:…=>
}
对象Foo仅扩展到对象//-->确定
类栏仅扩展对象//>编译错误

是!还有一些晦涩难懂且大多未记录的scala.Singleton:

scala> trait OnlyForObjects { this: Singleton => }
defined trait OnlyForObjects

scala> object Foo extends OnlyForObjects
defined module Foo

scala> class Bar extends OnlyForObjects
<console>:15: error: illegal inheritance;
 self-type Bar does not conform to OnlyForObjects's selftype OnlyForObjects
 with Singleton
       class Bar extends OnlyForObjects
                         ^
scala>trait only对象{this:Singleton=>}
仅为对象定义的特征
scala>object Foo仅扩展对象
定义模块Foo
scala>类栏仅扩展对象
:15:错误:非法继承;
自类型栏不符合仅对象的自类型仅对象
单身汉
类栏仅扩展对象
^

它在中提到过几次,但在API文档中甚至没有出现。

Ha!我开始期待你们使用疯狂的语言功能给出完全疯狂和惊人的答案,但现在我明白了,我们已经转向了秘密功能。是的!终于有人问了这个问题!。您知道为什么它在scala文件中工作,但在工作表中失败吗?