- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习如何在 Scala 中编程,并被告知分号在 Scala 中是可选的。因此,考虑到这一点,我尝试使用以下没有分号的嵌套代码块。但是,它在 Scala REPL 中引发错误
scala> { val a = 1
| {val b = a * 2
| {val c = b + 4
| c}
| }
| }
<console>:17: error: Int(1) does not take parameters
{val b = a * 2
带有分号的示例工作得很好。
scala> { val a = 1;
| { val b = a*2;
| { val c = b+4; c}
| }
| }
res22: Int = 6
因此,在我看来,分号并不是真正可选的,并且在某些情况下是强制性的。请问什么情况下必须加分号?
最佳答案
我将尝试从您的示例中提取精华。
考虑以下代码片段:
{ val x = 1 { val y = 2 } }
对于编译器来说,它看起来像是
的语法糖{ val x = 1.apply({ val y = 2 }) }
但是对象 1
没有使用 block 的 apply
方法,因此编译器会产生错误:
error: Int(1) does not take parameters
{ val x = 1 { val y = 2 } }
^
对比一下
object I { def apply(a: => Any): Unit = () }
{ val x = I { val y = 2 } }
这是可行的,因为我
现在确实有一个apply
方法。
为了更容易区分这两种情况,编译器需要在第一种情况下使用分号。
现在,人们可能想知道为什么 val x = 1
和 {
之间的换行符没有转换为推断的分号。我认为规范中的相关引用是这样的( 1.2 Newline Characters )(省略了枚举的大部分部分([...]
),强调我的):
The Scala grammar [...] contains productions where optional
nl
tokens, but not semicolons, are accepted. This has the effect that a newline in one of these positions does not terminate an expression or statement. These positions can be summarized as follows:[...]
in front of an opening brace ‘{’, if that brace is a legal continuation of the current statement or expression,
[...]
请注意,此引用仅涵盖带有单个可选换行符的情况。它不适用于两个或多个连续换行符,例如
scala> {
| val x = 1
|
| { val y = 2 }
| }
有效,并且 { val y = 2 }
被解析为单独的表达式。
我猜想的动机是允许嵌入带有语法糖的 DSL,如下所示:
MY_WHILE(x >= 0)
{
println(x)
x -= 1
}
如果必须将每个这样的 MY_WHILE
语句括在一对额外的圆括号中,那会很奇怪,不是吗?
关于scala - scala 中什么时候必须使用分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017702/
;用作语句分隔符,所以放置多个;在语句的末尾很好,因为它只是添加了空语句。 我遇到了这个有多个 ; 的代码最后但删除它们会导致错误: $line =~s;[.,]$;;; 应该与 $line =~s;
当使用自动完成来填充方法参数时,大多数情况下最后应该有一个分号。 似乎必须有一些键盘快捷键可以在最后输入分号(也可能是换行符),但我找不到它! 谢谢。 编辑 : 也许问题不清楚,当完成一个自动完成方法
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我知道我不应该在循环后放置分号。但我正在学习,不小心插入了一个。我想确切地知道我的错误到底发生了什么。所以下次发生类似的事情时,我就知道错误的根源。 在下面的代码中,在这部分代码中: triangul
我想拆分我的查询,但没有得到完全符合我要求的答案。 我的字符串如下: select 1;select \\2; select 3\\;copy customer from 's3://mybucket
VBA 中带有 Debug.Print 语句的分号 ( ; ) 的实际值/含义是什么? 我经常使用它,但对它没有真正的了解,我所知道的是它不等于 vbTab 常量,因为间距不一样。 IE。 Debug
我需要从 HTML 中的 javascript 代码获取分配的变量名称和值。 例如有html代码: ~~ contents var value1 = 55;var value2= 27;
我正在用 JSFiddle 校对这个函数,它说第二行缺少分号,但是我无法确定它会去哪里。 function debtSpiral() { var debtID = setInterval(debt
给定一个使用 ; 策略的有效 Coq 证明,是否有一个通用公式可以将其转换为用 . 替换 ; 的有效等效证明;? 许多 Coq 证明使用 ; 或战术排序策略。作为初学者,我想观察各个步骤的执行,因此我
我被困在工作中,电脑被锁定。但我正在尝试练习我的 Scala。我正在使用 Ideone.com,因为我什至无法安装 scalac... 无论如何这不是编译: class DPt(var name: S
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
考虑这些文件名。 ; f - Copy.txt ;f.txt f - Copy ;- Copy.txt f - Copy.txt 我有这段代码可以解析任何文件中的 & 符号,答案是:Drag and
为什么带有验证器(Mozilla 或 JSlint)的 Aptana 会提示这段代码: var collectionOfValues = { key0 : value0; key1 :
有什么方法可以绑定(bind); (\059) 到 tmux 中的命令? 默认情况下,它绑定(bind)到最后一个 Pane ;但是,我想将其重新绑定(bind)到 select-pane -R。 我
我有一个 Java 字符串,它实际上是一个 SQL 脚本。 CREATE OR REPLACE PROCEDURE Proc AS b NUMBER:=3; c
我有一个 python 字符串: names = "John, Paul; Sally/Frank" 我想用 , 分割字符串;/.我试过: names.split(",") 但我不确定如何通过所有分隔
在声明一组变量时使用逗号而不是分号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
在声明一组变量时使用逗号而不是分号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
我一直在尝试并查看各种命令,但似乎无法找到正确的语法; 我想合并两个 ls 命令的输出,除以 ;(分号)并将输出写入文件。 我的 ls 命令是: ls *.lst ls -d -1 $PWD/*.ls
在声明一组变量而不是分号时使用逗号有什么区别和/或优点(如果有的话)。 例如: var foo = 'bar', bar = 'foo'; 对比 var foo = 'bar'; var bar =
我是一名优秀的程序员,十分优秀!