- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
def call = s"-6ren">
在使用早期初始化语法时似乎有一个微妙之处。
trait Base { def callMe = "callMe" }
trait Proxy { this: Base => def call = s"proxied: $callMe" }
val base1 = new Base { } // non-early init works
val baseFail = new { } with Base // error: trait Base is abstract; cannot be instantiated
val base2 = new { val n=1 } with Base // why does this fix the failure?
val proxy = new { } with Base with Proxy // why doesn't this fail?
为什么 baseFail
行失败,而其他 val
却没有?
错误消息也令人困惑 - 我不是要实例化 Base
,只是将其混合进去。
最佳答案
当你写 new { } with Base
,技术上没有任何早期定义。根据 SLS 5.1.6,编译器查找如下模式:
EarlyDefs ::= `{' [EarlyDef {semi EarlyDef}] `}' `with'
EarlyDef ::= {Annotation} {Modifier} PatVarDef
虽然它没有明确说明当定义序列为空时会发生什么,但它似乎只是删除它们,因为当你编译 val a = new { val x = 1 } with Base
时,在解析阶段之后你会得到这样的东西:
val a = {
final class $anon extends Base {
val x = _;
def <init>() = {
val x = 1;
super.<init>();
()
}
};
new $anon()
}
但是如果你清空大括号,你只会得到:
val a = new Base()
这是非法的,因为 new Base
.
总结:
是一个匿名类,这是允许的:
val base1 = new Base { }
简化为 new Base
,这是非法的:
val baseFail = new { } with Base
是允许的适当的早期定义(非空):
val base2 = new { val n=1 } with Base
简化为 new Base with Proxy
这也是允许的:
val proxy = new { } with Base with Proxy
new { } with Base { }
也可以编译,但它与 new Base { }
完全相同, 所以没有理由那样写。
关于scala - 早期初始化程序 `new {} with SomeTrait` 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41189393/
我正在为从 API 级别 8 到 14 的 android 开发一个应用程序。我正在尝试在早期版本中获得与 android 4(请参阅联系人应用程序)相同的快速滚动行为(右侧固定的时尚滚动条)边)。有
早期(编译期)优化 jvm的编译器可以分为三个编译器: 前端编译器:把*.java转变为*.class的过程。如sun的javac、eclipse jdt中的增量式编译器(ecj)
苹果终于推出了所谓的auto-renewable subscriptions昨天。由于我在应用内购买方面的经验很少(仅限沙盒),所以我不确定我在这里是否一切顺利。似乎需要对收据进行服务器端验证。找出订
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 要求代码的问题必须表明对正在解决的问题的最低理解。包括尝试的解决方案、它们为什么不起作用以及预期结果。另
在 Wagner 的“Effective C#”第 23 项中,他解释说 interface methods are not virtual...they are a declaration of a
我最近遵循了本指南 Installing a Git Server using Apache (WebDAV) on Ubuntu Server 12.04使用 Apache (WebDAV) 设置本
这是我之前的问题 jQuery UI hiding not taking effect for early DOM elements 的后续问题。我几乎刚刚编辑了那个,但不想使 the accepte
我正在尝试替换 ZonedDateTime.toInstant方法,因为它仅从 API 26 for Android 开始可用。 但我的应用程序应该支持 API 19。 我想将 ZonedDateTi
我的电脑正确配置了 SSH,我在尝试克隆存储库时遇到了这个错误: 我运行这个命令来克隆存储库 git clone ssh://git-codecommit.us-west-2.amazonaws.co
我是一名优秀的程序员,十分优秀!