From 33243c0a00f1c0dd3ebaac25ae09ce470453475d Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Mon, 26 Feb 2024 11:16:04 +0800 Subject: [PATCH 1/2] update translation according to Engish. --- .../scala3-book/methods-main-methods.md | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/methods-main-methods.md b/_zh-cn/overviews/scala3-book/methods-main-methods.md index 60ffd9c028..0a35fd27a2 100644 --- a/_zh-cn/overviews/scala3-book/methods-main-methods.md +++ b/_zh-cn/overviews/scala3-book/methods-main-methods.md @@ -13,6 +13,7 @@ layout: multipage-overview permalink: "/zh-cn/scala3/book/:title.html" --- +
编写仅Scala 3 适用的一行程序
Scala 3 提供了一种定义可以从命令行调用的程序的新方法:在方法中添加 `@main` 注释会将其变成可执行程序的入口点: @@ -26,16 +27,10 @@ Scala 3 提供了一种定义可以从命令行调用的程序的新方法:在 {% endtab %} {% endtabs %} -只需将该行代码保存在一个名为 *Hello.scala* 的文件中——文件名不必与方法名匹配——并使用 `scalac` 编译它: +只需将该行代码保存在一个名为 *Hello.scala* 的文件中——文件名不必与方法名匹配——并使用 `scala` 运行它: ```bash -$ scalac Hello.scala -``` - -然后用 `scala` 运行它: - -```bash -$ scala hello +$ scala Hello.scala Hello, world ``` @@ -81,8 +76,8 @@ $ scala happyBirthday 23 Lisa Peter Happy 23rd Birthday, Lisa and Peter! ``` -如图所示,`@main` 方法可以有任意数量的参数。 -对于每个参数类型,必须是 `scala.util.CommandLineParser.FromString` 类型类的一个 [given实例]({% link _overviews/scala3-book/ca-context-parameters.md %}),它将参数 `String` 转换为所需的参数类型。 +如上所示,`@main` 方法可以有任意数量的参数。 +对于每个参数类型,必须是 `scala.util.CommandLineParser.FromString` 类型类的一个 [given实例]({% link _zh-cn/overviews/scala3-book/ca-context-parameters.md %}),它将参数 `String` 转换为所需的参数类型。 同样如图所示,主方法的参数列表可以以重复参数结尾,例如 `String*`,它接受命令行中给出的所有剩余参数。 从 `@main` 方法实现的程序检查命令行上是否有足够的参数来填充所有参数,以及参数字符串是否可以转换为所需的类型。 @@ -96,6 +91,36 @@ $ scala happyBirthday sixty Fred Illegal command line: java.lang.NumberFormatException: For input string: "sixty" ``` +## 用户自定义的类型作为参数 + +正如上面指出的,编译器为参数类型寻找 `scala.util.CommandLineParser.FromString` 类型类 的given 实例。 +例如,我们自定义了一个 `Color`类型,并希望当以参数使用。你可以像以下代码这样使用: +As mentioned up above, the compiler looks for a given instance of the +`scala.util.CommandLineParser.FromString` typeclass for the type of the +argument. For example, let's say you have a custom `Color` type that you want to +use as a parameter. You would do this like you see below: + +{% tabs method_3 %} +{% tab 'Scala 3 Only' for=method_3 %} + +```scala +enum Color: + case Red, Green, Blue + +given ComamndLineParser.FromString[Color] with + def fromString(value: String): Color = Color.valueOf(value) + +@main def run(color: Color): Unit = + println(s"The color is ${color.toString}") +``` + +{% endtab %} +{% endtabs %} + +这像你在程序中用的自己的用户类型一样,也像你使用从其它库的类型一样。 +This works the same for your own user types in your program as well as types you +might be using from another library. + ## 细节 Scala 编译器从 `@main` 方法 `f` 生成程序,如下所示: From 6850715b5c5ed513e83b1788862f23e82375dd6f Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Tue, 27 Feb 2024 09:35:59 +0800 Subject: [PATCH 2/2] correct translation. Deleted English sentences. --- _zh-cn/overviews/scala3-book/methods-main-methods.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/methods-main-methods.md b/_zh-cn/overviews/scala3-book/methods-main-methods.md index 0a35fd27a2..1b185854b1 100644 --- a/_zh-cn/overviews/scala3-book/methods-main-methods.md +++ b/_zh-cn/overviews/scala3-book/methods-main-methods.md @@ -95,10 +95,6 @@ Illegal command line: java.lang.NumberFormatException: For input string: "sixty" 正如上面指出的,编译器为参数类型寻找 `scala.util.CommandLineParser.FromString` 类型类 的given 实例。 例如,我们自定义了一个 `Color`类型,并希望当以参数使用。你可以像以下代码这样使用: -As mentioned up above, the compiler looks for a given instance of the -`scala.util.CommandLineParser.FromString` typeclass for the type of the -argument. For example, let's say you have a custom `Color` type that you want to -use as a parameter. You would do this like you see below: {% tabs method_3 %} {% tab 'Scala 3 Only' for=method_3 %} @@ -117,9 +113,7 @@ given ComamndLineParser.FromString[Color] with {% endtab %} {% endtabs %} -这像你在程序中用的自己的用户类型一样,也像你使用从其它库的类型一样。 -This works the same for your own user types in your program as well as types you -might be using from another library. +这对于您程序中的自定义类型以及可能使用的其他库中的类型,都是一样的。 ## 细节