From 2168ef94fdd8157da3e0b7be87afa65b13e648ca Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 27 Oct 2023 20:03:15 +0800 Subject: [PATCH 01/10] modified node number of each file. add string-interacting.md --- .../scala3-book/string-interpolation.md | 2 +- .../scala3-book/ca-context-bounds.md | 2 +- .../scala3-book/ca-context-parameters.md | 2 +- .../ca-contextual-abstractions-intro.md | 2 +- .../scala3-book/ca-extension-methods.md | 2 +- .../overviews/scala3-book/ca-given-imports.md | 2 +- .../scala3-book/ca-implicit-conversions.md | 2 +- .../scala3-book/ca-multiversal-equality.md | 2 +- _zh-cn/overviews/scala3-book/ca-summary.md | 2 +- .../overviews/scala3-book/ca-type-classes.md | 2 +- .../scala3-book/collections-classes.md | 2 +- .../scala3-book/collections-intro.md | 2 +- .../scala3-book/collections-methods.md | 2 +- .../scala3-book/collections-summary.md | 2 +- _zh-cn/overviews/scala3-book/concurrency.md | 2 +- .../scala3-book/control-structures.md | 4 +- .../scala3-book/domain-modeling-fp.md | 2 +- .../scala3-book/domain-modeling-intro.md | 4 +- .../scala3-book/domain-modeling-oop.md | 2 +- .../scala3-book/domain-modeling-tools.md | 2 +- .../fp-functional-error-handling.md | 2 +- .../scala3-book/fp-functions-are-values.md | 4 +- .../scala3-book/fp-immutable-values.md | 2 +- _zh-cn/overviews/scala3-book/fp-intro.md | 2 +- .../scala3-book/fp-pure-functions.md | 2 +- _zh-cn/overviews/scala3-book/fp-summary.md | 2 +- _zh-cn/overviews/scala3-book/fp-what-is-fp.md | 2 +- .../scala3-book/fun-anonymous-functions.md | 2 +- .../scala3-book/fun-eta-expansion.md | 2 +- .../scala3-book/fun-function-variables.md | 2 +- _zh-cn/overviews/scala3-book/fun-hofs.md | 2 +- _zh-cn/overviews/scala3-book/fun-intro.md | 2 +- _zh-cn/overviews/scala3-book/fun-summary.md | 2 +- .../scala3-book/fun-write-map-function.md | 2 +- .../fun-write-method-returns-function.md | 2 +- .../scala3-book/interacting-with-java.md | 2 +- _zh-cn/overviews/scala3-book/methods-intro.md | 2 +- .../scala3-book/methods-main-methods.md | 2 +- _zh-cn/overviews/scala3-book/methods-most.md | 2 +- .../overviews/scala3-book/methods-summary.md | 2 +- .../scala3-book/packaging-imports.md | 2 +- .../scala3-book/scala-for-java-devs.md | 2 +- .../scala3-book/scala-for-javascript-devs.md | 2 +- .../scala3-book/scala-for-python-devs.md | 2 +- _zh-cn/overviews/scala3-book/scala-tools.md | 2 +- .../scala3-book/string-interpolation.md | 356 ++++++++++++++++++ _zh-cn/overviews/scala3-book/tools-sbt.md | 2 +- .../overviews/scala3-book/tools-worksheets.md | 2 +- .../overviews/scala3-book/types-adts-gadts.md | 2 +- .../scala3-book/types-dependent-function.md | 2 +- .../overviews/scala3-book/types-generics.md | 2 +- .../overviews/scala3-book/types-inferred.md | 2 +- .../scala3-book/types-intersection.md | 2 +- .../scala3-book/types-introduction.md | 2 +- .../scala3-book/types-opaque-types.md | 2 +- _zh-cn/overviews/scala3-book/types-others.md | 2 +- .../overviews/scala3-book/types-structural.md | 2 +- _zh-cn/overviews/scala3-book/types-union.md | 2 +- .../overviews/scala3-book/types-variance.md | 2 +- _zh-cn/overviews/scala3-book/where-next.md | 2 +- 60 files changed, 417 insertions(+), 63 deletions(-) create mode 100644 _zh-cn/overviews/scala3-book/string-interpolation.md diff --git a/_overviews/scala3-book/string-interpolation.md b/_overviews/scala3-book/string-interpolation.md index cf68d4eaf..2e47a872b 100644 --- a/_overviews/scala3-book/string-interpolation.md +++ b/_overviews/scala3-book/string-interpolation.md @@ -2,7 +2,7 @@ title: String Interpolation type: chapter description: This page provides more information about creating strings and using string interpolation. -languages: [ru] +languages: [ru, zh-cn] num: 18 previous-page: first-look-at-types next-page: control-structures diff --git a/_zh-cn/overviews/scala3-book/ca-context-bounds.md b/_zh-cn/overviews/scala3-book/ca-context-bounds.md index 9e12eb219..9e9c84238 100644 --- a/_zh-cn/overviews/scala3-book/ca-context-bounds.md +++ b/_zh-cn/overviews/scala3-book/ca-context-bounds.md @@ -3,7 +3,7 @@ title: 上下文绑定 type: section description: This page demonstrates Context Bounds in Scala 3. language: zh-cn -num: 61 +num: 62 previous-page: ca-context-parameters next-page: ca-given-imports diff --git a/_zh-cn/overviews/scala3-book/ca-context-parameters.md b/_zh-cn/overviews/scala3-book/ca-context-parameters.md index c562e5d16..3742d36de 100644 --- a/_zh-cn/overviews/scala3-book/ca-context-parameters.md +++ b/_zh-cn/overviews/scala3-book/ca-context-parameters.md @@ -3,7 +3,7 @@ title: Given 实例和 Using 语句 type: section description: This page demonstrates how to use 'given' instances and 'using' clauses in Scala 3. language: zh-cn -num: 60 +num: 61 previous-page: ca-extension-methods next-page: ca-context-bounds diff --git a/_zh-cn/overviews/scala3-book/ca-contextual-abstractions-intro.md b/_zh-cn/overviews/scala3-book/ca-contextual-abstractions-intro.md index d8f0f1a4d..4b6a82db3 100644 --- a/_zh-cn/overviews/scala3-book/ca-contextual-abstractions-intro.md +++ b/_zh-cn/overviews/scala3-book/ca-contextual-abstractions-intro.md @@ -3,7 +3,7 @@ title: 上下文抽象 type: chapter description: This chapter provides an introduction to the Scala 3 concept of Contextual Abstractions. language: zh-cn -num: 58 +num: 59 previous-page: types-others next-page: ca-extension-methods diff --git a/_zh-cn/overviews/scala3-book/ca-extension-methods.md b/_zh-cn/overviews/scala3-book/ca-extension-methods.md index c0afeaa0b..be1237733 100644 --- a/_zh-cn/overviews/scala3-book/ca-extension-methods.md +++ b/_zh-cn/overviews/scala3-book/ca-extension-methods.md @@ -3,7 +3,7 @@ title: 扩展方法 type: section description: This page demonstrates how Extension Methods work in Scala 3. language: zh-cn -num: 59 +num: 60 previous-page: ca-contextual-abstractions-intro next-page: ca-context-parameters diff --git a/_zh-cn/overviews/scala3-book/ca-given-imports.md b/_zh-cn/overviews/scala3-book/ca-given-imports.md index faf1e8569..ba56af52f 100644 --- a/_zh-cn/overviews/scala3-book/ca-given-imports.md +++ b/_zh-cn/overviews/scala3-book/ca-given-imports.md @@ -3,7 +3,7 @@ title: Given 导入 type: section description: This page demonstrates how 'given' import statements work in Scala 3. language: zh-cn -num: 62 +num: 63 previous-page: ca-context-bounds next-page: ca-type-classes diff --git a/_zh-cn/overviews/scala3-book/ca-implicit-conversions.md b/_zh-cn/overviews/scala3-book/ca-implicit-conversions.md index 69a12cba3..5ab40c806 100644 --- a/_zh-cn/overviews/scala3-book/ca-implicit-conversions.md +++ b/_zh-cn/overviews/scala3-book/ca-implicit-conversions.md @@ -3,7 +3,7 @@ title: 隐式转换 type: section description: This page demonstrates how to implement Implicit Conversions in Scala 3. language: zh-cn -num: 65 +num: 66 previous-page: ca-multiversal-equality next-page: ca-summary diff --git a/_zh-cn/overviews/scala3-book/ca-multiversal-equality.md b/_zh-cn/overviews/scala3-book/ca-multiversal-equality.md index aff8f1d53..9c6d2f422 100644 --- a/_zh-cn/overviews/scala3-book/ca-multiversal-equality.md +++ b/_zh-cn/overviews/scala3-book/ca-multiversal-equality.md @@ -3,7 +3,7 @@ title: 多元相等性 type: section description: This page demonstrates how to implement Multiversal Equality in Scala 3. language: zh-cn -num: 64 +num: 65 previous-page: ca-type-classes next-page: ca-implicit-conversions diff --git a/_zh-cn/overviews/scala3-book/ca-summary.md b/_zh-cn/overviews/scala3-book/ca-summary.md index c492e8f99..60b1fd01a 100644 --- a/_zh-cn/overviews/scala3-book/ca-summary.md +++ b/_zh-cn/overviews/scala3-book/ca-summary.md @@ -3,7 +3,7 @@ title: 总结 type: section description: This page provides a summary of the Contextual Abstractions lessons. language: zh-cn -num: 66 +num: 67 previous-page: ca-implicit-conversions next-page: concurrency diff --git a/_zh-cn/overviews/scala3-book/ca-type-classes.md b/_zh-cn/overviews/scala3-book/ca-type-classes.md index 869d104ad..7adffa059 100644 --- a/_zh-cn/overviews/scala3-book/ca-type-classes.md +++ b/_zh-cn/overviews/scala3-book/ca-type-classes.md @@ -3,7 +3,7 @@ title: 实现类型类 type: section description: This page demonstrates how to create and use type classes in Scala 3. language: zh-cn -num: 63 +num: 64 previous-page: ca-given-imports next-page: ca-multiversal-equality diff --git a/_zh-cn/overviews/scala3-book/collections-classes.md b/_zh-cn/overviews/scala3-book/collections-classes.md index c102860b5..47ccd58bb 100644 --- a/_zh-cn/overviews/scala3-book/collections-classes.md +++ b/_zh-cn/overviews/scala3-book/collections-classes.md @@ -3,7 +3,7 @@ title: 集合类型 type: section description: This page introduces the common Scala 3 collections types and some of their methods. language: zh-cn -num: 37 +num: 38 previous-page: collections-intro next-page: collections-methods diff --git a/_zh-cn/overviews/scala3-book/collections-intro.md b/_zh-cn/overviews/scala3-book/collections-intro.md index 6d21b2f59..0b2e1b77f 100644 --- a/_zh-cn/overviews/scala3-book/collections-intro.md +++ b/_zh-cn/overviews/scala3-book/collections-intro.md @@ -3,7 +3,7 @@ title: Scala 集合 type: chapter description: This page provides and introduction to the common collections classes and their methods in Scala 3. language: zh-cn -num: 36 +num: 37 previous-page: packaging-imports next-page: collections-classes diff --git a/_zh-cn/overviews/scala3-book/collections-methods.md b/_zh-cn/overviews/scala3-book/collections-methods.md index 47cc4338a..898619d14 100644 --- a/_zh-cn/overviews/scala3-book/collections-methods.md +++ b/_zh-cn/overviews/scala3-book/collections-methods.md @@ -3,7 +3,7 @@ title: 集合方法 type: section description: This page demonstrates the common methods on the Scala 3 collections classes. language: zh-cn -num: 38 +num: 39 previous-page: collections-classes next-page: collections-summary diff --git a/_zh-cn/overviews/scala3-book/collections-summary.md b/_zh-cn/overviews/scala3-book/collections-summary.md index 465f339f1..8a0c95871 100644 --- a/_zh-cn/overviews/scala3-book/collections-summary.md +++ b/_zh-cn/overviews/scala3-book/collections-summary.md @@ -3,7 +3,7 @@ title: 总结 type: section description: This page provides a summary of the Collections chapter. language: zh-cn -num: 39 +num: 40 previous-page: collections-methods next-page: fp-intro diff --git a/_zh-cn/overviews/scala3-book/concurrency.md b/_zh-cn/overviews/scala3-book/concurrency.md index 1906f158a..169e1f168 100644 --- a/_zh-cn/overviews/scala3-book/concurrency.md +++ b/_zh-cn/overviews/scala3-book/concurrency.md @@ -3,7 +3,7 @@ title: 并发 type: chapter description: This page discusses how Scala concurrency works, with an emphasis on Scala Futures. language: zh-cn -num: 67 +num: 68 previous-page: ca-summary next-page: scala-tools diff --git a/_zh-cn/overviews/scala3-book/control-structures.md b/_zh-cn/overviews/scala3-book/control-structures.md index 7babcabc6..fa46f25a2 100644 --- a/_zh-cn/overviews/scala3-book/control-structures.md +++ b/_zh-cn/overviews/scala3-book/control-structures.md @@ -3,8 +3,8 @@ title: 控制结构 type: chapter description: This page provides an introduction to Scala's control structures, including if/then/else, 'for' loops, 'for' expressions, 'match' expressions, try/catch/finally, and 'while' loops. language: zh-cn -num: 18 -previous-page: first-look-at-types +num: 19 +previous-page: string-interpolation next-page: domain-modeling-intro partof: scala3-book diff --git a/_zh-cn/overviews/scala3-book/domain-modeling-fp.md b/_zh-cn/overviews/scala3-book/domain-modeling-fp.md index 2e500de05..46797dfc7 100644 --- a/_zh-cn/overviews/scala3-book/domain-modeling-fp.md +++ b/_zh-cn/overviews/scala3-book/domain-modeling-fp.md @@ -3,7 +3,7 @@ title: 函数式领域建模 type: section description: This chapter provides an introduction to FP domain modeling with Scala 3. language: zh-cn -num: 22 +num: 23 previous-page: domain-modeling-oop next-page: methods-intro diff --git a/_zh-cn/overviews/scala3-book/domain-modeling-intro.md b/_zh-cn/overviews/scala3-book/domain-modeling-intro.md index db31d9c52..f2d91f71a 100644 --- a/_zh-cn/overviews/scala3-book/domain-modeling-intro.md +++ b/_zh-cn/overviews/scala3-book/domain-modeling-intro.md @@ -1,9 +1,7 @@ --- title: 领域建模 -type: chapter description: This chapter provides an introduction to domain modeling in Scala 3. -language: zh-cn -num: 19 +num: 20 previous-page: control-structures next-page: domain-modeling-tools diff --git a/_zh-cn/overviews/scala3-book/domain-modeling-oop.md b/_zh-cn/overviews/scala3-book/domain-modeling-oop.md index 48ef01495..0c71a3f65 100644 --- a/_zh-cn/overviews/scala3-book/domain-modeling-oop.md +++ b/_zh-cn/overviews/scala3-book/domain-modeling-oop.md @@ -3,7 +3,7 @@ title: OOP 领域建模 type: section description: This chapter provides an introduction to OOP domain modeling with Scala 3. language: zh-cn -num: 21 +num: 22 previous-page: domain-modeling-tools next-page: domain-modeling-fp diff --git a/_zh-cn/overviews/scala3-book/domain-modeling-tools.md b/_zh-cn/overviews/scala3-book/domain-modeling-tools.md index b14c9caad..08aa972f0 100644 --- a/_zh-cn/overviews/scala3-book/domain-modeling-tools.md +++ b/_zh-cn/overviews/scala3-book/domain-modeling-tools.md @@ -3,7 +3,7 @@ title: 工具 type: section description: This chapter provides an introduction to the available domain modeling tools in Scala 3, including classes, traits, enums, and more. language: zh-cn -num: 20 +num: 21 previous-page: domain-modeling-intro next-page: domain-modeling-oop diff --git a/_zh-cn/overviews/scala3-book/fp-functional-error-handling.md b/_zh-cn/overviews/scala3-book/fp-functional-error-handling.md index a58c8a463..b1cce269a 100644 --- a/_zh-cn/overviews/scala3-book/fp-functional-error-handling.md +++ b/_zh-cn/overviews/scala3-book/fp-functional-error-handling.md @@ -3,7 +3,7 @@ title: 函数式错误处理 type: section description: This section provides an introduction to functional error handling in Scala 3. language: zh-cn -num: 45 +num: 46 previous-page: fp-functions-are-values next-page: fp-summary diff --git a/_zh-cn/overviews/scala3-book/fp-functions-are-values.md b/_zh-cn/overviews/scala3-book/fp-functions-are-values.md index a157a8f6d..5c82bf83c 100644 --- a/_zh-cn/overviews/scala3-book/fp-functions-are-values.md +++ b/_zh-cn/overviews/scala3-book/fp-functions-are-values.md @@ -3,8 +3,8 @@ title: 函数是值 type: section description: This section looks at the use of functions as values in functional programming. language: zh-cn -num: 44 -previous-page: fp-pure-functions +num: 45 +previors-page: fp-pure-functions next-page: fp-functional-error-handling partof: scala3-book diff --git a/_zh-cn/overviews/scala3-book/fp-immutable-values.md b/_zh-cn/overviews/scala3-book/fp-immutable-values.md index 5e4566bf9..1d6b474da 100644 --- a/_zh-cn/overviews/scala3-book/fp-immutable-values.md +++ b/_zh-cn/overviews/scala3-book/fp-immutable-values.md @@ -3,7 +3,7 @@ title: 不可变值 type: section description: This section looks at the use of immutable values in functional programming. language: zh-cn -num: 42 +num: 43 previous-page: fp-what-is-fp next-page: fp-pure-functions diff --git a/_zh-cn/overviews/scala3-book/fp-intro.md b/_zh-cn/overviews/scala3-book/fp-intro.md index f0862626d..8b2563b97 100644 --- a/_zh-cn/overviews/scala3-book/fp-intro.md +++ b/_zh-cn/overviews/scala3-book/fp-intro.md @@ -3,7 +3,7 @@ title: 函数式编程 type: chapter description: This chapter provides an introduction to functional programming in Scala 3. language: zh-cn -num: 40 +num: 41 previous-page: collections-summary next-page: fp-what-is-fp diff --git a/_zh-cn/overviews/scala3-book/fp-pure-functions.md b/_zh-cn/overviews/scala3-book/fp-pure-functions.md index b987b9127..25fa4398f 100644 --- a/_zh-cn/overviews/scala3-book/fp-pure-functions.md +++ b/_zh-cn/overviews/scala3-book/fp-pure-functions.md @@ -3,7 +3,7 @@ title: 纯函数 type: section description: This section looks at the use of pure functions in functional programming. language: zh-cn -num: 43 +num: 44 previous-page: fp-immutable-values next-page: fp-functions-are-values diff --git a/_zh-cn/overviews/scala3-book/fp-summary.md b/_zh-cn/overviews/scala3-book/fp-summary.md index f9c4dfce8..459b2d6f8 100644 --- a/_zh-cn/overviews/scala3-book/fp-summary.md +++ b/_zh-cn/overviews/scala3-book/fp-summary.md @@ -3,7 +3,7 @@ title: 总结 type: section description: This section summarizes the previous functional programming sections. language: zh-cn -num: 46 +num: 47 previous-page: fp-functional-error-handling next-page: types-introduction diff --git a/_zh-cn/overviews/scala3-book/fp-what-is-fp.md b/_zh-cn/overviews/scala3-book/fp-what-is-fp.md index 6411b55c4..79370d450 100644 --- a/_zh-cn/overviews/scala3-book/fp-what-is-fp.md +++ b/_zh-cn/overviews/scala3-book/fp-what-is-fp.md @@ -3,7 +3,7 @@ title: 什么是函数式编程? type: section description: This section provides an answer to the question, what is functional programming? language: zh-cn -num: 41 +num: 42 previous-page: fp-intro next-page: fp-immutable-values diff --git a/_zh-cn/overviews/scala3-book/fun-anonymous-functions.md b/_zh-cn/overviews/scala3-book/fun-anonymous-functions.md index 9b2c3c3dd..ec08a19e0 100644 --- a/_zh-cn/overviews/scala3-book/fun-anonymous-functions.md +++ b/_zh-cn/overviews/scala3-book/fun-anonymous-functions.md @@ -3,7 +3,7 @@ title: 匿名函数 type: section description: This page shows how to use anonymous functions in Scala, including examples with the List class 'map' and 'filter' functions. language: zh-cn -num: 28 +num: 29 previous-page: fun-intro next-page: fun-function-variables diff --git a/_zh-cn/overviews/scala3-book/fun-eta-expansion.md b/_zh-cn/overviews/scala3-book/fun-eta-expansion.md index 8e169b4c2..562ed0074 100644 --- a/_zh-cn/overviews/scala3-book/fun-eta-expansion.md +++ b/_zh-cn/overviews/scala3-book/fun-eta-expansion.md @@ -3,7 +3,7 @@ title: Eta 扩展 type: section description: This page discusses Eta Expansion, the Scala technology that automatically and transparently converts methods into functions. language: zh-cn -num: 30 +num: 31 previous-page: fun-function-variables next-page: fun-hofs diff --git a/_zh-cn/overviews/scala3-book/fun-function-variables.md b/_zh-cn/overviews/scala3-book/fun-function-variables.md index 5df4d0753..e4591ef2e 100644 --- a/_zh-cn/overviews/scala3-book/fun-function-variables.md +++ b/_zh-cn/overviews/scala3-book/fun-function-variables.md @@ -3,7 +3,7 @@ title: 函数变量 type: section description: This page shows how to use anonymous functions in Scala, including examples with the List class 'map' and 'filter' functions. language: zh-cn -num: 29 +num: 30 previous-page: fun-anonymous-functions next-page: fun-eta-expansion diff --git a/_zh-cn/overviews/scala3-book/fun-hofs.md b/_zh-cn/overviews/scala3-book/fun-hofs.md index d0fbaa358..a0fda317b 100644 --- a/_zh-cn/overviews/scala3-book/fun-hofs.md +++ b/_zh-cn/overviews/scala3-book/fun-hofs.md @@ -3,7 +3,7 @@ title: 高阶函数 type: section description: This page demonstrates how to create and use higher-order functions in Scala. language: zh-cn -num: 31 +num: 32 previous-page: fun-eta-expansion next-page: fun-write-map-function diff --git a/_zh-cn/overviews/scala3-book/fun-intro.md b/_zh-cn/overviews/scala3-book/fun-intro.md index 4bc7aceab..90d45c438 100644 --- a/_zh-cn/overviews/scala3-book/fun-intro.md +++ b/_zh-cn/overviews/scala3-book/fun-intro.md @@ -3,7 +3,7 @@ title: 函数 type: chapter description: This chapter looks at all topics related to functions in Scala 3. language: zh-cn -num: 27 +num: 28 previous-page: methods-summary next-page: fun-anonymous-functions diff --git a/_zh-cn/overviews/scala3-book/fun-summary.md b/_zh-cn/overviews/scala3-book/fun-summary.md index f5bcecaaa..efc747c7c 100644 --- a/_zh-cn/overviews/scala3-book/fun-summary.md +++ b/_zh-cn/overviews/scala3-book/fun-summary.md @@ -3,7 +3,7 @@ title: 总结 type: section description: This page shows how to use anonymous functions in Scala, including examples with the List class 'map' and 'filter' functions. language: zh-cn -num: 34 +num: 35 previous-page: fun-write-method-returns-function next-page: packaging-imports diff --git a/_zh-cn/overviews/scala3-book/fun-write-map-function.md b/_zh-cn/overviews/scala3-book/fun-write-map-function.md index b3eb86e24..3ba7e44c1 100644 --- a/_zh-cn/overviews/scala3-book/fun-write-map-function.md +++ b/_zh-cn/overviews/scala3-book/fun-write-map-function.md @@ -3,7 +3,7 @@ title: 自定义 map 函数 type: section description: This page demonstrates how to create and use higher-order functions in Scala. language: zh-cn -num: 32 +num: 33 previous-page: fun-hofs next-page: fun-write-method-returns-function diff --git a/_zh-cn/overviews/scala3-book/fun-write-method-returns-function.md b/_zh-cn/overviews/scala3-book/fun-write-method-returns-function.md index 25647e6ca..84e3460b6 100644 --- a/_zh-cn/overviews/scala3-book/fun-write-method-returns-function.md +++ b/_zh-cn/overviews/scala3-book/fun-write-method-returns-function.md @@ -3,7 +3,7 @@ title: 创建可以返回函数的方法 type: section description: This page demonstrates how to create and use higher-order functions in Scala. language: zh-cn -num: 33 +num: 34 previous-page: fun-write-map-function next-page: fun-summary diff --git a/_zh-cn/overviews/scala3-book/interacting-with-java.md b/_zh-cn/overviews/scala3-book/interacting-with-java.md index 12682a930..f2263d0bd 100644 --- a/_zh-cn/overviews/scala3-book/interacting-with-java.md +++ b/_zh-cn/overviews/scala3-book/interacting-with-java.md @@ -3,7 +3,7 @@ title: 与 Java 交互 type: chapter description: This page demonstrates how Scala code can interact with Java, and how Java code can interact with Scala code. language: zh-cn -num: 71 +num: 72 previous-page: tools-worksheets next-page: scala-for-java-devs diff --git a/_zh-cn/overviews/scala3-book/methods-intro.md b/_zh-cn/overviews/scala3-book/methods-intro.md index 4c72c7e54..ec383b437 100644 --- a/_zh-cn/overviews/scala3-book/methods-intro.md +++ b/_zh-cn/overviews/scala3-book/methods-intro.md @@ -3,7 +3,7 @@ title: 方法 type: chapter description: This section introduces methods in Scala 3. language: zh-cn -num: 23 +num: 24 previous-page: domain-modeling-fp next-page: methods-most diff --git a/_zh-cn/overviews/scala3-book/methods-main-methods.md b/_zh-cn/overviews/scala3-book/methods-main-methods.md index f0b43aab6..60ffd9c02 100644 --- a/_zh-cn/overviews/scala3-book/methods-main-methods.md +++ b/_zh-cn/overviews/scala3-book/methods-main-methods.md @@ -3,7 +3,7 @@ title: main 方法 type: section description: This page describes how 'main' methods and the '@main' annotation work in Scala 3. language: zh-cn -num: 25 +num: 26 previous-page: methods-most next-page: methods-summary diff --git a/_zh-cn/overviews/scala3-book/methods-most.md b/_zh-cn/overviews/scala3-book/methods-most.md index 08991419b..86d78f796 100644 --- a/_zh-cn/overviews/scala3-book/methods-most.md +++ b/_zh-cn/overviews/scala3-book/methods-most.md @@ -3,7 +3,7 @@ title: 方法特性 type: section description: This section introduces Scala 3 methods, including main methods, extension methods, and more. language: zh-cn -num: 24 +num: 25 previous-page: methods-intro next-page: methods-main-methods diff --git a/_zh-cn/overviews/scala3-book/methods-summary.md b/_zh-cn/overviews/scala3-book/methods-summary.md index d406f100c..472c6230a 100644 --- a/_zh-cn/overviews/scala3-book/methods-summary.md +++ b/_zh-cn/overviews/scala3-book/methods-summary.md @@ -3,7 +3,7 @@ title: 总结 type: section description: This section summarizes the previous sections on Scala 3 methods. language: zh-cn -num: 26 +num: 27 previous-page: methods-main-methods next-page: fun-intro diff --git a/_zh-cn/overviews/scala3-book/packaging-imports.md b/_zh-cn/overviews/scala3-book/packaging-imports.md index b85798e93..bc928dbe6 100644 --- a/_zh-cn/overviews/scala3-book/packaging-imports.md +++ b/_zh-cn/overviews/scala3-book/packaging-imports.md @@ -3,7 +3,7 @@ title: 打包和导入 type: chapter description: A discussion of using packages and imports to organize your code, build related modules of code, control scope, and help prevent namespace collisions. language: zh-cn -num: 35 +num: 36 previous-page: fun-summary next-page: collections-intro diff --git a/_zh-cn/overviews/scala3-book/scala-for-java-devs.md b/_zh-cn/overviews/scala3-book/scala-for-java-devs.md index dc620ce53..c0072a95d 100644 --- a/_zh-cn/overviews/scala3-book/scala-for-java-devs.md +++ b/_zh-cn/overviews/scala3-book/scala-for-java-devs.md @@ -3,7 +3,7 @@ title: 向 Java 开发者介绍Scala type: chapter description: This page is for Java developers who are interested in learning about Scala 3. language: zh-cn -num: 72 +num: 73 previous-page: interacting-with-java next-page: scala-for-javascript-devs diff --git a/_zh-cn/overviews/scala3-book/scala-for-javascript-devs.md b/_zh-cn/overviews/scala3-book/scala-for-javascript-devs.md index c94405f47..0ec0816ee 100644 --- a/_zh-cn/overviews/scala3-book/scala-for-javascript-devs.md +++ b/_zh-cn/overviews/scala3-book/scala-for-javascript-devs.md @@ -3,7 +3,7 @@ title: Scala for JavaScript Developers type: chapter description: This chapter provides an introduction to Scala 3 for JavaScript developers language: zh-cn -num: 73 +num: 74 previous-page: scala-for-java-devs next-page: scala-for-python-devs diff --git a/_zh-cn/overviews/scala3-book/scala-for-python-devs.md b/_zh-cn/overviews/scala3-book/scala-for-python-devs.md index fe53fd6b1..618f8de44 100644 --- a/_zh-cn/overviews/scala3-book/scala-for-python-devs.md +++ b/_zh-cn/overviews/scala3-book/scala-for-python-devs.md @@ -3,7 +3,7 @@ title: Scala for Python Developers type: chapter description: This page is for Python developers who are interested in learning about Scala 3. language: zh-cn -num: 74 +num: 75 previous-page: scala-for-javascript-devs next-page: where-next diff --git a/_zh-cn/overviews/scala3-book/scala-tools.md b/_zh-cn/overviews/scala3-book/scala-tools.md index d773d0f83..822c3d428 100644 --- a/_zh-cn/overviews/scala3-book/scala-tools.md +++ b/_zh-cn/overviews/scala3-book/scala-tools.md @@ -3,7 +3,7 @@ title: Scala 工具 type: chapter description: This chapter looks at two commonly-used Scala tools, sbt and ScalaTest. language: zh-cn -num: 68 +num: 69 previous-page: concurrency next-page: tools-sbt diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md new file mode 100644 index 000000000..79c553b6f --- /dev/null +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -0,0 +1,356 @@ +--- +title: 字符串插值 +type: chapter +description: This page provides more information about creating strings and using 字符串插值. +languages: zh-cn +num: 18 +previous-page: first-look-at-types +next-page: control-structures +redirect_from: + - /overviews/core/string-interpolation.html + +partof: scala3-book +overview-name: "Scala 3 — Book" +layout: multipage-overview +permalink: "/zh-cn/scala3/book/:title.html" +--- + +## 介绍 + +字符串插值提供了一种在字符串中使用变量的方法。 +比如: + +{% tabs example-1 %} +{% tab 'Scala 2 and 3' for=example-1 %} +```scala +val name = "James" +val age = 30 +println(s"$name is $age years old") // "James is 30 years old" +``` +{% endtab %} +{% endtabs %} + +字符串插值由在字符串引号前面的 `s` 和任何有前缀 `$` 的变量组成。 + +### 其它插值 + +把 `s` 放在字符串前,只是 Scala 提供的插值的一种可能。 + +Scala 内置三种字符串插值方法: `s`, `f` 和 raw`. +进一步,字符串插值器只是一种特殊的方法,所以你也可以定义自己的插值器。例如, +有些数据库的函数库定义了 `sql` 插值器,这个插值器可以返回数据库查询。 + +## `s` 插值器 (`s`-字符串) + +在任何字符串字面量加上 `s` 前缀,就可以让你在字符串中直接使用变量。你已经在这里见过这个例子: + +{% tabs example-2 %} +{% tab 'Scala 2 and 3' for=example-2 %} +```scala +val name = "James" +val age = 30 +println(s"$name is $age years old") // "James is 30 years old" +``` +{% endtab %} +{% endtabs %} + +这里字符串中的 `$name` 和 `$age` 占位符相应地被调用 `name.toString` 和 `age.toString` 的结果所替换。 +`s`-字符串可以获取当前作用域的所有变量。 + +虽然这看着很明显,但它很重要,需要在这指出来,字符串插值在普通字符串字面量中_不_起作用: + +{% tabs example-3 %} +{% tab 'Scala 2 and 3' for=example-3 %} +```scala +val name = "James" +val age = 30 +println("$name is $age years old") // "$name is $age years old" +``` +{% endtab %} +{% endtabs %} + +字符串插值器可以使用任何表达式。例如: + +{% tabs example-4 %} +{% tab 'Scala 2 and 3' for=example-4 %} +```scala +println(s"2 + 2 = ${2 + 2}") // "2 + 2 = 4" +val x = -1 +println(s"x.abs = ${x.abs}") // "x.abs = 1" +``` +{% endtab %} +{% endtabs %} + +任何表达式可以嵌入 `${}` 中. + +有些特殊字符在嵌入到字符串内时需要转义。 +当需要显示真实的美元符号时,你可以把美元符号双写 `$$`, 像这样: + +{% tabs example-5 %} +{% tab 'Scala 2 and 3' for=example-5 %} +```scala +println(s"New offers starting at $$14.99") // "New offers starting at $14.99" +``` +{% endtab %} +{% endtabs %} + +双引号一样需要转义。这个可以使用三引号来达到,如下: + +{% tabs example-6 %} +{% tab 'Scala 2 and 3' for=example-6 %} +```scala +println(s"""{"name":"James"}""") // `{"name":"James"}` +``` +{% endtab %} +{% endtabs %} + +最后,可以在所有多行字符串内插值 + +{% tabs example-7 %} +{% tab 'Scala 2 and 3' for=example-7 %} +```scala +println(s"""name: "$name", + |age: $age""".stripMargin) +``` + +This will print as follows: + +``` +name: "James" +age: 30 +``` +{% endtab %} +{% endtabs %} + +## `f` 插值器 (`f`-字符串) + +在任何字符串字面量加上 `f` 前缀,允许你创建简单的格式化字符串,像其它语言中的 `printf`。当使用 `f` 插值器时, +所有变量的引用应该遵循 `printf` 风格的格式化字符串,像 `%d`。让我们看以下例子: + +{% tabs example-8 %} +{% tab 'Scala 2 and 3' for=example-8 %} +```scala +val height = 1.9d +val name = "James" +println(f"$name%s is $height%2.2f meters tall") // "James is 1.90 meters tall" +``` +{% endtab %} +{% endtabs %} + +`f` 插值器是类型安全的。如果你尝试把一个双精度数传递给一个只能处理整数的格式化字符串,编译器会发出一个错误信息。 +例如: + +{% tabs f-inspector-error class=tabs-scala-version %} + +{% tab 'Scala 2' for=f-interpolator-error %} +```scala +val height: Double = 1.9d + +scala> f"$height%4d" +:9: error: type mismatch; + found : Double + required: Int + f"$height%4d" + ^ +``` +{% endtab %} + +{% tab 'Scala 3' for=f-interpolator-error %} +```scala +val height: Double = 1.9d + +scala> f"$height%4d" +-- Error: ---------------------------------------------------------------------- +1 |f"$height%4d" + | ^^^^^^ + | Found: (height : Double), Required: Int, Long, Byte, Short, BigInt +1 error found + +``` +{% endtab %} +{% endtabs %} + +`f` 插值器使用来自 Java 的字符串格式化工具。格式化允许在 `%` 字符后,在[Formatter javadoc][java-format-docs] 中有说明。 +如果没有 `%` 字符在变量之后,那么 `%s` (`String`) 作为缺省的格式化工具。 + +最后,像在 Java 里,使用 `%%` 来让 `%` 字符出现在输出字符中: + +{% tabs literal-percent %} +{% tab 'Scala 2 and 3' for=literal-percent %} +```scala +println(f"3/19 is less than 20%%") // "3/19 is less than 20%" +``` +{% endtab %} +{% endtabs %} + +### `raw` 插值器 + +raw 插值器 和 `s` 插值器很像,除了它不动字符串内的转义符。这里有一个处理字符串的例子: + +{% tabs example-9 %} +{% tab 'Scala 2 and 3' for=example-9 %} +```scala +scala> s"a\nb" +res0: String = +a +b +``` +{% endtab %} +{% endtabs %} + +这里 `s` 字符串插值器把 `\n` 替换成回车字符。`raw` 插值器不会做那些。 + +{% tabs example-10 %} +{% tab 'Scala 2 and 3' for=example-10 %} +```scala +scala> raw"a\nb" +res1: String = a\nb +``` +{% endtab %} +{% endtabs %} + +当你希望避免像 `\n` 这样的表达式转义成回车符,raw 插值器会有用。 + +除了这三种自带的字符串插值器外,你还可以定义自己的插值器。 + +## 高级用法 + +字面量 `s"Hi $name"` 被 Scala 当成 _过程_ 字符串字面量来进行分析。 +这意味着编译器对这个字面量额外做了一些工作。具体的处理后的字符串 +和字符串插入器可以在 [SIP-11][sip-11] 中找到描述,但这里用一个快速的例子来展示它 +是如何工作的。 + +### 定制插值器 + +在 Scala 中,所有被处理过的字符串字面量都是简单的代码转换。任何时候当编译器遇到 +形式如下,处理过的字符串字面量时: + +{% tabs example-11 %} +{% tab 'Scala 2 and 3' for=example-11 %} +```scala +id"string content" +``` +{% endtab %} +{% endtabs %} + +编译器把这段代码转换成 在 [StringContext](https://www.scala-lang.org/api/current/scala/StringContext.html) 实例之上调用 (`id`) 方法. +该方法也在隐式作用域有效。 +为了定义我们自己的字符串插值器,我们需要创建一个 implicit class (Scala 2) 或者一个 `extension` 方法(Scala 3)方法,这样可以把新方法添加到 `StringContext` 中。 + +作为一个小例子,让我们假定 `Point` 类,并假定我们想创建一个定制化的插值器,它把 `p"a,b"` into a 转换成 `Point` 对象。 + +{% tabs custom-interpolator-1 %} +{% tab 'Scala 2 and 3' for=custom-interpolator-1 %} +```scala +case class Point(x: Double, y: Double) + +val pt = p"1,-2" // Point(1.0,-2.0) +``` +{% endtab %} +{% endtabs %} + +我们首先实现一个如下的 `StringContext` 扩展来创建一个定制的 `p`-插值器: + +{% tabs custom-interpolator-2 class=tabs-scala-version %} + +{% tab 'Scala 2' for=custom-interpolator-2 %} +```scala +implicit class PointHelper(val sc: StringContext) extends AnyVal { + def p(args: Any*): Point = ??? +} +``` + +**注意:** 在 Scala 2.x 中重要的一点是继承自 `AnyVal` ,从而防止运行时对每个插值器进行实例化。 +更多内容见 [值类]({% link _overviews/core/value-classes.md %}) 文档。 + +{% endtab %} + +{% tab 'Scala 3' for=custom-interpolator-2 %} +```scala +extension (sc: StringContext) + def p(args: Any*): Point = ??? +``` +{% endtab %} + +{% endtabs %} + +一旦这个扩展在作用域,当 Scala 编译器遇到 `p"some string"` 时,它将 +`some string` 中每一个嵌入到字符串中的变量转换为字符串和表达式参数。 + +例如 `p"1, $someVar"` 将转变成: + +{% tabs extension-desugaring class=tabs-scala-version %} + +{% tab 'Scala 2' for=extension-desugaring %} +```scala +new StringContext("1, ", "").p(someVar) +``` + +隐式类将用来重写成以下的样子: + +```scala +new PointHelper(new StringContext("1, ", "")).p(someVar) +``` +{% endtab %} + +{% tab 'Scala 3' for=extension-desugaring %} +```scala +StringContext("1, ","").p(someVar) +``` + +{% endtab %} + +{% endtabs %} + +作为结果,每一个处理过的字符串片段都暴露在 `StringContext.parts` 成员内,而在字符串中的任何 +表达式的值都传递给该方法的 `args` 参数。 + +### 实现的例子 + +我们这个天真的 Point 插值器方法的实现看着像以下的代码, +但是更复杂的方法可能会用更加精确的控制用于处理字符串 `parts` 和 表达式 `args`, +这样可以替代目前重用 `s`-插值器。 + +{% tabs naive-implementation class=tabs-scala-version %} + +{% tab 'Scala 2' for=naive-implementation %} +```scala +implicit class PointHelper(val sc: StringContext) extends AnyVal { + def p(args: Double*): Point = { + // reuse the `s`-interpolator and then split on ',' + val pts = sc.s(args: _*).split(",", 2).map { _.toDoubleOption.getOrElse(0.0) } + Point(pts(0), pts(1)) + } +} + +val x=12.0 + +p"1, -2" // Point(1.0, -2.0) +p"${x/5}, $x" // Point(2.4, 12.0) +``` +{% endtab %} + +{% tab 'Scala 3' for=naive-implementation %} +```scala +extension (sc: StringContext) + def p(args: Double*): Point = { + // reuse the `s`-interpolator and then split on ',' + val pts = sc.s(args: _*).split(",", 2).map { _.toDoubleOption.getOrElse(0.0) } + Point(pts(0), pts(1)) + } + +val x=12.0 + +p"1, -2" // Point(1.0, -2.0) +p"${x/5}, $x" // Point(2.4, 12.0) +``` +{% endtab %} +{% endtabs %} + +虽然字符串插值器刚开始是用来创建某种字符串形式,但使用上面的定制插值器可以有强大的句法简写, +并且社区已经制造了一些语法便捷的用途,如 ANSI 终端颜色扩展,可执行 SQL 查询,神奇的 +`$"identifier"` 表达,还有更多其它的。 + +[java-format-docs]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#detail +[值类]: {% link _overviews/core/value-classes.md %} +[sip-11]: {% link _sips/sips/string-interpolation.md %} diff --git a/_zh-cn/overviews/scala3-book/tools-sbt.md b/_zh-cn/overviews/scala3-book/tools-sbt.md index 362de902b..3d4e78f4a 100644 --- a/_zh-cn/overviews/scala3-book/tools-sbt.md +++ b/_zh-cn/overviews/scala3-book/tools-sbt.md @@ -3,7 +3,7 @@ title: 使用 sbt 构建和测试 Scala 项目 type: section description: This section looks at a commonly-used build tool, sbt, and a testing library, ScalaTest. language: zh-cn -num: 69 +num: 70 previous-page: scala-tools next-page: tools-worksheets diff --git a/_zh-cn/overviews/scala3-book/tools-worksheets.md b/_zh-cn/overviews/scala3-book/tools-worksheets.md index 2e8270c99..214b744d8 100644 --- a/_zh-cn/overviews/scala3-book/tools-worksheets.md +++ b/_zh-cn/overviews/scala3-book/tools-worksheets.md @@ -3,7 +3,7 @@ title: worksheet type: section description: This section looks at worksheets, an alternative to Scala projects. language: zh-cn -num: 70 +num: 71 previous-page: tools-sbt next-page: interacting-with-java diff --git a/_zh-cn/overviews/scala3-book/types-adts-gadts.md b/_zh-cn/overviews/scala3-book/types-adts-gadts.md index 9d3e46933..4d1604e18 100644 --- a/_zh-cn/overviews/scala3-book/types-adts-gadts.md +++ b/_zh-cn/overviews/scala3-book/types-adts-gadts.md @@ -3,7 +3,7 @@ title: 代数数据类型 type: section description: This section introduces and demonstrates algebraic data types (ADTs) in Scala 3. language: zh-cn -num: 52 +num: 53 previous-page: types-union next-page: types-variance diff --git a/_zh-cn/overviews/scala3-book/types-dependent-function.md b/_zh-cn/overviews/scala3-book/types-dependent-function.md index fa5c8d8c8..37084f164 100644 --- a/_zh-cn/overviews/scala3-book/types-dependent-function.md +++ b/_zh-cn/overviews/scala3-book/types-dependent-function.md @@ -3,7 +3,7 @@ title: 依赖函数类型 type: section description: This section introduces and demonstrates dependent function types in Scala 3. language: zh-cn -num: 56 +num: 57 previous-page: types-structural next-page: types-others diff --git a/_zh-cn/overviews/scala3-book/types-generics.md b/_zh-cn/overviews/scala3-book/types-generics.md index 8e036c4b7..cdea2a2c4 100644 --- a/_zh-cn/overviews/scala3-book/types-generics.md +++ b/_zh-cn/overviews/scala3-book/types-generics.md @@ -3,7 +3,7 @@ title: 泛型 type: section description: This section introduces and demonstrates generics in Scala 3. language: zh-cn -num: 49 +num: 50 previous-page: types-inferred next-page: types-intersection diff --git a/_zh-cn/overviews/scala3-book/types-inferred.md b/_zh-cn/overviews/scala3-book/types-inferred.md index b524a03ed..aa6d3faf1 100644 --- a/_zh-cn/overviews/scala3-book/types-inferred.md +++ b/_zh-cn/overviews/scala3-book/types-inferred.md @@ -3,7 +3,7 @@ title: 类型推断 type: section description: This section introduces and demonstrates inferred types in Scala 3 language: zh-cn -num: 48 +num: 49 previous-page: types-introduction next-page: types-generics diff --git a/_zh-cn/overviews/scala3-book/types-intersection.md b/_zh-cn/overviews/scala3-book/types-intersection.md index c61548e16..db1d250a4 100644 --- a/_zh-cn/overviews/scala3-book/types-intersection.md +++ b/_zh-cn/overviews/scala3-book/types-intersection.md @@ -3,7 +3,7 @@ title: 相交类型 type: section description: This section introduces and demonstrates intersection types in Scala 3. language: zh-cn -num: 50 +num: 51 previous-page: types-generics next-page: types-union diff --git a/_zh-cn/overviews/scala3-book/types-introduction.md b/_zh-cn/overviews/scala3-book/types-introduction.md index e2e4df5b2..dfe1b6b79 100644 --- a/_zh-cn/overviews/scala3-book/types-introduction.md +++ b/_zh-cn/overviews/scala3-book/types-introduction.md @@ -3,7 +3,7 @@ title: 类型和类型系统 type: chapter description: This chapter provides an introduction to Scala 3 types and the type system. language: zh-cn -num: 47 +num: 48 previous-page: fp-summary next-page: types-inferred diff --git a/_zh-cn/overviews/scala3-book/types-opaque-types.md b/_zh-cn/overviews/scala3-book/types-opaque-types.md index f68f79864..c8ba8405f 100644 --- a/_zh-cn/overviews/scala3-book/types-opaque-types.md +++ b/_zh-cn/overviews/scala3-book/types-opaque-types.md @@ -3,7 +3,7 @@ title: 不透明类型 type: section description: This section introduces and demonstrates opaque types in Scala 3. language: zh-cn -num: 54 +num: 55 previous-page: types-variance next-page: types-structural diff --git a/_zh-cn/overviews/scala3-book/types-others.md b/_zh-cn/overviews/scala3-book/types-others.md index cb0910280..788814ea0 100644 --- a/_zh-cn/overviews/scala3-book/types-others.md +++ b/_zh-cn/overviews/scala3-book/types-others.md @@ -3,7 +3,7 @@ title: 其他类型 type: section description: This section mentions other advanced types in Scala 3. language: zh-cn -num: 57 +num: 58 previous-page: types-dependent-function next-page: ca-contextual-abstractions-intro diff --git a/_zh-cn/overviews/scala3-book/types-structural.md b/_zh-cn/overviews/scala3-book/types-structural.md index 694da0c63..4c12a8790 100644 --- a/_zh-cn/overviews/scala3-book/types-structural.md +++ b/_zh-cn/overviews/scala3-book/types-structural.md @@ -3,7 +3,7 @@ title: 结构化类型 type: section description: This section introduces and demonstrates structural types in Scala 3. language: zh-cn -num: 55 +num: 56 previous-page: types-opaque-types next-page: types-dependent-function diff --git a/_zh-cn/overviews/scala3-book/types-union.md b/_zh-cn/overviews/scala3-book/types-union.md index 51f61590f..4e1ca7924 100644 --- a/_zh-cn/overviews/scala3-book/types-union.md +++ b/_zh-cn/overviews/scala3-book/types-union.md @@ -3,7 +3,7 @@ title: 联合类型 type: section description: This section introduces and demonstrates union types in Scala 3. language: zh-cn -num: 51 +num: 52 previous-page: types-intersection next-page: types-adts-gadts diff --git a/_zh-cn/overviews/scala3-book/types-variance.md b/_zh-cn/overviews/scala3-book/types-variance.md index d6a682b40..3c774c1d5 100644 --- a/_zh-cn/overviews/scala3-book/types-variance.md +++ b/_zh-cn/overviews/scala3-book/types-variance.md @@ -3,7 +3,7 @@ title: 型变 type: section description: This section introduces and demonstrates variance in Scala 3. language: zh-cn -num: 53 +num: 54 previous-page: types-adts-gadts next-page: types-opaque-types diff --git a/_zh-cn/overviews/scala3-book/where-next.md b/_zh-cn/overviews/scala3-book/where-next.md index e1d6052b6..66c3afe63 100644 --- a/_zh-cn/overviews/scala3-book/where-next.md +++ b/_zh-cn/overviews/scala3-book/where-next.md @@ -3,7 +3,7 @@ title: 下一步去哪 type: chapter description: Where to go next after reading the Scala Book language: zh-cn -num: 75 +num: 76 previous-page: scala-for-python-devs next-page: From 084a409206598d8795ccd1783d4f41131d4e8db6 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 27 Oct 2023 20:48:30 +0800 Subject: [PATCH 02/10] correct tab label --- _zh-cn/overviews/scala3-book/string-interpolation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index 79c553b6f..5458ca9cb 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -36,7 +36,7 @@ println(s"$name is $age years old") // "James is 30 years old" 把 `s` 放在字符串前,只是 Scala 提供的插值的一种可能。 -Scala 内置三种字符串插值方法: `s`, `f` 和 raw`. +Scala 内置三种字符串插值方法: `s`, `f` 和 `raw`. 进一步,字符串插值器只是一种特殊的方法,所以你也可以定义自己的插值器。例如, 有些数据库的函数库定义了 `sql` 插值器,这个插值器可以返回数据库查询。 @@ -140,7 +140,7 @@ println(f"$name%s is $height%2.2f meters tall") // "James is 1.90 meters tall" `f` 插值器是类型安全的。如果你尝试把一个双精度数传递给一个只能处理整数的格式化字符串,编译器会发出一个错误信息。 例如: -{% tabs f-inspector-error class=tabs-scala-version %} +{% tabs f-interpolator-error class=tabs-scala-version %} {% tab 'Scala 2' for=f-interpolator-error %} ```scala From 203d90bdb5a4eb200bdc323d08cf41aeb117528c Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sun, 29 Oct 2023 21:37:14 +0800 Subject: [PATCH 03/10] correct link for "value-classes" --- _zh-cn/overviews/scala3-book/string-interpolation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index 5458ca9cb..cbe6f8d2b 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -261,7 +261,7 @@ implicit class PointHelper(val sc: StringContext) extends AnyVal { ``` **注意:** 在 Scala 2.x 中重要的一点是继承自 `AnyVal` ,从而防止运行时对每个插值器进行实例化。 -更多内容见 [值类]({% link _overviews/core/value-classes.md %}) 文档。 +更多内容见 [值类][value-classes] 文档。 {% endtab %} @@ -352,5 +352,5 @@ p"${x/5}, $x" // Point(2.4, 12.0) `$"identifier"` 表达,还有更多其它的。 [java-format-docs]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#detail -[值类]: {% link _overviews/core/value-classes.md %} +[value-classes]: {% link _overviews/core/value-classes.md %} [sip-11]: {% link _sips/sips/string-interpolation.md %} From 168bfc1bab2b4b42e55cabd9a8dabbe6deecea4b Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Mon, 13 Nov 2023 22:00:41 +0800 Subject: [PATCH 04/10] Update _zh-cn/overviews/scala3-book/string-interpolation.md Co-authored-by: Jamie Thompson --- _zh-cn/overviews/scala3-book/string-interpolation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index cbe6f8d2b..0a594b23c 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -7,7 +7,7 @@ num: 18 previous-page: first-look-at-types next-page: control-structures redirect_from: - - /overviews/core/string-interpolation.html + - /zh-cn/overviews/core/string-interpolation.html partof: scala3-book overview-name: "Scala 3 — Book" From 6d20113cd945d01d08498d881df4fa297780a75f Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sun, 14 Jan 2024 21:11:47 +0800 Subject: [PATCH 05/10] correct minor errors. --- _zh-cn/overviews/scala3-book/fp-functions-are-values.md | 2 +- _zh-cn/overviews/scala3-book/string-interpolation.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/fp-functions-are-values.md b/_zh-cn/overviews/scala3-book/fp-functions-are-values.md index 5c82bf83c..ab802ed02 100644 --- a/_zh-cn/overviews/scala3-book/fp-functions-are-values.md +++ b/_zh-cn/overviews/scala3-book/fp-functions-are-values.md @@ -4,7 +4,7 @@ type: section description: This section looks at the use of functions as values in functional programming. language: zh-cn num: 45 -previors-page: fp-pure-functions +previous-page: fp-pure-functions next-page: fp-functional-error-handling partof: scala3-book diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index 0a594b23c..cbe6f8d2b 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -7,7 +7,7 @@ num: 18 previous-page: first-look-at-types next-page: control-structures redirect_from: - - /zh-cn/overviews/core/string-interpolation.html + - /overviews/core/string-interpolation.html partof: scala3-book overview-name: "Scala 3 — Book" From 033b3f1956b97de39995bc44937cb7951b0c42a9 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sun, 14 Jan 2024 22:50:44 +0800 Subject: [PATCH 06/10] correct permalink in string-interpolation.md. --- _zh-cn/overviews/scala3-book/string-interpolation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index cbe6f8d2b..46c89a27f 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -12,7 +12,7 @@ redirect_from: partof: scala3-book overview-name: "Scala 3 — Book" layout: multipage-overview -permalink: "/zh-cn/scala3/book/:title.html" +permalink: "/scala3/book/:title.html" --- ## 介绍 From bd936fd11fe26364f1e267493a48a87faed8fc10 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sun, 14 Jan 2024 23:15:03 +0800 Subject: [PATCH 07/10] modified back again. --- _zh-cn/overviews/scala3-book/string-interpolation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index 46c89a27f..cbe6f8d2b 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -12,7 +12,7 @@ redirect_from: partof: scala3-book overview-name: "Scala 3 — Book" layout: multipage-overview -permalink: "/scala3/book/:title.html" +permalink: "/zh-cn/scala3/book/:title.html" --- ## 介绍 From 6a9557b84b063d092b126e7e8a28af6e12dd36d1 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Fri, 19 Jan 2024 16:33:54 +0100 Subject: [PATCH 08/10] Apply suggestions from code review fix links and redirect --- _zh-cn/overviews/scala3-book/string-interpolation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_zh-cn/overviews/scala3-book/string-interpolation.md b/_zh-cn/overviews/scala3-book/string-interpolation.md index cbe6f8d2b..372eccc5e 100644 --- a/_zh-cn/overviews/scala3-book/string-interpolation.md +++ b/_zh-cn/overviews/scala3-book/string-interpolation.md @@ -2,12 +2,12 @@ title: 字符串插值 type: chapter description: This page provides more information about creating strings and using 字符串插值. -languages: zh-cn +language: zh-cn num: 18 previous-page: first-look-at-types next-page: control-structures redirect_from: - - /overviews/core/string-interpolation.html + - /zh-cn/overviews/core/string-interpolation.html partof: scala3-book overview-name: "Scala 3 — Book" From 3c0c052c4528832d31cc86b609dcba5062cef4fd Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Fri, 19 Jan 2024 16:39:41 +0100 Subject: [PATCH 09/10] remove old zh-cn string interpolation page --- _zh-cn/overviews/core/string-interpolation.md | 119 ------------------ 1 file changed, 119 deletions(-) delete mode 100644 _zh-cn/overviews/core/string-interpolation.md diff --git a/_zh-cn/overviews/core/string-interpolation.md b/_zh-cn/overviews/core/string-interpolation.md deleted file mode 100644 index eab9e4a74..000000000 --- a/_zh-cn/overviews/core/string-interpolation.md +++ /dev/null @@ -1,119 +0,0 @@ ---- -layout: singlepage-overview -title: 字符串插值 - -partof: string-interpolation - -language: zh-cn ---- - -**Josh Suereth 著** - -## 简介 - -自2.10.0版本开始,Scala提供了一种新的机制来根据数据生成字符串:字符串插值。字符串插值允许使用者将变量引用直接插入处理过的字面字符中。如下例: - - val name="James" - println(s"Hello,$name")//Hello,James - -在上例中, s"Hello,$name" 是待处理字符串字面,编译器会对它做额外的工作。待处理字符串字面通过“号前的字符来标示(例如:上例中是s)。字符串插值的实现细节在 [SIP-11](https://docs.scala-lang.org/sips/pending/string-interpolation.html) 中有全面介绍。 - -## 用法 - -Scala 提供了三种创新的字符串插值方法:s,f 和 raw. - -### s 字符串插值器 - -在任何字符串前加上s,就可以直接在串中使用变量了。你已经见过这个例子: - - val name="James" - println(s"Hello,$name")//Hello,James -此例中,$name嵌套在一个将被s字符串插值器处理的字符串中。插值器知道在这个字符串的这个地方应该插入这个name变量的值,以使输出字符串为Hello,James。使用s插值器,在这个字符串中可以使用任何在处理范围内的名字。 - -字符串插值器也可以处理任意的表达式。例如: - - println(s"1+1=${1+1}") -将会输出字符串1+1=2。任何表达式都可以嵌入到${}中。 - -### f 插值器 - -在任何字符串字面前加上 f,就可以生成简单的格式化串,功能相似于其他语言中的 printf 函数。当使用 f 插值器的时候,所有的变量引用都应当后跟一个printf-style格式的字符串,如%d。看下面这个例子: - - val height=1.9d - val name="James" - println(f"$name%s is $height%2.2f meters tall")//James is 1.90 meters tall -f 插值器是类型安全的。如果试图向只支持 int 的格式化串传入一个double 值,编译器则会报错。例如: - - val height:Double=1.9d - - scala>f"$height%4d" - :9: error: type mismatch; - found : Double - required: Int - f"$height%4d" - ^ -f 插值器利用了java中的字符串数据格式。这种以%开头的格式在 [Formatter javadoc] 中有相关概述。如果在具体变量后没有%,则格式化程序默认使用 %s(串型)格式。 - -### raw 插值器 - -除了对字面值中的字符不做编码外,raw 插值器与 s 插值器在功能上是相同的。如下是个被处理过的字符串: - - scala>s"a\nb" - res0:String= - a - b -这里,s 插值器用回车代替了\n。而raw插值器却不会如此处理。 - - scala>raw"a\nb" - res1:String=a\nb -当不想输入\n被转换为回车的时候,raw 插值器是非常实用的。 - -除了以上三种字符串插值器外,使用者可以自定义插值器。 - -### 高级用法 - -在Scala中,所有处理过的字符串字面值都进行了简单编码转换。任何时候编译器遇到一个如下形式的字符串字面值: - - id"string content" -它都会被转换成一个StringContext实例的call(id)方法。这个方法在隐式范围内仍可用。只需要简单得 -建立一个隐类,给StringContext实例增加一个新方法,便可以定义我们自己的字符串插值器。如下例: - - //注意:为了避免运行时实例化,我们从AnyVal中继承。 - //更多信息请见值类的说明 - implicit class JsonHelper(val sc:StringContext) extends AnyVal{ - def json(args:Any*):JSONObject=sys.error("TODO-IMPLEMENT") - } - - def giveMeSomeJson(x:JSONObject):Unit=... - - giveMeSomeJson(json"{name:$name,id:$id}") -在这个例子中,我们试图通过字符串插值生成一个JSON文本语法。隐类 JsonHelper 作用域内使用该语法,且这个JSON方法需要一个完整的实现。只不过,字符串字面值格式化的结果不是一个字符串,而是一个JSON对象。 - -当编译器遇到"{name:$name,id:$id"}",它将会被重写成如下表达式: - - new StringContext("{name:",",id:","}").json(name,id) - -隐类则被重写成如下形式 - - new JsonHelper(new StringContext("{name:",",id:","}")).json(name,id) - -所以,JSON方法可以访问字符串的原生片段而每个表达式都是一个值。这个方法的一个简单但又令人迷惑的例子: - - implicit class JsonHelper(val sc:StringContext) extends AnyVal{ - def json(args:Any*):JSONObject={ - val strings=sc.parts.iterator - val expressions=args.iterator - var buf=new StringBuilder(strings.next()) - while(strings.hasNext){ - buf.append(expressions.next()) - buf.append(strings.next()) - } - parseJson(buf) - } - } - -被处理过的字符串的每部分都是StringContext的成员。每个表达式的值都将传入到JSON方法的args参数。JSON方法接受这些值并合成一个大字符串,然后再解析成JSON格式。有一种更复杂的实现可以避免合成字符串的操作,它只是简单的直接通过原生字符串和表达式值构建JSON。 - -## 限制 - -字符串插值目前对模式匹配语句不适用。此特性将在2.11版本中生效。 From b870861e24c5adf9cf2e06a54562a52783668c1d Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Fri, 19 Jan 2024 16:47:54 +0100 Subject: [PATCH 10/10] fix next page for first-look-at-types --- _zh-cn/overviews/scala3-book/first-look-at-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_zh-cn/overviews/scala3-book/first-look-at-types.md b/_zh-cn/overviews/scala3-book/first-look-at-types.md index a2c876654..0c8fb0974 100644 --- a/_zh-cn/overviews/scala3-book/first-look-at-types.md +++ b/_zh-cn/overviews/scala3-book/first-look-at-types.md @@ -5,7 +5,7 @@ description: This page provides a brief introduction to Scala's built-in data ty language: zh-cn num: 17 previous-page: taste-summary -next-page: control-structures +next-page: string-interpolation partof: scala3-book overview-name: "Scala 3 — Book"