为什么 sbt 以前(版本 ≤ 0.13.6)在 .sbt 文件中的设置之间需要空行? [英] Why did sbt formerly (version ≤ 0.13.6) require blank lines between settings in .sbt files?

查看:26
本文介绍了为什么 sbt 以前(版本 ≤ 0.13.6)在 .sbt 文件中的设置之间需要空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以例如你不能写:

a := 3
b := 4

您必须像这样将它们分开:

You had to separate them, like this:

a := 3

b := 4

我想不出另一种具有类似要求的文件格式.对于新人来说,这绝对是令人惊讶的.甚至一些长期使用 sbt 的用户也认为它令人反感.

I can't think of another file format with a similar requirement. It was definitely surprising to newcomers. Even some longtime sbt users considered it objectionable.

根据我的经验,这是在 sbt 更改之前最常被问到的一个问题.

In my experience, this was the single frequently-most-asked question about sbt, before it was changed.

推荐答案

从 sbt 0.13.7 开始,不再需要空行.所以以下现在是历史信息.

As of sbt 0.13.7, the blank lines are no longer required. So the following is now historical information.

简短的回答是空白行使文件机器可读机器可写.

The short answer is the blank lines make the file machine-readable and machine-writable.

让我们解开这个.

首先,请注意,设置使用多行是很常见的,例如长 Seq 或包含代码的任务正文.

First, note that it's common for settings to use multiple lines, for example a long Seq, or a task body containing code.

其次,请注意设置是表达式,而不是语句.所以 sbt 不能推断它们之间的分号.在 Scala 中,分号仅在语句之间进行推断.

Second, note that settings are expressions, not statements. So sbt can't infer semicolons between them. In Scala, semicolons are only inferred between statements.

所以需要空行,以便 sbt 知道一个设置在哪里结束,下一个设置在哪里开始.

So the blank lines are needed so that sbt knows where one setting ends and the next begins.

理论上,它可以确定吗?也许.2011 年 Mark Harrah(sbt 的创建者)写了:

In theory, could it determine that anyway? Perhaps. In 2011 Mark Harrah (the creator of sbt) wrote:

使用编译器分隔表达式 [...]可能会在某个时候发生,但我尽量避免引入尽可能启动编译器

using the compiler to separate expressions [...] may happen at some point, but I try to avoid introducing the overhead of starting up a compiler wherever possible

因此,这里的一个关键问题是性能.Scala 编译器是出了名的慢.您接下来可能想知道,无论如何,设置都需要编译,不是吗?是的,但很少有人意识到,为了提高性能,.sbt 文件中的每个设置都单独编译,编译结果缓存,即使在调用 sbt 之间也是如此.因此,如果您编辑构建并更改一项设置,只会重新编译该一项设置.

So it turns out that a key concern here is performance. The Scala compiler is notoriously slow. You might next wonder, well, the settings are going to need to be compiled anyway, aren't they? Yes, but few people realize that to improve performance, each setting in an .sbt file is separately compiled and the result of compilation cached, even between invocations of sbt. So if you edit your build and change one setting, only that one setting is recompiled.

上面,我说过 .sbt 文件是机器可写,而不仅仅是机器可读.您可以即时更改设置,然后使用 session save 保存更改.2014 年,Jason Zaugg(Typesafe 的)写道:

Above, I said that .sbt files are machine-writable, not just machine-readable. You can change settings on the fly and then save your changes with session save. In 2014 Jason Zaugg (of Typesafe) wrote:

[关于空白行]的决定最初来自 build.sbt 的约束应该是机器可编辑的.在 SBT 会话中,您可以运行 set foo := bar,然后将被持久化到文件中.

The decision [on blank lines] originally came from the constraint that build.sbt should be machine editable. In an SBT session, you can run set foo := bar, and that will be persisted into the file.

实际上,人们不太倾向于使用此功能,我们可能会重新审视这个决定 [...]

In practise, people don't tend to use this feature that much, and we might revisit this decision [...]

有关空白行背后的设计注意事项以及未来可能发生的变化的更多详细信息,请参阅 这篇文章 作者是 Josh Suereth(也是 Typesafe 的).

For further details on the design considerations behind the blank lines and how this might change in the future, see this writeup by Josh Suereth (also of Typesafe).

这篇关于为什么 sbt 以前(版本 ≤ 0.13.6)在 .sbt 文件中的设置之间需要空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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