From ba8ca8f64702f21d9c820fc60834933b9c9f81cd Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 17 Jan 2019 19:12:09 +0900 Subject: [PATCH 001/358] Japanese translation of TOUR OF SCALA introduction --- _data/translations.yml | 2 +- _ja/tour/tour-of-scala.md | 84 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 _ja/tour/tour-of-scala.md diff --git a/_data/translations.yml b/_data/translations.yml index 266b039905..56d0fd59cf 100644 --- a/_data/translations.yml +++ b/_data/translations.yml @@ -1,2 +1,2 @@ tour: - languages: [ba, es, ko, pt-br, pl, zh-cn, th, ru] + languages: [ba, es, ko, pt-br, pl, zh-cn, th, ru, ja] diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md new file mode 100644 index 0000000000..2060779b7e --- /dev/null +++ b/_ja/tour/tour-of-scala.md @@ -0,0 +1,84 @@ +--- +layout: tour +title: 前書き +language: ja + +discourse: true + +partof: scala-tour + +num: 1 + +next-page: basics + +redirect_from: "/tutorials/tour/tour-of-scala.html" +--- + +## ようこそツアーへ +このツアーはScalaで最もよく使う機能を一口サイズで紹介をしています。 +これらはScala初心者を対象としています。 + +これはほんの短いツアーで、完全な言語のチュートリアルではありません。 +もしそれを望むなら、[こちらの本](/books.html\) を手に入れるか、 +[その他の解決手段](/learn.html\) での相談を検討してください。 + +## Scalaとは? +Scalaは一般的なプログラミング方法を簡潔かつエレガントかつ型安全な方法で表現するために設計されたモダンなマルチパラダイム言語です。 +それはオブジェクト指向言語と関数型言語の機能をスムーズに統合しています。 + +## Scalaはオブジェクト指向 ## +Scalaは[全ての値がオブジェクトである](unified-types.html\) という意味では純数オブジェクト指向言語です。 +型とオブジェクトの振る舞いは[クラス](classes.html\) と[トレイト](traits.html\) によって記述されます。 +クラスはサブクラス化と多重継承の代わりの柔軟な[ミックスインを基にした合成](mixin-class-composition.html\) 機構により拡張されます。 + +## Scalaは関数型 ## +Scalaは[すべての関数が値である](unified-types.html\) という意味で関数型言語でもあります。 +Scalaは無名関数を定義するために[軽量な構文](basics.html#functions\) 提供し、 +それは[高階関数](higher-order-functions.html\) をサポートし、関数の[ネスト](nested-functions.html\) を許可し、[カリー化](multiple-parameter-lists.html\) をサポートします. +Scalaの[ケースクラス](case-classes.html\\) とその組み込みは多くのプログラミング言語で使われる[パターンマッチング](pattern-matching.html) モデル代数型をサポートします +[シングルトンオブジェクト](singleton-objects.html\) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 + +さらに、Scalaのパターンマッチングの概念は当然のことながら、[エクストラクタオブジェクト](extractor-objects.html\) による一般的な拡張として、[シーケンスパターンを正しく無視して](regular-expression-patterns.html\) 、[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing\\) をすることまで拡張されます。 + + +## Scalaは静的型付け ## +Scalaは抽象化が安全で首尾一貫した方法で使われることを静的に強制する表現型システムを備えています。 +特にその型システムは以下をサポートします: + +* [ジェネリッククラス](generic-classes.html\) +* [変位指定アノテーション](variances.html\) +* [上限](upper-type-bounds.html\\) と [下限](lower-type-bounds.html\\) 型境界, +* [内部クラス](inner-classes.html\) とオブジェクトメンバーとしての[抽象型メンバー](abstract-type-members.html\) +* [複合型](compound-types.html\) +* [明示的に型指定された自己参照](self-types.html\) +* [暗黙パラメーター](implicit-parameters.html\\) と [暗黙の変換](implicit-conversions.html\) +* [多態性メソッド](polymorphic-methods.html\) + +[型インターフェイス](type-inference.html\\) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 +組み合わせることで、それらの機能はプログラミングの抽象化の安全な再利用とソフトウェアの型安全な拡張のための強力な基礎を提供します。 + +## Scalaは拡張可能 ## + +実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張をよく必要となります。 +Scalaはライブラリの形で新しい言語構成をスムーズに追加すことを簡単にする言語メカニズムのユニークな組み合わせを提供します。 + +多くのケースで、これはマクロのようなメタプログラミングの機能を使うことなく実行することができます。例えば、 + +* [暗黙のクラス](http://docs.scala-lang.org/overviews/core/implicit-classes.html\\) は既存の型に拡張メソッドを追加することを許します。 + +* [文字列補間](/overviews/core/string-interpolation.html\\) はカスタム補間を使ってユーザー拡張可能です。 + +## Scalaの相互運用 + +Scalaは一般的なJava実行環境 (JRE\) と相互運用するように設計されています。 +特に、主流であるオブジェクト指向のJavaプログラミング言語とのやり取りはできるだけスムーズになっています。 +ScalaではSAMs、[ラムダ](higher-order-functions.html\) 、[アノテーション](annotations.html\) 、[ジェネリクス](generic-classes.html\) のようなより新しいJavaの機能の類似物があります。 + +[デフォルト引数値](default-parameter-values.html\) 、[名前付きパラメータ](named-arguments.html\) +といったJavaに類似物がないScalaの機能はできるだけJavaが合理的に理解できるよう、Javaに近しい形にコンパイルされます。 +ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み\) を持っており、数千の既存の高品質なライブラリへのアクセスができます。 + +## ツアーを楽しんで! + +もっと読むには、目次メニューの[次のページ](basics.html\) に進んでください。 + From 817a87a57244acb1853accd159480bc4d055bfed Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 18 Jan 2019 12:02:29 +0900 Subject: [PATCH 002/358] add missing sentence --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 2060779b7e..b3ed67ded7 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -39,7 +39,7 @@ Scalaの[ケースクラス](case-classes.html\\) とその組み込みは多く [シングルトンオブジェクト](singleton-objects.html\) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 さらに、Scalaのパターンマッチングの概念は当然のことながら、[エクストラクタオブジェクト](extractor-objects.html\) による一般的な拡張として、[シーケンスパターンを正しく無視して](regular-expression-patterns.html\) 、[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing\\) をすることまで拡張されます。 - +この際、For内包表記はクエリの定式化に役立ちます。これらの機能によりWebサービスのようなアプリケーション開発の際、Scalaを理想的なものとなります。 ## Scalaは静的型付け ## Scalaは抽象化が安全で首尾一貫した方法で使われることを静的に強制する表現型システムを備えています。 From 1fca6714bbf8d4d6bc1f7e5ba8f4357209e57ce4 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 18 Jan 2019 12:07:36 +0900 Subject: [PATCH 003/358] add origin tour md files --- _ja/tour/abstract-type-members.md | 78 ++++++ _ja/tour/annotations.md | 128 ++++++++++ _ja/tour/automatic-closures.md | 61 +++++ _ja/tour/basics.md | 323 ++++++++++++++++++++++++ _ja/tour/by-name-parameters.md | 42 +++ _ja/tour/case-classes.md | 59 +++++ _ja/tour/classes.md | 113 +++++++++ _ja/tour/compound-types.md | 54 ++++ _ja/tour/default-parameter-values.md | 49 ++++ _ja/tour/extractor-objects.md | 66 +++++ _ja/tour/for-comprehensions.md | 66 +++++ _ja/tour/generic-classes.md | 59 +++++ _ja/tour/higher-order-functions.md | 125 +++++++++ _ja/tour/implicit-conversions.md | 63 +++++ _ja/tour/implicit-parameters.md | 73 ++++++ _ja/tour/inner-classes.md | 81 ++++++ _ja/tour/lower-type-bounds.md | 70 +++++ _ja/tour/mixin-class-composition.md | 83 ++++++ _ja/tour/multiple-parameter-lists.md | 81 ++++++ _ja/tour/named-arguments.md | 34 +++ _ja/tour/nested-functions.md | 38 +++ _ja/tour/operators.md | 81 ++++++ _ja/tour/package-objects.md | 77 ++++++ _ja/tour/packages-and-imports.md | 85 +++++++ _ja/tour/pattern-matching.md | 151 +++++++++++ _ja/tour/polymorphic-methods.md | 37 +++ _ja/tour/regular-expression-patterns.md | 63 +++++ _ja/tour/self-types.md | 40 +++ _ja/tour/singleton-objects.md | 110 ++++++++ _ja/tour/traits.md | 84 ++++++ _ja/tour/tuples.md | 95 +++++++ _ja/tour/type-inference.md | 74 ++++++ _ja/tour/unified-types.md | 82 ++++++ _ja/tour/upper-type-bounds.md | 54 ++++ _ja/tour/variances.md | 154 +++++++++++ 35 files changed, 2933 insertions(+) create mode 100644 _ja/tour/abstract-type-members.md create mode 100644 _ja/tour/annotations.md create mode 100644 _ja/tour/automatic-closures.md create mode 100644 _ja/tour/basics.md create mode 100644 _ja/tour/by-name-parameters.md create mode 100644 _ja/tour/case-classes.md create mode 100644 _ja/tour/classes.md create mode 100644 _ja/tour/compound-types.md create mode 100644 _ja/tour/default-parameter-values.md create mode 100644 _ja/tour/extractor-objects.md create mode 100644 _ja/tour/for-comprehensions.md create mode 100644 _ja/tour/generic-classes.md create mode 100644 _ja/tour/higher-order-functions.md create mode 100644 _ja/tour/implicit-conversions.md create mode 100644 _ja/tour/implicit-parameters.md create mode 100644 _ja/tour/inner-classes.md create mode 100644 _ja/tour/lower-type-bounds.md create mode 100644 _ja/tour/mixin-class-composition.md create mode 100644 _ja/tour/multiple-parameter-lists.md create mode 100644 _ja/tour/named-arguments.md create mode 100644 _ja/tour/nested-functions.md create mode 100644 _ja/tour/operators.md create mode 100644 _ja/tour/package-objects.md create mode 100644 _ja/tour/packages-and-imports.md create mode 100644 _ja/tour/pattern-matching.md create mode 100644 _ja/tour/polymorphic-methods.md create mode 100644 _ja/tour/regular-expression-patterns.md create mode 100644 _ja/tour/self-types.md create mode 100644 _ja/tour/singleton-objects.md create mode 100644 _ja/tour/traits.md create mode 100644 _ja/tour/tuples.md create mode 100644 _ja/tour/type-inference.md create mode 100644 _ja/tour/unified-types.md create mode 100644 _ja/tour/upper-type-bounds.md create mode 100644 _ja/tour/variances.md diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md new file mode 100644 index 0000000000..f60cb61766 --- /dev/null +++ b/_ja/tour/abstract-type-members.md @@ -0,0 +1,78 @@ +--- +layout: tour +title: Abstract Type Members + +discourse: true + +partof: scala-tour +num: 23 +next-page: compound-types +previous-page: inner-classes +topics: abstract type members +prerequisite-knowledge: variance, upper-type-bound + +redirect_from: "/tutorials/tour/abstract-types.html" +redirect_from: "/tour/abstract-types.html" +--- + +Abstract types, such as traits and abstract classes, can in turn have abstract type members. +This means that the concrete implementations define the actual types. +Here's an example: + +```tut +trait Buffer { + type T + val element: T +} +``` +Here we have defined an abstract `type T`. It is used to describe the type of `element`. We can extend this trait in an abstract class, adding an upper-type-bound to `T` to make it more specific. + +```tut +abstract class SeqBuffer extends Buffer { + type U + type T <: Seq[U] + def length = element.length +} +``` +Notice how we can use yet another abstract `type U` as an upper-type-bound. This `class SeqBuffer` allows us to store only sequences in the buffer by stating that type `T` has to be a subtype of `Seq[U]` for a new abstract type `U`. + +Traits or [classes](classes.html) with abstract type members are often used in combination with anonymous class instantiations. To illustrate this, we now look at a program which deals with a sequence buffer that refers to a list of integers: + +```tut +abstract class IntSeqBuffer extends SeqBuffer { + type U = Int +} + + +def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer = + new IntSeqBuffer { + type T = List[U] + val element = List(elem1, elem2) + } +val buf = newIntSeqBuf(7, 8) +println("length = " + buf.length) +println("content = " + buf.element) +``` +Here the factory `newIntSeqBuf` uses an anonymous class implementation of `IntSeqBuf` (i.e. `new IntSeqBuffer`), setting `type T` to a `List[Int]`. + +It is also possible to turn abstract type members into type parameters of classes and vice versa. Here is a version of the code above which only uses type parameters: + +```tut +abstract class Buffer[+T] { + val element: T +} +abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] { + def length = element.length +} + +def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] = + new SeqBuffer[Int, List[Int]] { + val element = List(e1, e2) + } + +val buf = newIntSeqBuf(7, 8) +println("length = " + buf.length) +println("content = " + buf.element) +``` + +Note that we have to use [variance annotations](variances.html) here (`+T <: Seq[U]`) in order to hide the concrete sequence implementation type of the object returned from method `newIntSeqBuf`. Furthermore, there are cases where it is not possible to replace abstract types with type parameters. diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md new file mode 100644 index 0000000000..b486d7b806 --- /dev/null +++ b/_ja/tour/annotations.md @@ -0,0 +1,128 @@ +--- +layout: tour +title: Annotations + +discourse: true + +partof: scala-tour + +num: 32 +next-page: default-parameter-values +previous-page: by-name-parameters + +redirect_from: "/tutorials/tour/annotations.html" +--- + +Annotations associate meta-information with definitions. For example, the annotation `@deprecated` before a method causes the compiler to print a warning if the method is used. +``` +object DeprecationDemo extends App { + @deprecated("deprecation message", "release # which deprecates method") + def hello = "hola" + + hello +} +``` +This will compile but the compiler will print a warning: "there was one deprecation warning". + +An annotation clause applies to the first definition or declaration following it. More than one annotation clause may precede a definition and declaration. The order in which these clauses are given does not matter. + + +## Annotations that ensure correctness of encodings +Certain annotations will actually cause compilation to fail if a condition(s) is not met. For example, the annotation `@tailrec` ensures that a method is [tail-recursive](https://en.wikipedia.org/wiki/Tail_call). Tail-recursion can keep memory requirements constant. Here's how it's used in a method which calculates the factorial: +```tut +import scala.annotation.tailrec + +def factorial(x: Int): Int = { + + @tailrec + def factorialHelper(x: Int, accumulator: Int): Int = { + if (x == 1) accumulator else factorialHelper(x - 1, accumulator * x) + } + factorialHelper(x, 1) +} +``` +The `factorialHelper` method has the `@tailrec` which ensures the method is indeed tail-recursive. If we were to change the implementation of `factorialHelper` to the following, it would fail: +``` +import scala.annotation.tailrec + +def factorial(x: Int): Int = { + @tailrec + def factorialHelper(x: Int): Int = { + if (x == 1) 1 else x * factorialHelper(x - 1) + } + factorialHelper(x) +} +``` +We would get the message "Recursive call not in tail position". + + +## Annotations affecting code generation +Some annotations like `@inline` affect the generated code (i.e. your jar file might have different bytes than if you hadn't used the annotation). Inlining means inserting the code in a method's body at the call site. The resulting bytecode is longer, but hopefully runs faster. Using the annotation `@inline` does not ensure that a method will be inlined, but it will cause the compiler to do it if and only if some heuristics about the size of the generated code are met. + +### Java Annotations ### +When writing Scala code which interoperates with Java, there are a few differences in annotation syntax to note. +**Note:** Make sure you use the `-target:jvm-1.8` option with Java annotations. + +Java has user-defined metadata in the form of [annotations](https://docs.oracle.com/javase/tutorial/java/annotations/). A key feature of annotations is that they rely on specifying name-value pairs to initialize their elements. For instance, if we need an annotation to track the source of some class we might define it as + +``` +@interface Source { + public String URL(); + public String mail(); +} +``` + +And then apply it as follows + +``` +@Source(URL = "http://coders.com/", + mail = "support@coders.com") +public class MyClass extends HisClass ... +``` + +An annotation application in Scala looks like a constructor invocation, for instantiating a Java annotation one has to use named arguments: + +``` +@Source(URL = "http://coders.com/", + mail = "support@coders.com") +class MyScalaClass ... +``` + +This syntax is quite tedious if the annotation contains only one element (without default value) so, by convention, if the name is specified as `value` it can be applied in Java using a constructor-like syntax: + +``` +@interface SourceURL { + public String value(); + public String mail() default ""; +} +``` + +And then apply it as follows + +``` +@SourceURL("http://coders.com/") +public class MyClass extends HisClass ... +``` + +In this case, Scala provides the same possibility + +``` +@SourceURL("http://coders.com/") +class MyScalaClass ... +``` + +The `mail` element was specified with a default value so we need not explicitly provide a value for it. However, if we need to do it we can not mix-and-match the two styles in Java: + +``` +@SourceURL(value = "http://coders.com/", + mail = "support@coders.com") +public class MyClass extends HisClass ... +``` + +Scala provides more flexibility in this respect + +``` +@SourceURL("http://coders.com/", + mail = "support@coders.com") + class MyScalaClass ... +``` diff --git a/_ja/tour/automatic-closures.md b/_ja/tour/automatic-closures.md new file mode 100644 index 0000000000..37b82524ef --- /dev/null +++ b/_ja/tour/automatic-closures.md @@ -0,0 +1,61 @@ +--- +layout: tour +title: Automatic Type-Dependent Closure Construction + +discourse: true + +partof: scala-tour +--- + +Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evalutation). + +The following code demonstrates this mechanism: + + object TargetTest1 extends Application { + def whileLoop(cond: => Boolean)(body: => Unit): Unit = + if (cond) { + body + whileLoop(cond)(body) + } + var i = 10 + whileLoop (i > 0) { + println(i) + i -= 1 + } + } + +The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme. + +We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax). + +Here is the implementation of a loop-unless statement: + + object TargetTest2 extends Application { + def loop(body: => Unit): LoopUnlessCond = + new LoopUnlessCond(body) + protected class LoopUnlessCond(body: => Unit) { + def unless(cond: => Boolean) { + body + if (!cond) unless(cond) + } + } + var i = 10 + loop { + println("i = " + i) + i -= 1 + } unless (i == 0) + } +The `loop` function just accepts a body of a loop and returns an instance of class `LoopUnlessCond` (which encapsulates this body object). Note that the body didn't get evaluated yet. Class `LoopUnlessCond` has a method `unless` which we can use as a *infix operator*. This way, we achieve a quite natural syntax for our new loop: `loop { < stats > } unless ( < cond > )`. + +Here's the output when `TargetTest2` gets executed: + + i = 10 + i = 9 + i = 8 + i = 7 + i = 6 + i = 5 + i = 4 + i = 3 + i = 2 + i = 1 diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md new file mode 100644 index 0000000000..c9a0486c27 --- /dev/null +++ b/_ja/tour/basics.md @@ -0,0 +1,323 @@ +--- +layout: tour +title: Basics + +discourse: true + +partof: scala-tour + +num: 2 +next-page: unified-types +previous-page: tour-of-scala + +redirect_from: "/tutorials/tour/basics.html" +--- + +In this page, we will cover basics of Scala. + +## Trying Scala in the Browser + +You can run Scala in your browser with ScalaFiddle. + +1. Go to [https://scalafiddle.io](https://scalafiddle.io). +2. Paste `println("Hello, world!")` in the left pane. +3. Hit "Run" button. Output appears in the right pane. + +This is an easy, zero-setup way to experiment with pieces of Scala code. + +Many of the code examples in this documentation are also integrated with ScalaFiddle, so you +can directly experiment with them simply by clicking the Run-button. + +## Expressions + +Expressions are computable statements. +``` +1 + 1 +``` +You can output results of expressions using `println`. + +{% scalafiddle %} +```tut +println(1) // 1 +println(1 + 1) // 2 +println("Hello!") // Hello! +println("Hello," + " world!") // Hello, world! +``` +{% endscalafiddle %} + +### Values + +You can name results of expressions with the `val` keyword. + +```tut +val x = 1 + 1 +println(x) // 2 +``` + +Named results, such as `x` here, are called values. Referencing +a value does not re-compute it. + +Values cannot be re-assigned. + +```tut:fail +x = 3 // This does not compile. +``` + +Types of values can be inferred, but you can also explicitly state the type, like this: + +```tut +val x: Int = 1 + 1 +``` + +Notice how the type declaration `Int` comes after the identifier `x`. You also need a `:`. + +### Variables + +Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword. + +```tut +var x = 1 + 1 +x = 3 // This compiles because "x" is declared with the "var" keyword. +println(x * x) // 9 +``` + +As with values, you can explicitly state the type if you want: + +```tut +var x: Int = 1 + 1 +``` + + +## Blocks + +You can combine expressions by surrounding them with `{}`. We call this a block. + +The result of the last expression in the block is the result of the overall block, too. + +```tut +println({ + val x = 1 + 1 + x + 1 +}) // 3 +``` + +## Functions + +Functions are expressions that take parameters. + +You can define an anonymous function (i.e. no name) that returns a given integer plus one: + +```tut +(x: Int) => x + 1 +``` + +On the left of `=>` is a list of parameters. On the right is an expression involving the parameters. + +You can also name functions. + +{% scalafiddle %} +```tut +val addOne = (x: Int) => x + 1 +println(addOne(1)) // 2 +``` +{% endscalafiddle %} + +Functions may take multiple parameters. + +{% scalafiddle %} +```tut +val add = (x: Int, y: Int) => x + y +println(add(1, 2)) // 3 +``` +{% endscalafiddle %} + +Or it can take no parameters. + +```tut +val getTheAnswer = () => 42 +println(getTheAnswer()) // 42 +``` + +## Methods + +Methods look and behave very similar to functions, but there are a few key differences between them. + +Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body. + +{% scalafiddle %} +```tut +def add(x: Int, y: Int): Int = x + y +println(add(1, 2)) // 3 +``` +{% endscalafiddle %} + +Notice how the return type is declared _after_ the parameter list and a colon `: Int`. + +Methods can take multiple parameter lists. + +{% scalafiddle %} +```tut +def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier +println(addThenMultiply(1, 2)(3)) // 9 +``` +{% endscalafiddle %} + +Or no parameter lists at all. + +```tut +def name: String = System.getProperty("user.name") +println("Hello, " + name + "!") +``` + +There are some other differences, but for now, you can think of them as something similar to functions. + +Methods can have multi-line expressions as well. +```tut +def getSquareString(input: Double): String = { + val square = input * input + square.toString +} +``` +The last expression in the body is the method's return value. (Scala does have a `return` keyword, but it's rarely used.) + +## Classes + +You can define classes with the `class` keyword followed by its name and constructor parameters. + +```tut +class Greeter(prefix: String, suffix: String) { + def greet(name: String): Unit = + println(prefix + name + suffix) +} +``` +The return type of the method `greet` is `Unit`, which says there's nothing meaningful to return. It's used similarly to `void` in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.) + +You can make an instance of a class with the `new` keyword. + +```tut +val greeter = new Greeter("Hello, ", "!") +greeter.greet("Scala developer") // Hello, Scala developer! +``` + +We will cover classes in depth [later](classes.html). + +## Case Classes + +Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the `case class` keywords. + +```tut +case class Point(x: Int, y: Int) +``` + +You can instantiate case classes without `new` keyword. + +```tut +val point = Point(1, 2) +val anotherPoint = Point(1, 2) +val yetAnotherPoint = Point(2, 2) +``` + +And they are compared by value. + +```tut +if (point == anotherPoint) { + println(point + " and " + anotherPoint + " are the same.") +} else { + println(point + " and " + anotherPoint + " are different.") +} // Point(1,2) and Point(1,2) are the same. + +if (point == yetAnotherPoint) { + println(point + " and " + yetAnotherPoint + " are the same.") +} else { + println(point + " and " + yetAnotherPoint + " are different.") +} // Point(1,2) and Point(2,2) are different. +``` + +There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.html). + +## Objects + +Objects are single instances of their own definitions. You can think of them as singletons of their own classes. + +You can define objects with the `object` keyword. + +```tut +object IdFactory { + private var counter = 0 + def create(): Int = { + counter += 1 + counter + } +} +``` + +You can access an object by referring to its name. + +```tut +val newId: Int = IdFactory.create() +println(newId) // 1 +val newerId: Int = IdFactory.create() +println(newerId) // 2 +``` + +We will cover objects in depth [later](singleton-objects.html). + +## Traits + +Traits are types containing certain fields and methods. Multiple traits can be combined. + +You can define traits with `trait` keyword. + +```tut +trait Greeter { + def greet(name: String): Unit +} +``` + +Traits can also have default implementations. + +{% scalafiddle %} +```tut +trait Greeter { + def greet(name: String): Unit = + println("Hello, " + name + "!") +} +``` + +You can extend traits with the `extends` keyword and override an implementation with the `override` keyword. + +```tut +class DefaultGreeter extends Greeter + +class CustomizableGreeter(prefix: String, postfix: String) extends Greeter { + override def greet(name: String): Unit = { + println(prefix + name + postfix) + } +} + +val greeter = new DefaultGreeter() +greeter.greet("Scala developer") // Hello, Scala developer! + +val customGreeter = new CustomizableGreeter("How are you, ", "?") +customGreeter.greet("Scala developer") // How are you, Scala developer? +``` +{% endscalafiddle %} + +Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits. + +We will cover traits in depth [later](traits.html). + +## Main Method + +The main method is an entry point of a program. The Java Virtual +Machine requires a main method to be named `main` and take one +argument, an array of strings. + +Using an object, you can define a main method as follows: + +```tut +object Main { + def main(args: Array[String]): Unit = + println("Hello, Scala developer!") +} +``` diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md new file mode 100644 index 0000000000..b9648a21f4 --- /dev/null +++ b/_ja/tour/by-name-parameters.md @@ -0,0 +1,42 @@ +--- +layout: tour +title: By-name Parameters + +discourse: true + +partof: scala-tour + +num: 31 +next-page: annotations +previous-page: operators + +redirect_from: "/tutorials/tour/by-name-parameters.html" +--- + +_By-name parameters_ are only evaluated when used. They are in contrast to _by-value parameters_. To make a parameter called by-name, simply prepend `=>` to its type. +```tut +def calculate(input: => Int) = input * 37 +``` +By-name parameters have the advantage that they are not evaluated if they aren't used in the function body. On the other hand, by-value parameters have the advantage that they are evaluated only once. + +Here's an example of how we could implement a while loop: + +```tut +def whileLoop(condition: => Boolean)(body: => Unit): Unit = + if (condition) { + body + whileLoop(condition)(body) + } + +var i = 2 + +whileLoop (i > 0) { + println(i) + i -= 1 +} // prints 2 1 +``` +The method `whileLoop` uses multiple parameter lists to take a condition and a body of the loop. If the `condition` is true, the `body` is executed and then a recursive call to whileLoop is made. If the `condition` is false, the body is never evaluated because we prepended `=>` to the type of `body`. + +Now when we pass `i > 0` as our `condition` and `println(i); i-= 1` as the `body`, it behaves like the standard while loop in many languages. + +This ability to delay evaluation of a parameter until it is used can help performance if the parameter is computationally intensive to evaluate or a longer-running block of code such as fetching a URL. diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md new file mode 100644 index 0000000000..607e449d0e --- /dev/null +++ b/_ja/tour/case-classes.md @@ -0,0 +1,59 @@ +--- +layout: tour +title: Case Classes + +discourse: true + +partof: scala-tour + +num: 11 +next-page: pattern-matching +previous-page: multiple-parameter-lists +prerequisite-knowledge: classes, basics, mutability + +redirect_from: "/tutorials/tour/case-classes.html" +--- + +Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we'll see how they are useful in [pattern matching](pattern-matching.html). + +## Defining a case class +A minimal case class requires the keywords `case class`, an identifier, and a parameter list (which may be empty): +```tut +case class Book(isbn: String) + +val frankenstein = Book("978-0486282114") +``` +Notice how the keyword `new` was not used to instantiate the `Book` case class. This is because case classes have an `apply` method by default which takes care of object construction. + +When you create a case class with parameters, the parameters are public `val`s. +``` +case class Message(sender: String, recipient: String, body: String) +val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") + +println(message1.sender) // prints guillaume@quebec.ca +message1.sender = "travis@washington.us" // this line does not compile +``` +You can't reassign `message1.sender` because it is a `val` (i.e. immutable). It is possible to use `var`s in case classes but this is discouraged. + +## Comparison +Case classes are compared by structure and not by reference: +``` +case class Message(sender: String, recipient: String, body: String) + +val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?") +val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?") +val messagesAreTheSame = message2 == message3 // true +``` +Even though `message2` and `message3` refer to different objects, the value of each object is equal. + +## Copying +You can create a (shallow) copy of an instance of a case class simply by using the `copy` method. You can optionally change the constructor arguments. +``` +case class Message(sender: String, recipient: String, body: String) +val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg") +val message5 = message4.copy(sender = message4.recipient, recipient = "claire@bourgogne.fr") +message5.sender // travis@washington.us +message5.recipient // claire@bourgogne.fr +message5.body // "Me zo o komz gant ma amezeg" +``` +The recipient of `message4` is used as the sender of `message5` but the `body` of `message4` was copied directly. diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md new file mode 100644 index 0000000000..1f4b80b59e --- /dev/null +++ b/_ja/tour/classes.md @@ -0,0 +1,113 @@ +--- +layout: tour +title: Classes + +discourse: true + +partof: scala-tour + +num: 4 +next-page: traits +previous-page: unified-types +topics: classes +prerequisite-knowledge: no-return-keyword, type-declaration-syntax, string-interpolation, procedures + +redirect_from: "/tutorials/tour/classes.html" +--- + +Classes in Scala are blueprints for creating objects. They can contain methods, +values, variables, types, objects, traits, and classes which are collectively called _members_. Types, objects, and traits will be covered later in the tour. + +## Defining a class +A minimal class definition is simply the keyword `class` and +an identifier. Class names should be capitalized. +```tut +class User + +val user1 = new User +``` +The keyword `new` is used to create an instance of the class. `User` has a default constructor which takes no arguments because no constructor was defined. However, you'll often want a constructor and class body. Here is an example class definition for a point: + +```tut +class Point(var x: Int, var y: Int) { + + def move(dx: Int, dy: Int): Unit = { + x = x + dx + y = y + dy + } + + override def toString: String = + s"($x, $y)" +} + +val point1 = new Point(2, 3) +point1.x // 2 +println(point1) // prints (2, 3) +``` + +This `Point` class has four members: the variables `x` and `y` and the methods `move` and +`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments and returns the Unit value `()`, which carries no information. This corresponds roughly with `void` in Java-like languages. `toString`, on the other hand, does not take any arguments but returns a `String` value. Since `toString` overrides `toString` from [`AnyRef`](unified-types.html), it is tagged with the `override` keyword. + +## Constructors + +Constructors can have optional parameters by providing a default value like so: + +```tut +class Point(var x: Int = 0, var y: Int = 0) + +val origin = new Point // x and y are both set to 0 +val point1 = new Point(1) +println(point1.x) // prints 1 + +``` + +In this version of the `Point` class, `x` and `y` have the default value `0` so no arguments are required. However, because the constructor reads arguments left to right, if you just wanted to pass in a `y` value, you would need to name the parameter. +``` +class Point(var x: Int = 0, var y: Int = 0) +val point2 = new Point(y=2) +println(point2.y) // prints 2 +``` + +This is also a good practice to enhance clarity. + +## Private Members and Getter/Setter Syntax +Members are public by default. Use the `private` access modifier +to hide them from outside of the class. +```tut +class Point { + private var _x = 0 + private var _y = 0 + private val bound = 100 + + def x = _x + def x_= (newValue: Int): Unit = { + if (newValue < bound) _x = newValue else printWarning + } + + def y = _y + def y_= (newValue: Int): Unit = { + if (newValue < bound) _y = newValue else printWarning + } + + private def printWarning = println("WARNING: Out of bounds") +} + +val point1 = new Point +point1.x = 99 +point1.y = 101 // prints the warning +``` +In this version of the `Point` class, the data is stored in private variables `_x` and `_y`. There are methods `def x` and `def y` for accessing the private data. `def x_=` and `def y_=` are for validating and setting the value of `_x` and `_y`. Notice the special syntax for the setters: the method has `_=` appended to the identifier of the getter and the parameters come after. + +Primary constructor parameters with `val` and `var` are public. However, because `val`s are immutable, you can't write the following. +``` +class Point(val x: Int, val y: Int) +val point = new Point(1, 2) +point.x = 3 // <-- does not compile +``` + +Parameters without `val` or `var` are private values, visible only within the class. +``` +class Point(x: Int, y: Int) +val point = new Point(1, 2) +point.x // <-- does not compile +``` diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md new file mode 100644 index 0000000000..3e36f604e3 --- /dev/null +++ b/_ja/tour/compound-types.md @@ -0,0 +1,54 @@ +--- +layout: tour +title: Compound Types + +discourse: true + +partof: scala-tour + +num: 24 +next-page: self-types +previous-page: abstract-type-members + +redirect_from: "/tutorials/tour/compound-types.html" +--- + +Sometimes it is necessary to express that the type of an object is a subtype of several other types. In Scala this can be expressed with the help of *compound types*, which are intersections of object types. + +Suppose we have two traits `Cloneable` and `Resetable`: + +```tut +trait Cloneable extends java.lang.Cloneable { + override def clone(): Cloneable = { + super.clone().asInstanceOf[Cloneable] + } +} +trait Resetable { + def reset: Unit +} +``` + +Now suppose we want to write a function `cloneAndReset` which takes an object, clones it and resets the original object: + +``` +def cloneAndReset(obj: ?): Cloneable = { + val cloned = obj.clone() + obj.reset + cloned +} +``` + +The question arises what the type of the parameter `obj` is. If it's `Cloneable` then the object can be `clone`d, but not `reset`; if it's `Resetable` we can `reset` it, but there is no `clone` operation. To avoid type casts in such a situation, we can specify the type of `obj` to be both `Cloneable` and `Resetable`. This compound type is written like this in Scala: `Cloneable with Resetable`. + +Here's the updated function: + +``` +def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { + //... +} +``` + +Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members. +The general form is: `A with B with C ... { refinement }` + +An example for the use of refinements is given on the page about [class composition with mixins](mixin-class-composition.html). diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md new file mode 100644 index 0000000000..20f701e03d --- /dev/null +++ b/_ja/tour/default-parameter-values.md @@ -0,0 +1,49 @@ +--- +layout: tour +title: Default Parameter Values + +discourse: true + +partof: scala-tour + +num: 33 +next-page: named-arguments +previous-page: annotations +prerequisite-knowledge: named-arguments, function syntax + +redirect_from: "/tutorials/tour/default-parameter-values.html" +--- + +Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. + +```tut +def log(message: String, level: String = "INFO") = println(s"$level: $message") + +log("System starting") // prints INFO: System starting +log("User not found", "WARNING") // prints WARNING: User not found +``` + +The parameter `level` has a default value so it is optional. On the last line, the argument `"WARNING"` overrides the default argument `"INFO"`. Where you might do overloaded methods in Java, you can use methods with optional parameters to achieve the same effect. However, if the caller omits an argument, any following arguments must be named. + +```tut +class Point(val x: Double = 0, val y: Double = 0) + +val point1 = new Point(y = 1) +``` +Here we have to say `y = 1`. + +Note that default parameters in Scala are not optional when called from Java code: + +```tut +// Point.scala +class Point(val x: Double = 0, val y: Double = 0) +``` + +```java +// Main.java +public class Main { + public static void main(String[] args) { + Point point = new Point(1); // does not compile + } +} +``` diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md new file mode 100644 index 0000000000..a346efd6bd --- /dev/null +++ b/_ja/tour/extractor-objects.md @@ -0,0 +1,66 @@ +--- +layout: tour +title: Extractor Objects + +discourse: true + +partof: scala-tour + +num: 16 +next-page: for-comprehensions +previous-page: regular-expression-patterns + +redirect_from: "/tutorials/tour/extractor-objects.html" +--- + +An extractor object is an object with an `unapply` method. Whereas the `apply` method is like a constructor which takes arguments and creates an object, the `unapply` takes an object and tries to give back the arguments. This is most often used in pattern matching and partial functions. + +```tut +import scala.util.Random + +object CustomerID { + + def apply(name: String) = s"$name--${Random.nextLong}" + + def unapply(customerID: String): Option[String] = { + val stringArray: Array[String] = customerID.split("--") + if (stringArray.tail.nonEmpty) Some(stringArray.head) else None + } +} + +val customer1ID = CustomerID("Sukyoung") // Sukyoung--23098234908 +customer1ID match { + case CustomerID(name) => println(name) // prints Sukyoung + case _ => println("Could not extract a CustomerID") +} +``` + +The `apply` method creates a `CustomerID` string from a `name`. The `unapply` does the inverse to get the `name` back. When we call `CustomerID("Sukyoung")`, this is shorthand syntax for calling `CustomerID.apply("Sukyoung")`. When we call `case CustomerID(name) => println(name)`, we're calling the unapply method. + +Since a value definition can use a pattern to introduce a new variable, an extractor can be used to initialize the variable, where the unapply method supplies the value. + +```tut +val customer2ID = CustomerID("Nico") +val CustomerID(name) = customer2ID +println(name) // prints Nico +``` + +This is equivalent to `val name = CustomerID.unapply(customer2ID).get`. + +```tut +val CustomerID(name2) = "--asdfasdfasdf" +``` + +If there is no match, a `scala.MatchError` is thrown: + +```tut:fail +val CustomerID(name3) = "-asdfasdfasdf" +``` + +The return type of an `unapply` should be chosen as follows: + +* If it is just a test, return a `Boolean`. For instance `case even()`. +* If it returns a single sub-value of type T, return an `Option[T]`. +* If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`. + +Sometimes, the number of values to extract isn't fixed and we would like to return an arbitrary number of values, depending on the input. For this use case, you can define extractors with an `unapplySeq` method which returns an `Option[Seq[T]]`. Common examples of these patterns include deconstructing a `List` using `case List(x, y, z) =>` and decomposing a `String` using a regular expression `Regex`, such as `case r(name, remainingFields @ _*) =>`. diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md new file mode 100644 index 0000000000..85f5730cd0 --- /dev/null +++ b/_ja/tour/for-comprehensions.md @@ -0,0 +1,66 @@ +--- +layout: tour +title: For Comprehensions + +discourse: true + +partof: scala-tour + +num: 17 +next-page: generic-classes +previous-page: extractor-objects + +redirect_from: "/tutorials/tour/for-comprehensions.html" +--- + +Scala offers a lightweight notation for expressing *sequence comprehensions*. Comprehensions have the form `for (enumerators) yield e`, where `enumerators` refers to a semicolon-separated list of enumerators. An *enumerator* is either a generator which introduces new variables, or it is a filter. A comprehension evaluates the body `e` for each binding generated by the enumerators and returns a sequence of these values. + +Here's an example: + +```tut +case class User(name: String, age: Int) + +val userBase = List(User("Travis", 28), + User("Kelly", 33), + User("Jennifer", 44), + User("Dennis", 23)) + +val twentySomethings = for (user <- userBase if (user.age >=20 && user.age < 30)) + yield user.name // i.e. add this to a list + +twentySomethings.foreach(name => println(name)) // prints Travis Dennis +``` +The `for` loop used with a `yield` statement actually creates a `List`. Because we said `yield user.name`, it's a `List[String]`. `user <- userBase` is our generator and `if (user.age >=20 && user.age < 30)` is a guard that filters out users who are not in their 20s. + +Here is a more complicated example using two generators. It computes all pairs of numbers between `0` and `n-1` whose sum is equal to a given value `v`: + +```tut +def foo(n: Int, v: Int) = + for (i <- 0 until n; + j <- i until n if i + j == v) + yield (i, j) + +foo(10, 10) foreach { + case (i, j) => + println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) +} + +``` +Here `n == 10` and `v == 10`. On the first iteration, `i == 0` and `j == 0` so `i + j != v` and therefore nothing is yielded. `j` gets incremented 9 more times before `i` gets incremented to `1`. Without the `if` guard, this would simply print the following: +``` + +(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 1) ... +``` + +Note that comprehensions are not restricted to lists. Every datatype that supports the operations `withFilter`, `map`, and `flatMap` (with the proper types) can be used in sequence comprehensions. + +You can omit `yield` in a comprehension. In that case, comprehension will return `Unit`. This can be useful in case you need to perform side-effects. Here's a program equivalent to the previous one, but without using `yield`: + +```tut +def foo(n: Int, v: Int) = + for (i <- 0 until n; + j <- i until n if i + j == v) + println(s"($i, $j)") + +foo(10, 10) +``` diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md new file mode 100644 index 0000000000..2433cd6b45 --- /dev/null +++ b/_ja/tour/generic-classes.md @@ -0,0 +1,59 @@ +--- +layout: tour +title: Generic Classes + +discourse: true + +partof: scala-tour + +num: 18 +next-page: variances +previous-page: for-comprehensions +assumed-knowledge: classes unified-types + +redirect_from: "/tutorials/tour/generic-classes.html" +--- +Generic classes are classes which take a type as a parameter. They are particularly useful for collection classes. + +## Defining a generic class +Generic classes take a type as a parameter within square brackets `[]`. One convention is to use the letter `A` as type parameter identifier, though any parameter name may be used. +```tut +class Stack[A] { + private var elements: List[A] = Nil + def push(x: A) { elements = x :: elements } + def peek: A = elements.head + def pop(): A = { + val currentTop = peek + elements = elements.tail + currentTop + } +} +``` +This implementation of a `Stack` class takes any type `A` as a parameter. This means the underlying list, `var elements: List[A] = Nil`, can only store elements of type `A`. The procedure `def push` only accepts objects of type `A` (note: `elements = x :: elements` reassigns `elements` to a new list created by prepending `x` to the current `elements`). + +## Usage + +To use a generic class, put the type in the square brackets in place of `A`. +``` +val stack = new Stack[Int] +stack.push(1) +stack.push(2) +println(stack.pop) // prints 2 +println(stack.pop) // prints 1 +``` +The instance `stack` can only take Ints. However, if the type argument had subtypes, those could be passed in: +``` +class Fruit +class Apple extends Fruit +class Banana extends Fruit + +val stack = new Stack[Fruit] +val apple = new Apple +val banana = new Banana + +stack.push(apple) +stack.push(banana) +``` +Class `Apple` and `Banana` both extend `Fruit` so we can push instances `apple` and `banana` onto the stack of `Fruit`. + +_Note: subtyping of generic types is *invariant*. This means that if we have a stack of characters of type `Stack[Char]` then it cannot be used as an integer stack of type `Stack[Int]`. This would be unsound because it would enable us to enter true integers into the character stack. To conclude, `Stack[A]` is only a subtype of `Stack[B]` if and only if `B = A`. Since this can be quite restrictive, Scala offers a [type parameter annotation mechanism](variances.html) to control the subtyping behavior of generic types._ diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md new file mode 100644 index 0000000000..efc8d37db2 --- /dev/null +++ b/_ja/tour/higher-order-functions.md @@ -0,0 +1,125 @@ +--- +layout: tour +title: Higher-order Functions + +discourse: true + +partof: scala-tour + +num: 8 +next-page: nested-functions +previous-page: mixin-class-composition + +redirect_from: "/tutorials/tour/higher-order-functions.html" +--- + +Higher order functions take other functions as parameters or return a function as +a result. This is possible because functions are first-class values in Scala. +The terminology can get a bit confusing at this point, and we use the phrase +"higher order function" for both methods and functions that take functions as parameters +or that return a function. + +One of the most common examples is the higher-order +function `map` which is available for collections in Scala. +```tut +val salaries = Seq(20000, 70000, 40000) +val doubleSalary = (x: Int) => x * 2 +val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000) +``` +`doubleSalary` is a function which takes a single Int, `x`, and returns `x * 2`. In general, the tuple on the left of the arrow `=>` is a parameter list and the value of the expression on the right is what gets returned. On line 3, the function `doubleSalary` gets applied to each element in the +list of salaries. + +To shrink the code, we could make the function anonymous and pass it directly as +an argument to map: +``` +val salaries = Seq(20000, 70000, 40000) +val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000) +``` +Notice how `x` is not declared as an Int in the above example. That's because the +compiler can infer the type based on the type of function map expects. An even more idiomatic way to write the same piece of code would be: + +```tut +val salaries = Seq(20000, 70000, 40000) +val newSalaries = salaries.map(_ * 2) +``` +Since the Scala compiler already knows the type of the parameters (a single Int), + you just need to provide the right side of the function. The only +caveat is that you need to use `_` in place of a parameter name (it was `x` in +the previous example). + +## Coercing methods into functions +It is also possible to pass methods as arguments to higher-order functions because +the Scala compiler will coerce the method into a function. +``` +case class WeeklyWeatherForecast(temperatures: Seq[Double]) { + + private def convertCtoF(temp: Double) = temp * 1.8 + 32 + + def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF +} +``` +Here the method `convertCtoF` is passed to `forecastInFahrenheit`. This is possible because the compiler coerces `convertCtoF` to the function `x => convertCtoF(x)` (note: `x` will + be a generated name which is guaranteed to be unique within its scope). + +## Functions that accept functions +One reason to use higher-order functions is to reduce redundant code. Let's say you wanted some methods that could raise someone's salaries by various factors. Without creating a higher-order function, +it might look something like this: + +```tut +object SalaryRaiser { + + def smallPromotion(salaries: List[Double]): List[Double] = + salaries.map(salary => salary * 1.1) + + def greatPromotion(salaries: List[Double]): List[Double] = + salaries.map(salary => salary * math.log(salary)) + + def hugePromotion(salaries: List[Double]): List[Double] = + salaries.map(salary => salary * salary) +} +``` + +Notice how each of the three methods vary only by the multiplication factor. To simplify, +you can extract the repeated code into a higher-order function like so: + +```tut +object SalaryRaiser { + + private def promotion(salaries: List[Double], promotionFunction: Double => Double): List[Double] = + salaries.map(promotionFunction) + + def smallPromotion(salaries: List[Double]): List[Double] = + promotion(salaries, salary => salary * 1.1) + + def bigPromotion(salaries: List[Double]): List[Double] = + promotion(salaries, salary => salary * math.log(salary)) + + def hugePromotion(salaries: List[Double]): List[Double] = + promotion(salaries, salary => salary * salary) +} +``` + +The new method, `promotion`, takes the salaries plus a function of type `Double => Double` +(i.e. a function that takes a Double and returns a Double) and returns the product. + +## Functions that return functions + +There are certain cases where you want to generate a function. Here's an example +of a method that returns a function. + +```tut +def urlBuilder(ssl: Boolean, domainName: String): (String, String) => String = { + val schema = if (ssl) "https://" else "http://" + (endpoint: String, query: String) => s"$schema$domainName/$endpoint?$query" +} + +val domainName = "www.example.com" +def getURL = urlBuilder(ssl=true, domainName) +val endpoint = "users" +val query = "id=1" +val url = getURL(endpoint, query) // "https://www.example.com/users?id=1": String +``` + +Notice the return type of urlBuilder `(String, String) => String`. This means that +the returned anonymous function takes two Strings and returns a String. In this case, +the returned anonymous function is `(endpoint: String, query: String) => s"https://www.example.com/$endpoint?$query"`. diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md new file mode 100644 index 0000000000..be7f2edf6f --- /dev/null +++ b/_ja/tour/implicit-conversions.md @@ -0,0 +1,63 @@ +--- +layout: tour +title: Implicit Conversions + +discourse: true + +partof: scala-tour + +num: 27 +next-page: polymorphic-methods +previous-page: implicit-parameters + +redirect_from: "/tutorials/tour/implicit-conversions.html" +--- + +An implicit conversion from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type. + +Implicit conversions are applied in two situations: + +* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`. +* In a selection `e.m` with `e` of type `S`, if the selector `m` does not denote a member of `S`. + +In the first case, a conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`. +In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`. + +If an implicit method `List[A] => Ordered[List[A]]` is in scope, as well as an implicit method `Int => Ordered[Int]`, the following operation on the two lists of type `List[Int]` is legal: + +``` +List(1, 2, 3) <= List(4, 5) +``` + +An implicit method `Int => Ordered[Int]` is provided automatically through `scala.Predef.intWrapper`. An example of an implicit method `List[A] => Ordered[List[A]]` is provided below. + +```tut +import scala.language.implicitConversions + +implicit def list2ordered[A](x: List[A]) + (implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] = + new Ordered[List[A]] { + //replace with a more useful implementation + def compare(that: List[A]): Int = 1 + } +``` + +The implicitly imported object `scala.Predef` declares several aliases to frequently used types (e.g. `scala.collection.immutable.Map` is aliased to `Map`) and methods (e.g. `assert`) but also several implicit conversions. + +For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions: + +```tut +import scala.language.implicitConversions + +implicit def int2Integer(x: Int) = + java.lang.Integer.valueOf(x) +``` + +Because implicit conversions can have pitfalls if used indiscriminately the compiler warns when compiling the implicit conversion definition. + +To turn off the warnings take either of these actions: + +* Import `scala.language.implicitConversions` into the scope of the implicit conversion definition +* Invoke the compiler with `-language:implicitConversions` + +No warning is emitted when the conversion is applied by the compiler. diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md new file mode 100644 index 0000000000..43befb04e7 --- /dev/null +++ b/_ja/tour/implicit-parameters.md @@ -0,0 +1,73 @@ +--- +layout: tour +title: Implicit Parameters + +discourse: true + +partof: scala-tour + +num: 26 +next-page: implicit-conversions +previous-page: self-types + +redirect_from: "/tutorials/tour/implicit-parameters.html" +--- + +A method can have an _implicit_ parameter list, marked by the _implicit_ keyword at the start of the parameter list. If the parameters in that parameter list are not passed as usual, Scala will look if it can get an implicit value of the correct type, and if it can, pass it automatically. + +The places Scala will look for these parameters fall into two categories: + +* Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called. +* Then it looks for members marked implicit in all the companion objects associated with the implicit candidate type. + +A more detailed guide to where scala looks for implicits can be found in [the FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html) + +In the following example we define a method `sum` which computes the sum of a list of elements using the monoid's `add` and `unit` operations. Please note that implicit values can not be top-level. + +```tut +abstract class Monoid[A] { + def add(x: A, y: A): A + def unit: A +} + +object ImplicitTest { + implicit val stringMonoid: Monoid[String] = new Monoid[String] { + def add(x: String, y: String): String = x concat y + def unit: String = "" + } + + implicit val intMonoid: Monoid[Int] = new Monoid[Int] { + def add(x: Int, y: Int): Int = x + y + def unit: Int = 0 + } + + def sum[A](xs: List[A])(implicit m: Monoid[A]): A = + if (xs.isEmpty) m.unit + else m.add(xs.head, sum(xs.tail)) + + def main(args: Array[String]): Unit = { + println(sum(List(1, 2, 3))) // uses intMonoid implicitly + println(sum(List("a", "b", "c"))) // uses stringMonoid implicitly + } +} +``` + +`Monoid` defines an operation called `add` here, that combines a pair of `A`s and returns another `A`, together with an operation called `unit` that is able to create some (specific) `A`. + +To show how implicit parameters work, we first define monoids `stringMonoid` and `intMonoid` for strings and integers, respectively. The `implicit` keyword indicates that the corresponding object can be used implicitly. + +The method `sum` takes a `List[A]` and returns an `A`, which takes the initial `A` from `unit`, and combines each next `A` in the list to that with the `add` method. Making the parameter `m` implicit here means we only have to provide the `xs` parameter when we call the method if Scala can find a an implict `Monoid[A]` to use for the implicit `m` parameter. + +In our `main` method we call `sum` twice, and only provide the `xs` parameter. Scala will now look for an implicit in the scope mentioned above. The first call to `sum` passes a `List[Int]` for `xs`, which means that `A` is `Int`. The implicit parameter list with `m` is left out, so Scala will look for an implicit of type `Monoid[Int]`. The first lookup rule reads + +> Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called. + +`intMonoid` is an implicit definition that can be accessed directly in `main`. It is also of the correct type, so it's passed to the `sum` method automatically. + +The second call to `sum` passes a `List[String]`, which means that `A` is `String`. Implicit lookup will go the same way as with `Int`, but will this time find `stringMonoid`, and passes that automatically as `m`. + +The program will output +``` +6 +abc +``` diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md new file mode 100644 index 0000000000..172dd03bcd --- /dev/null +++ b/_ja/tour/inner-classes.md @@ -0,0 +1,81 @@ +--- +layout: tour +title: Inner Classes + +discourse: true + +partof: scala-tour + +num: 22 +next-page: abstract-type-members +previous-page: lower-type-bounds + +redirect_from: "/tutorials/tour/inner-classes.html" +--- + +In Scala it is possible to let classes have other classes as members. As opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala such inner classes are bound to the outer object. Suppose we want the compiler to prevent us, at compile time, from mixing up which nodes belong to what graph. Path-dependent types provide a solution. + +To illustrate the difference, we quickly sketch the implementation of a graph datatype: + +```tut +class Graph { + class Node { + var connectedNodes: List[Node] = Nil + def connectTo(node: Node) { + if (connectedNodes.find(node.equals).isEmpty) { + connectedNodes = node :: connectedNodes + } + } + } + var nodes: List[Node] = Nil + def newNode: Node = { + val res = new Node + nodes = res :: nodes + res + } +} +``` +This program represents a graph as a list of nodes (`List[Node]`). Each node has a list of other nodes it's connected to (`connectedNodes`). The `class Node` is a _path-dependent type_ because it is nested in the `class Graph`. Therefore, all nodes in the `connectedNodes` must be created using the `newNode` from the same instance of `Graph`. + +```tut +val graph1: Graph = new Graph +val node1: graph1.Node = graph1.newNode +val node2: graph1.Node = graph1.newNode +val node3: graph1.Node = graph1.newNode +node1.connectTo(node2) +node3.connectTo(node1) +``` +We have explicitly declared the type of `node1`, `node2`, and `node3` as `graph1.Node` for clarity but the compiler could have inferred it. This is because when we call `graph1.newNode` which calls `new Node`, the method is using the instance of `Node` specific to the instance `graph1`. + +If we now have two graphs, the type system of Scala does not allow us to mix nodes defined within one graph with the nodes of another graph, since the nodes of the other graph have a different type. +Here is an illegal program: + +``` +val graph1: Graph = new Graph +val node1: graph1.Node = graph1.newNode +val node2: graph1.Node = graph1.newNode +node1.connectTo(node2) // legal +val graph2: Graph = new Graph +val node3: graph2.Node = graph2.newNode +node1.connectTo(node3) // illegal! +``` +The type `graph1.Node` is distinct from the type `graph2.Node`. In Java, the last line in the previous example program would have been correct. For nodes of both graphs, Java would assign the same type `Graph.Node`; i.e. `Node` is prefixed with class `Graph`. In Scala such a type can be expressed as well, it is written `Graph#Node`. If we want to be able to connect nodes of different graphs, we have to change the definition of our initial graph implementation in the following way: + +```tut +class Graph { + class Node { + var connectedNodes: List[Graph#Node] = Nil + def connectTo(node: Graph#Node) { + if (connectedNodes.find(node.equals).isEmpty) { + connectedNodes = node :: connectedNodes + } + } + } + var nodes: List[Node] = Nil + def newNode: Node = { + val res = new Node + nodes = res :: nodes + res + } +} +``` diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md new file mode 100644 index 0000000000..47acb7664c --- /dev/null +++ b/_ja/tour/lower-type-bounds.md @@ -0,0 +1,70 @@ +--- +layout: tour +title: Lower Type Bounds + +discourse: true + +partof: scala-tour + +num: 21 +next-page: inner-classes +previous-page: upper-type-bounds +prerequisite-knowledge: upper-type-bounds, generics, variance + +redirect_from: "/tutorials/tour/lower-type-bounds.html" +--- + +While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of another type, *lower type bounds* declare a type to be a supertype of another type. The term `B >: A` expresses that the type parameter `B` or the abstract type `B` refer to a supertype of type `A`. In most cases, `A` will be the type parameter of the class and `B` will be the type parameter of a method. + +Here is an example where this is useful: + +```tut:fail +trait Node[+B] { + def prepend(elem: B): Node[B] +} + +case class ListNode[+B](h: B, t: Node[B]) extends Node[B] { + def prepend(elem: B): ListNode[B] = ListNode(elem, this) + def head: B = h + def tail: Node[B] = t +} + +case class Nil[+B]() extends Node[B] { + def prepend(elem: B): ListNode[B] = ListNode(elem, this) +} +``` + +This program implements a singly-linked list. `Nil` represents an empty element (i.e. an empty list). `class ListNode` is a node which contains an element of type `B` (`head`) and a reference to the rest of the list (`tail`). The `class Node` and its subtypes are covariant because we have `+B`. + +However, this program does _not_ compile because the parameter `elem` in `prepend` is of type `B`, which we declared *co*variant. This doesn't work because functions are *contra*variant in their parameter types and *co*variant in their result types. + +To fix this, we need to flip the variance of the type of the parameter `elem` in `prepend`. We do this by introducing a new type parameter `U` that has `B` as a lower type bound. + +```tut +trait Node[+B] { + def prepend[U >: B](elem: U): Node[U] +} + +case class ListNode[+B](h: B, t: Node[B]) extends Node[B] { + def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this) + def head: B = h + def tail: Node[B] = t +} + +case class Nil[+B]() extends Node[B] { + def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this) +} +``` + +Now we can do the following: +```tut +trait Bird +case class AfricanSwallow() extends Bird +case class EuropeanSwallow() extends Bird + + +val africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil()) +val birdList: Node[Bird] = africanSwallowList +birdList.prepend(new EuropeanSwallow) +``` +The `Node[Bird]` can be assigned the `africanSwallowList` but then accept `EuropeanSwallow`s. diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md new file mode 100644 index 0000000000..35a87dc664 --- /dev/null +++ b/_ja/tour/mixin-class-composition.md @@ -0,0 +1,83 @@ +--- +layout: tour +title: Class Composition with Mixins + +discourse: true + +partof: scala-tour + +num: 7 +next-page: higher-order-functions +previous-page: tuples +prerequisite-knowledge: inheritance, traits, abstract-classes, unified-types + +redirect_from: "/tutorials/tour/mixin-class-composition.html" +--- +Mixins are traits which are used to compose a class. + +```tut +abstract class A { + val message: String +} +class B extends A { + val message = "I'm an instance of class B" +} +trait C extends A { + def loudMessage = message.toUpperCase() +} +class D extends B with C + +val d = new D +println(d.message) // I'm an instance of class B +println(d.loudMessage) // I'M AN INSTANCE OF CLASS B +``` +Class `D` has a superclass `B` and a mixin `C`. Classes can only have one superclass but many mixins (using the keywords `extends` and `with` respectively). The mixins and the superclass may have the same supertype. + +Now let's look at a more interesting example starting with an abstract class: + +```tut +abstract class AbsIterator { + type T + def hasNext: Boolean + def next(): T +} +``` +The class has an abstract type `T` and the standard iterator methods. + +Next, we'll implement a concrete class (all abstract members `T`, `hasNext`, and `next` have implementations): + +```tut +class StringIterator(s: String) extends AbsIterator { + type T = Char + private var i = 0 + def hasNext = i < s.length + def next() = { + val ch = s charAt i + i += 1 + ch + } +} +``` +`StringIterator` takes a `String` and can be used to iterate over the String (e.g. to see if a String contains a certain character). + +Now let's create a trait which also extends `AbsIterator`. + +```tut +trait RichIterator extends AbsIterator { + def foreach(f: T => Unit): Unit = while (hasNext) f(next()) +} +``` +This trait implements `foreach` by continually calling the provided function `f: T => Unit` on the next element (`next()`) as long as there are further elements (`while (hasNext)`). Because `RichIterator` is a trait, it doesn't need to implement the abstract members of AbsIterator. + +We would like to combine the functionality of `StringIterator` and `RichIterator` into a single class. + +```tut +object StringIteratorTest extends App { + class RichStringIter extends StringIterator("Scala") with RichIterator + val richStringIter = new RichStringIter + richStringIter foreach println +} +``` +The new class `RichStringIter` has `StringIterator` as a superclass and `RichIterator` as a mixin. + +With single inheritance we would not be able to achieve this level of flexibility. diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md new file mode 100644 index 0000000000..e8bdb9d86b --- /dev/null +++ b/_ja/tour/multiple-parameter-lists.md @@ -0,0 +1,81 @@ +--- +layout: tour +title: Multiple Parameter Lists (Currying) + +discourse: true + +partof: scala-tour + +num: 10 +next-page: case-classes +previous-page: nested-functions + +redirect_from: "/tutorials/tour/multiple-parameter-lists.html" +--- + +Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. This is formally known as [partial application](https://en.wikipedia.org/wiki/Partial_application). + +Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections: + +``` +def foldLeft[B](z: B)(op: (B, A) => B): B +``` + +`foldLeft` applies a binary operator `op` to an initial value `z` and all elements of this traversable, going left to right. Shown below is an example of its usage. + +Starting with an initial value of 0, `foldLeft` here applies the function `(m, n) => m + n` to each element in the List and the previous accumulated value. + +```tut +val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) +val res = numbers.foldLeft(0)((m, n) => m + n) +print(res) // 55 +``` + +Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include: + +#### Single functional parameter + In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. Without multiple parameter lists, the code would look like this: + +``` +numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) +``` + + Note that the use of multiple parameter lists here also allows us to take advantage of Scala type inference to make the code more concise as shown below; which would not be possible in a non-curried definition. + +``` +numbers.foldLeft(0)(_ + _) +``` + Above statement `numbers.foldLeft(0)(_ + _)` allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: +```tut +val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) +val numberFunc = numbers.foldLeft(List[Int]())_ + +val squares = numberFunc((xs, x) => xs:+ x*x) +print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) + +val cubes = numberFunc((xs, x) => xs:+ x*x*x) +print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) +``` + + Finally, `foldLeft` and `foldRight` can be used in any of the following terms, +```tut +val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + +numbers.foldLeft(0)((sum, item) => sum + item) // Generic Form +numbers.foldRight(0)((sum, item) => sum + item) // Generic Form + +numbers.foldLeft(0)(_+_) // Curried Form +numbers.foldRight(0)(_+_) // Curried Form + +(0 /: numbers)(_+_) // Used in place of foldLeft +(numbers :\ 0)(_+_) // Used in place of foldRight +``` + + +#### Implicit parameters + To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is: + +``` +def execute(arg: Int)(implicit ec: ExecutionContext) = ??? +``` + diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md new file mode 100644 index 0000000000..d8d1d7a88e --- /dev/null +++ b/_ja/tour/named-arguments.md @@ -0,0 +1,34 @@ +--- +layout: tour +title: Named Arguments + +discourse: true + +partof: scala-tour + +num: 34 +next-page: packages-and-imports +previous-page: default-parameter-values +prerequisite-knowledge: function-syntax + +redirect_from: "/tutorials/tour/named-arguments.html" +--- + +When calling methods, you can label the arguments with their parameter names like so: + +```tut +def printName(first: String, last: String): Unit = { + println(first + " " + last) +} + +printName("John", "Smith") // Prints "John Smith" +printName(first = "John", last = "Smith") // Prints "John Smith" +printName(last = "Smith", first = "John") // Prints "John Smith" +``` +Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. + +```tut:fail +printName(last = "Smith", "john") // error: positional after named argument +``` + +Note that named arguments do not work with calls to Java methods. diff --git a/_ja/tour/nested-functions.md b/_ja/tour/nested-functions.md new file mode 100644 index 0000000000..09e95c742a --- /dev/null +++ b/_ja/tour/nested-functions.md @@ -0,0 +1,38 @@ +--- +layout: tour +title: Nested Methods + +discourse: true + +partof: scala-tour + +num: 9 +next-page: multiple-parameter-lists +previous-page: higher-order-functions + +redirect_from: "/tutorials/tour/nested-functions.html" +--- + +In Scala it is possible to nest method definitions. The following object provides a `factorial` method for computing the factorial of a given number: + +{% scalafiddle %} +```tut + def factorial(x: Int): Int = { + def fact(x: Int, accumulator: Int): Int = { + if (x <= 1) accumulator + else fact(x - 1, x * accumulator) + } + fact(x, 1) + } + + println("Factorial of 2: " + factorial(2)) + println("Factorial of 3: " + factorial(3)) +``` +{% endscalafiddle %} + +The output of this program is: + +``` +Factorial of 2: 2 +Factorial of 3: 6 +``` diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md new file mode 100644 index 0000000000..b3ad662813 --- /dev/null +++ b/_ja/tour/operators.md @@ -0,0 +1,81 @@ +--- +layout: tour +title: Operators + +discourse: true + +partof: scala-tour + +num: 30 +next-page: by-name-parameters +previous-page: type-inference +prerequisite-knowledge: case-classes + +redirect_from: "/tutorials/tour/operators.html" +--- +In Scala, operators are methods. Any method with a single parameter can be used as an _infix operator_. For example, `+` can be called with dot-notation: +``` +10.+(1) +``` + +However, it's easier to read as an infix operator: +``` +10 + 1 +``` + +## Defining and using operators +You can use any legal identifier as an operator. This includes a name like `add` or a symbol(s) like `+`. +```tut +case class Vec(val x: Double, val y: Double) { + def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y) +} + +val vector1 = Vec(1.0, 1.0) +val vector2 = Vec(2.0, 2.0) + +val vector3 = vector1 + vector2 +vector3.x // 3.0 +vector3.y // 3.0 +``` +The class Vec has a method `+` which we used to add `vector1` and `vector2`. Using parentheses, you can build up complex expressions with readable syntax. Here is the definition of class `MyBool` which includes methods `and` and `or`: + +```tut +case class MyBool(x: Boolean) { + def and(that: MyBool): MyBool = if (x) that else this + def or(that: MyBool): MyBool = if (x) this else that + def negate: MyBool = MyBool(!x) +} +``` + +It is now possible to use `and` and `or` as infix operators: + +```tut +def not(x: MyBool) = x.negate +def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y) +``` + +This helps to make the definition of `xor` more readable. + +## Precedence +When an expression uses multiple operators, the operators are evaluated based on the priority of the first character: +``` +(characters not shown below) +* / % ++ - +: += ! +< > +& +^ +| +(all letters) +``` +This applies to functions you define. For example, the following expression: +``` +a + b ^? c ?^ d less a ==> b | c +``` +Is equivalent to +``` +((a + b) ^? (c ?^ d)) less ((a ==> b) | c) +``` +`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `==>`, `^?`, `|`, and `less`. diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md new file mode 100644 index 0000000000..628a56d3a0 --- /dev/null +++ b/_ja/tour/package-objects.md @@ -0,0 +1,77 @@ +--- +layout: tour +title: Package Objects + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +# Package objects + +Scala provides package objects as a convenient container shared across an entire package. + +Package objects +can contain arbitrary definitions, not just variable and method definitions. For instance, they are frequently +used to hold package-wide type aliases and implicit conversions. Package objects can even inherit +Scala classes and traits. + +By convention, the source code for a package object is usually put in a source file named `package.scala`. + +Each package is allowed to have one package object. Any definitions placed in a package object are considered +members of the package itself. + +See example below. Assume first a class `Fruit` and three `Fruit` objects in a package +`gardening.fruits`: + +``` +// in file gardening/fruits/Fruit.scala +package gardening.fruits + +case class Fruit(name: String, color: String) +object Apple extends Fruit("Apple", "green") +object Plum extends Fruit("Plum", "blue") +object Banana extends Fruit("Banana", "yellow") +``` + +Now assume you want to place a variable `planted` and a method `showFruit` directly into package `gardening.fruits`. +Here's how this is done: + +``` +// in file gardening/fruits/package.scala +package gardening +package object fruits { + val planted = List(Apple, Plum, Banana) + def showFruit(fruit: Fruit): Unit = { + println(s"${fruit.name}s are ${fruit.color}") + } +} +``` + +As an example of how the use site looks, the following object `PrintPlanted` imports `planted` and `showFruit` in exactly the same +way it imports class `Fruit`, using a wildcard import on package gardening.fruits: + +``` +// in file PrintPlanted.scala +import gardening.fruits._ +object PrintPlanted { + def main(args: Array[String]): Unit = { + for (fruit <- fruits.planted) { + showFruit(fruit) + } + } +} +``` + +Package objects are like other objects, which means you can use inheritance for building them. For example, one might mix in a couple of traits: + +``` +package object fruits extends FruitAliases with FruitHelpers { + // helpers and variables follows here +} +``` + +Note that method overloading doesn't work in package objects. diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md new file mode 100644 index 0000000000..68efdd8083 --- /dev/null +++ b/_ja/tour/packages-and-imports.md @@ -0,0 +1,85 @@ +--- +layout: tour +title: Packages and Imports + +discourse: true + +partof: scala-tour + +num: 35 +previous-page: named-arguments +next-page: package-objects +--- + +# Packages and Imports +Scala uses packages to create namespaces which allow you to modularize programs. + +## Creating a package +Packages are created by declaring one or more package names at the top of a Scala file. + +``` +package users + +class User +``` +One convention is to name the package the same as the directory containing the Scala file. However, Scala is agnostic to file layout. The directory structure of an sbt project for `package users` might look like this: +``` +- ExampleProject + - build.sbt + - project + - src + - main + - scala + - users + User.scala + UserProfile.scala + UserPreferences.scala + - test +``` +Notice how the `users` directory is within the `scala` directory and how there are multiple Scala files within the package. Each Scala file in the package could have the same package declaration. The other way to declare packages is by using braces: +``` +package users { + package administrators { + class NormalUser + } + package normalusers { + class NormalUser + } +} +``` +As you can see, this allows for package nesting and provides greater control for scope and encapsulation. + +The package name should be all lower case and if the code is being developed within an organization which has a website, it should be the following format convention: `..`. For example, if Google had a project called `SelfDrivingCar`, the package name would look like this: +``` +package com.google.selfdrivingcar.camera + +class Lens +``` +This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`. + +## Imports +`import` clauses are for accessing members (classes, traits, functions, etc.) in other packages. An `import` clause is not required for accessing members of the same package. Import clauses are selective: +``` +import users._ // import everything from the users package +import users.User // import the class User +import users.{User, UserPreferences} // Only imports selected members +import users.{UserPreferences => UPrefs} // import and rename for convenience +``` + +One way in which Scala is different from Java is that imports can be used anywhere: + +```tut +def sqrtplus1(x: Int) = { + import scala.math.sqrt + sqrt(x) + 1.0 +} +``` +In the event there is a naming conflict and you need to import something from the root of the project, prefix the package name with `_root_`: +``` +package accounts + +import _root_.users._ +``` + + +Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default. diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md new file mode 100644 index 0000000000..5a6d4969c4 --- /dev/null +++ b/_ja/tour/pattern-matching.md @@ -0,0 +1,151 @@ +--- +layout: tour +title: Pattern Matching + +discourse: true + +partof: scala-tour + +num: 12 + +next-page: singleton-objects +previous-page: case-classes +prerequisite-knowledge: case-classes, string-interpolation, subtyping + +redirect_from: "/tutorials/tour/pattern-matching.html" +--- + +Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the `switch` statement in Java and it can likewise be used in place of a series of if/else statements. + +## Syntax +A match expression has a value, the `match` keyword, and at least one `case` clause. +```tut +import scala.util.Random + +val x: Int = Random.nextInt(10) + +x match { + case 0 => "zero" + case 1 => "one" + case 2 => "two" + case _ => "many" +} +``` +The `val x` above is a random integer between 0 and 10. `x` becomes the left operand of the `match` operator and on the right is an expression with four cases. The last case `_` is a "catch all" case for any number greater than 2. Cases are also called _alternatives_. + +Match expressions have a value. +```tut +def matchTest(x: Int): String = x match { + case 1 => "one" + case 2 => "two" + case _ => "many" +} +matchTest(3) // many +matchTest(1) // one +``` +This match expression has a type String because all of the cases return String. Therefore, the function `matchTest` returns a String. + +## Matching on case classes + +Case classes are especially useful for pattern matching. + +```tut +abstract class Notification + +case class Email(sender: String, title: String, body: String) extends Notification + +case class SMS(caller: String, message: String) extends Notification + +case class VoiceRecording(contactName: String, link: String) extends Notification + + +``` +`Notification` is an abstract super class which has three concrete Notification types implemented with case classes `Email`, `SMS`, and `VoiceRecording`. Now we can do pattern matching on these case classes: + +``` +def showNotification(notification: Notification): String = { + notification match { + case Email(email, title, _) => + s"You got an email from $email with title: $title" + case SMS(number, message) => + s"You got an SMS from $number! Message: $message" + case VoiceRecording(name, link) => + s"you received a Voice Recording from $name! Click the link to hear it: $link" + } +} +val someSms = SMS("12345", "Are you there?") +val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") + +println(showNotification(someSms)) // prints You got an SMS from 12345! Message: Are you there? + +println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123 +``` +The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(email, title, _)` the fields `email` and `title` are used in the return value but the `body` field is ignored with `_`. + +## Pattern guards +Pattern guards are simply boolean expressions which are used to make cases more specific. Just add `if ` after the pattern. +``` + +def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = { + notification match { + case Email(email, _, _) if importantPeopleInfo.contains(email) => + "You got an email from special someone!" + case SMS(number, _) if importantPeopleInfo.contains(number) => + "You got an SMS from special someone!" + case other => + showNotification(other) // nothing special, delegate to our original showNotification function + } +} + +val importantPeopleInfo = Seq("867-5309", "jenny@gmail.com") + +val someSms = SMS("867-5309", "Are you there?") +val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") +val importantEmail = Email("jenny@gmail.com", "Drinks tonight?", "I'm free after 5!") +val importantSms = SMS("867-5309", "I'm here! Where are you?") + +println(showImportantNotification(someSms, importantPeopleInfo)) +println(showImportantNotification(someVoiceRecording, importantPeopleInfo)) +println(showImportantNotification(importantEmail, importantPeopleInfo)) +println(showImportantNotification(importantSms, importantPeopleInfo)) +``` + +In the `case Email(email, _, _) if importantPeopleInfo.contains(email)`, the pattern is matched only if the `email` is in the list of important people. + +## Matching on type only +You can match on the type like so: +```tut +abstract class Device +case class Phone(model: String) extends Device{ + def screenOff = "Turning screen off" +} +case class Computer(model: String) extends Device { + def screenSaverOn = "Turning screen saver on..." +} + +def goIdle(device: Device) = device match { + case p: Phone => p.screenOff + case c: Computer => c.screenSaverOn +} +``` +`def goIdle` has a different behavior depending on the type of `Device`. This is useful when the case needs to call a method on the pattern. It is a convention to use the first letter of the type as the case identifier (`p` and `c` in this case). + +## Sealed classes +Traits and classes can be marked `sealed` which means all subtypes must be declared in the same file. This assures that all subtypes are known. + +```tut +sealed abstract class Furniture +case class Couch() extends Furniture +case class Chair() extends Furniture + +def findPlaceToSit(piece: Furniture): String = piece match { + case a: Couch => "Lie on the couch" + case b: Chair => "Sit on the chair" +} +``` +This is useful for pattern matching because we don't need a "catch all" case. + +## Notes + +Scala's pattern matching statement is most useful for matching on algebraic types expressed via [case classes](case-classes.html). +Scala also allows the definition of patterns independently of case classes, using `unapply` methods in [extractor objects](extractor-objects.html). diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md new file mode 100644 index 0000000000..8f9cb4525f --- /dev/null +++ b/_ja/tour/polymorphic-methods.md @@ -0,0 +1,37 @@ +--- +layout: tour +title: Polymorphic Methods + +discourse: true + +partof: scala-tour + +num: 28 + +next-page: type-inference +previous-page: implicit-conversions +prerequisite-knowledge: unified-types + +redirect_from: "/tutorials/tour/polymorphic-methods.html" +--- + +Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses. + +Here is an example: + +```tut +def listOfDuplicates[A](x: A, length: Int): List[A] = { + if (length < 1) + Nil + else + x :: listOfDuplicates(x, length - 1) +} +println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3) +println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) +``` + +The method `listOfDuplicates` takes a type parameter `A` and value parameters `x` and `length`. Value `x` is of type `A`. If `length < 1` we return an empty list. Otherwise we prepend `x` to the the list of duplicates returned by the recursive call. (Note that `::` means prepend an element on the left to a list on the right.) + +In first example call, we explicitly provide the type parameter by writing `[Int]`. Therefore the first argument must be an `Int` and the return type will be `List[Int]`. + +The second example call shows that you don't always need to explicitly provide the type parameter. The compiler can often infer it based on context or on the types of the value arguments. In this example, `"La"` is a `String` so the compiler knows `A` must be `String`. diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md new file mode 100644 index 0000000000..ec07f9f368 --- /dev/null +++ b/_ja/tour/regular-expression-patterns.md @@ -0,0 +1,63 @@ +--- +layout: tour +title: Regular Expression Patterns + +discourse: true + +partof: scala-tour + +num: 15 + +next-page: extractor-objects +previous-page: singleton-objects + +redirect_from: "/tutorials/tour/regular-expression-patterns.html" +--- + +Regular expressions are strings which can be used to find patterns (or lack thereof) in data. Any string can be converted to a regular expression using the `.r` method. + +```tut +import scala.util.matching.Regex + +val numberPattern: Regex = "[0-9]".r + +numberPattern.findFirstMatchIn("awesomepassword") match { + case Some(_) => println("Password OK") + case None => println("Password must contain a number") +} +``` + +In the above example, the `numberPattern` is a `Regex` +(regular expression) which we use to make sure a password contains a number. + +You can also search for groups of regular expressions using parentheses. + +```tut +import scala.util.matching.Regex + +val keyValPattern: Regex = "([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)".r + +val input: String = + """background-color: #A03300; + |background-image: url(img/header100.png); + |background-position: top center; + |background-repeat: repeat-x; + |background-size: 2160px 108px; + |margin: 0; + |height: 108px; + |width: 100%;""".stripMargin + +for (patternMatch <- keyValPattern.findAllMatchIn(input)) + println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}") +``` +Here we parse out the keys and values of a String. Each match has a group of sub-matches. Here is the output: +``` +key: background-color value: #A03300 +key: background-image value: url(img +key: background-position value: top center +key: background-repeat value: repeat-x +key: background-size value: 2160px 108px +key: margin value: 0 +key: height value: 108px +key: width value: 100 +``` diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md new file mode 100644 index 0000000000..f54e62ae18 --- /dev/null +++ b/_ja/tour/self-types.md @@ -0,0 +1,40 @@ +--- +layout: tour +title: Self-type + +discourse: true + +partof: scala-tour + +num: 25 +next-page: implicit-parameters +previous-page: compound-types +topics: self-types +prerequisite-knowledge: nested-classes, mixin-class-composition + +redirect_from: "/tutorials/tour/self-types.html" +--- +Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn't directly extend it. That makes the members of the dependency available without imports. + +A self-type is a way to narrow the type of `this` or another identifier that aliases `this`. The syntax looks like normal function syntax but means something entirely different. + +To use a self-type in a trait, write an identifier, the type of another trait to mix in, and a `=>` (e.g. `someIdentifier: SomeOtherTrait =>`). +```tut +trait User { + def username: String +} + +trait Tweeter { + this: User => // reassign this + def tweet(tweetText: String) = println(s"$username: $tweetText") +} + +class VerifiedTweeter(val username_ : String) extends Tweeter with User { // We mixin User because Tweeter required it + def username = s"real $username_" +} + +val realBeyoncé = new VerifiedTweeter("Beyoncé") +realBeyoncé.tweet("Just spilled my glass of lemonade") // prints "real Beyoncé: Just spilled my glass of lemonade" +``` + +Because we said `this: User =>` in `trait Tweeter`, now the variable `username` is in scope for the `tweet` method. This also means that since `VerifiedTweeter` extends `Tweeter`, it must also mix-in `User` (using `with User`). diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md new file mode 100644 index 0000000000..2e05c851f2 --- /dev/null +++ b/_ja/tour/singleton-objects.md @@ -0,0 +1,110 @@ +--- +layout: tour +title: Singleton Objects + +discourse: true + +partof: scala-tour + +num: 13 + +next-page: regular-expression-patterns +previous-page: pattern-matching +redirect_from: "/tutorials/tour/singleton-objects.html" +prerequisite-knowledge: classes, methods, private-methods, packages, option +--- +An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val. + +As a top-level value, an object is a singleton. + +As a member of an enclosing class or as a local value, it behaves exactly like a lazy val. +# Defining a singleton object +An object is a value. The definition of an object looks like a class, but uses the keyword `object`: +```tut +object Box +``` + +Here's an example of an object with a method: +``` +package logging + +object Logger { + def info(message: String): Unit = println(s"INFO: $message") +} +``` +The method `info` can be imported from anywhere in the program. Creating utility methods like this is a common use case for singleton objects. + +Let's see how to use `info` in another package: + +``` +import logging.Logger.info + +class Project(name: String, daysToComplete: Int) + +class Test { + val project1 = new Project("TPS Reports", 1) + val project2 = new Project("Website redesign", 5) + info("Created projects") // Prints "INFO: Created projects" +} +``` + +The `info` method is visible because of the import statement, `import logging.Logger.info`. + +Imports require a "stable path" to the imported symbol, and an object is a stable path. + +Note: If an `object` is not top-level but is nested in another class or object, then the object is "path-dependent" like any other member. This means that given two kinds of beverages, `class Milk` and `class OrangeJuice`, a class member `object NutritionInfo` "depends" on the enclosing instance, either milk or orange juice. `milk.NutritionInfo` is entirely distinct from `oj.NutritionInfo`. + +## Companion objects + +An object with the same name as a class is called a _companion object_. Conversely, the class is the object's companion class. A companion class or object can access the private members of its companion. Use a companion object for methods and values which are not specific to instances of the companion class. +``` +import scala.math._ + +case class Circle(radius: Double) { + import Circle._ + def area: Double = calculateArea(radius) +} + +object Circle { + private def calculateArea(radius: Double): Double = Pi * pow(radius, 2.0) +} + +val circle1 = new Circle(5.0) + +circle1.area +``` + +The `class Circle` has a member `area` which is specific to each instance, and the singleton `object Circle` has a method `calculateArea` which is available to every instance. + +The companion object can also contain factory methods: +```tut +class Email(val username: String, val domainName: String) + +object Email { + def fromString(emailString: String): Option[Email] = { + emailString.split('@') match { + case Array(a, b) => Some(new Email(a, b)) + case _ => None + } + } +} + +val scalaCenterEmail = Email.fromString("scala.center@epfl.ch") +scalaCenterEmail match { + case Some(email) => println( + s"""Registered an email + |Username: ${email.username} + |Domain name: ${email.domainName} + """) + case None => println("Error: could not parse email") +} +``` +The `object Email` contains a factory `fromString` which creates an `Email` instance from a String. We return it as an `Option[Email]` in case of parsing errors. + +Note: If a class or object has a companion, both must be defined in the same file. To define companions in the REPL, either define them on the same line or enter `:paste` mode. + +## Notes for Java programmers ## + +`static` members in Java are modeled as ordinary members of a companion object in Scala. + +When using a companion object from Java code, the members will be defined in a companion class with a `static` modifier. This is called _static forwarding_. It occurs even if you haven't defined a companion class yourself. diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md new file mode 100644 index 0000000000..121255457d --- /dev/null +++ b/_ja/tour/traits.md @@ -0,0 +1,84 @@ +--- +layout: tour +title: Traits + +discourse: true + +partof: scala-tour + +num: 5 +next-page: tuples +previous-page: classes +topics: traits +prerequisite-knowledge: expressions, classes, generics, objects, companion-objects + +redirect_from: "/tutorials/tour/traits.html" +--- + +Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters. + +## Defining a trait +A minimal trait is simply the keyword `trait` and an identifier: + +```tut +trait HairColor +``` + +Traits become especially useful as generic types and with abstract methods. +```tut +trait Iterator[A] { + def hasNext: Boolean + def next(): A +} +``` + +Extending the `trait Iterator[A]` requires a type `A` and implementations of the methods `hasNext` and `next`. + +## Using traits +Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword: +```tut +trait Iterator[A] { + def hasNext: Boolean + def next(): A +} + +class IntIterator(to: Int) extends Iterator[Int] { + private var current = 0 + override def hasNext: Boolean = current < to + override def next(): Int = { + if (hasNext) { + val t = current + current += 1 + t + } else 0 + } +} + + +val iterator = new IntIterator(10) +iterator.next() // returns 0 +iterator.next() // returns 1 +``` +This `IntIterator` class takes a parameter `to` as an upper bound. It `extends Iterator[Int]` which means that the `next` method must return an Int. + +## Subtyping +Where a given trait is required, a subtype of the trait can be used instead. +```tut +import scala.collection.mutable.ArrayBuffer + +trait Pet { + val name: String +} + +class Cat(val name: String) extends Pet +class Dog(val name: String) extends Pet + +val dog = new Dog("Harry") +val cat = new Cat("Sally") + +val animals = ArrayBuffer.empty[Pet] +animals.append(dog) +animals.append(cat) +animals.foreach(pet => println(pet.name)) // Prints Harry Sally +``` +The `trait Pet` has an abstract field `name` which gets implemented by Cat and Dog in their constructors. On the last line, we call `pet.name` which must be implemented in any subtype of the trait `Pet`. diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md new file mode 100644 index 0000000000..e945311a0e --- /dev/null +++ b/_ja/tour/tuples.md @@ -0,0 +1,95 @@ +--- +layout: tour +title: Tuples + +discourse: true + +partof: scala-tour + +num: 6 +next-page: mixin-class-composition +previous-page: traits +topics: tuples + +redirect_from: "/tutorials/tour/tuples.html" +--- + +In Scala, a tuple is a class that can hold elements of different types. +Tuples are immutable. + +Tuples come in handy when we have to return multiple values from a function. + +A tuple can be created as: + +```tut +val ingredient = ("Sugar" , 25):Tuple2[String, Int] +``` +This creates a tuple containing a String element and an Int element. + +Tuple in Scala is a series of classes: Tuple2, Tuple3, etc., through Tuple22. +So when we create a tuple with n elements(n lying between 2 and 22), Scala basically instantiates +one of the corresponding classes from the group, parameterized with types of constituent elements. +For eg., ingredient is of type Tuple2[String, Int]. + +## Accessing the elements + +Tuple elements are accessed using underscore syntax. +'tuple._n' gives nth element(given there are that many elements). + +```tut +println(ingredient._1) // Sugar + +println(ingredient._2) // 25 +``` + +## Destructuring tuple data + +Scala tuple also supports destructuring. + +```tut +val (name, quantity) = ingredient + +println(name) // Sugar + +println(quantity) // 25 +``` + +Tuple destructuring can be used in pattern matching too. + +```tut +val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3)) + +planetDistanceFromSun.foreach{ tuple => { + + tuple match { + + case ("Mercury", distance) => println(s"Mercury is $distance millions km far from Sun") + + case p if(p._1 == "Venus") => println(s"Venus is ${p._2} millions km far from Sun") + + case p if(p._1 == "Earth") => println(s"Blue planet is ${p._2} millions km far from Sun") + + case _ => println("Too far....") + + } + + } + +} +``` + +Or, in 'for' comprehension. + +```tut +val numPairs = List((2, 5), (3, -7), (20, 56)) + +for ((a, b) <- numPairs) { + + println(a * b) + +} +``` + +The value () of type Unit is conceptually the same as the value () of type Tuple0. There can only be one value of this type since it has no elements. + +Users may sometimes find hard to choose between Tuples and case classes. As a rule, case classes are preferred choice if elements carry more meaning. diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md new file mode 100644 index 0000000000..d949fc4f4b --- /dev/null +++ b/_ja/tour/type-inference.md @@ -0,0 +1,74 @@ +--- +layout: tour +title: Type Inference + +discourse: true + +partof: scala-tour + +num: 29 +next-page: operators +previous-page: polymorphic-methods +--- + +The Scala compiler can often infer the type of an expression so you don't have to declare it explicitly. + +## Omitting the type + +```tut +val businessName = "Montreux Jazz Café" +``` +The compiler can detect that `businessName` is a String. It works similarly with methods: + +```tut +def squareOf(x: Int) = x * x +``` +The compiler can infer that the return type is an `Int`, so no explicit return type is required. + +For recursive methods, the compiler is not able to infer a result type. Here is a program which will fail the compiler for this reason: + +```tut:fail +def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1) +``` + +It is also not compulsory to specify type parameters when [polymorphic methods](polymorphic-methods.html) are called or [generic classes](generic-classes.html) are instantiated. The Scala compiler will infer such missing type parameters from the context and from the types of the actual method/constructor parameters. + +Here are two examples: + +```tut +case class MyPair[A, B](x: A, y: B); +val p = MyPair(1, "scala") // type: MyPair[Int, String] + +def id[T](x: T) = x +val q = id(1) // type: Int +``` + +The compiler uses the types of the arguments of `MyPair` to figure out what type `A` and `B` are. Likewise for the type of `x`. + +## Parameters + +The compiler never infers method parameter types. However, in certain cases, it can infer anonymous function parameter types when the function is passed as argument. + +```tut +Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) +``` + +The parameter for map is `f: A => B`. Because we put integers in the `Seq`, the compiler knows that `A` is `Int` (i.e. that `x` is an integer). Therefore, the compiler can infer from `x * 2` that `B` is type `Int`. + +## When _not_ to rely on type inference + +It is generally considered more readable to declare the type of members exposed in a public API. Therefore, we recommended that you make the type explicit for any APIs that will be exposed to users of your code. + +Also, type inference can sometimes infer a too-specific type. Suppose we write: + +```tut +var obj = null +``` + +We can't then go on and make this reassignment: + +```tut:fail +obj = new AnyRef +``` + +It won't compile, because the type inferred for `obj` was `Null`. Since the only value of that type is `null`, it is impossible to assign a different value. diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md new file mode 100644 index 0000000000..2c4d5bb239 --- /dev/null +++ b/_ja/tour/unified-types.md @@ -0,0 +1,82 @@ +--- +layout: tour +title: Unified Types + +discourse: true + +partof: scala-tour + +num: 3 +next-page: classes +previous-page: basics +prerequisite-knowledge: classes, basics + +redirect_from: "/tutorials/tour/unified-types.html" +--- + +In Scala, all values have a type, including numerical values and functions. The diagram below illustrates a subset of the type hierarchy. + +Scala Type Hierarchy + +## Scala Type Hierarchy ## + +[`Any`](http://www.scala-lang.org/api/2.12.1/scala/Any.html) is the supertype of all types, also called the top type. It defines certain universal methods such as `equals`, `hashCode`, and `toString`. `Any` has two direct subclasses: `AnyVal` and `AnyRef`. + +`AnyVal` represents value types. There are nine predefined value types and they are non-nullable: `Double`, `Float`, `Long`, `Int`, `Short`, `Byte`, `Char`, `Unit`, and `Boolean`. `Unit` is a value type which carries no meaningful information. There is exactly one instance of `Unit` which can be declared literally like so: `()`. All functions must return something so sometimes `Unit` is a useful return type. + +`AnyRef` represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of `AnyRef`. If Scala is used in the context of a Java runtime environment, `AnyRef` corresponds to `java.lang.Object`. + +Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object: + +```tut +val list: List[Any] = List( + "a string", + 732, // an integer + 'c', // a character + true, // a boolean value + () => "an anonymous function returning a string" +) + +list.foreach(element => println(element)) +``` + +It defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. + +Here is the output of the program: + +``` +a string +732 +c +true + +``` + +## Type Casting +Value types can be cast in the following way: +Scala Type Hierarchy + +For example: + +```tut +val x: Long = 987654321 +val y: Float = x // 9.8765434E8 (note that some precision is lost in this case) + +val face: Char = '☺' +val number: Int = face // 9786 +``` + +Casting is unidirectional. This will not compile: + +``` +val x: Long = 987654321 +val y: Float = x // 9.8765434E8 +val z: Long = y // Does not conform +``` + +You can also cast a reference type to a subtype. This will be covered later in the tour. + +## Nothing and Null +`Nothing` is a subtype of all types, also called the bottom type. There is no value that has type `Nothing`. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally). + +`Null` is a subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal `null`. `Null` is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We'll cover alternatives to `null` later in the tour. diff --git a/_ja/tour/upper-type-bounds.md b/_ja/tour/upper-type-bounds.md new file mode 100644 index 0000000000..30df5918f7 --- /dev/null +++ b/_ja/tour/upper-type-bounds.md @@ -0,0 +1,54 @@ +--- +layout: tour +title: Upper Type Bounds + +discourse: true + +partof: scala-tour +categories: tour +num: 20 +next-page: lower-type-bounds +previous-page: variances + +redirect_from: "/tutorials/tour/upper-type-bounds.html" +--- + +In Scala, [type parameters](generic-classes.html) and [abstract type members](abstract-type-members.html) may be constrained by a type bound. Such type bounds limit the concrete values of the type variables and possibly reveal more information about the members of such types. An _upper type bound_ `T <: A` declares that type variable `T` refers to a subtype of type `A`. +Here is an example that demonstrates upper type bound for a type parameter of class `PetContainer`: + +```tut +abstract class Animal { + def name: String +} + +abstract class Pet extends Animal {} + +class Cat extends Pet { + override def name: String = "Cat" +} + +class Dog extends Pet { + override def name: String = "Dog" +} + +class Lion extends Animal { + override def name: String = "Lion" +} + +class PetContainer[P <: Pet](p: P) { + def pet: P = p +} + +val dogContainer = new PetContainer[Dog](new Dog) +val catContainer = new PetContainer[Cat](new Cat) +``` + +```tut:fail +// this would not compile +val lionContainer = new PetContainer[Lion](new Lion) +``` +The `class PetContainer` take a type parameter `P` which must be a subtype of `Pet`. `Dog` and `Cat` are subtypes of `Pet` so we can create a new `PetContainer[Dog]` and `PetContainer[Cat]`. However, if we tried to create a `PetContainer[Lion]`, we would get the following Error: + +`type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]` + +This is because `Lion` is not a subtype of `Pet`. diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md new file mode 100644 index 0000000000..f7f33947bd --- /dev/null +++ b/_ja/tour/variances.md @@ -0,0 +1,154 @@ +--- +layout: tour +title: Variances + +discourse: true + +partof: scala-tour + +num: 19 +next-page: upper-type-bounds +previous-page: generic-classes + +redirect_from: "/tutorials/tour/variances.html" +--- + +Variance is the correlation of subtyping relationships of complex types and the subtyping relationships of their component types. Scala supports variance annotations of type parameters of [generic classes](generic-classes.html), to allow them to be covariant, contravariant, or invariant if no annotations are used. The use of variance in the type system allows us to make intuitive connections between complex types, whereas the lack of variance can restrict the reuse of a class abstraction. + +```tut +class Foo[+A] // A covariant class +class Bar[-A] // A contravariant class +class Baz[A] // An invariant class +``` + +### Covariance + +A type parameter `A` of a generic class can be made covariant by using the annotation `+A`. For some `class List[+A]`, making `A` covariant implies that for two types `A` and `B` where `A` is a subtype of `B`, then `List[A]` is a subtype of `List[B]`. This allows us to make very useful and intuitive subtyping relationships using generics. + +Consider this simple class structure: + +```tut +abstract class Animal { + def name: String +} +case class Cat(name: String) extends Animal +case class Dog(name: String) extends Animal +``` + +Both `Cat` and `Dog` are subtypes of `Animal`. The Scala standard library has a generic immutable `sealed abstract class List[+A]` class, where the type parameter `A` is covariant. This means that a `List[Cat]` is a `List[Animal]` and a `List[Dog]` is also a `List[Animal]`. Intuitively, it makes sense that a list of cats and a list of dogs are each lists of animals, and you should be able to substitute either of them for a `List[Animal]`. + +In the following example, the method `printAnimalNames` will accept a list of animals as an argument and print their names each on a new line. If `List[A]` were not covariant, the last two method calls would not compile, which would severely limit the usefulness of the `printAnimalNames` method. + +```tut +object CovarianceTest extends App { + def printAnimalNames(animals: List[Animal]): Unit = { + animals.foreach { animal => + println(animal.name) + } + } + + val cats: List[Cat] = List(Cat("Whiskers"), Cat("Tom")) + val dogs: List[Dog] = List(Dog("Fido"), Dog("Rex")) + + printAnimalNames(cats) + // Whiskers + // Tom + + printAnimalNames(dogs) + // Fido + // Rex +} +``` + +### Contravariance + +A type parameter `A` of a generic class can be made contravariant by using the annotation `-A`. This creates a subtyping relationship between the class and its type parameter that is similar, but opposite to what we get with covariance. That is, for some `class Writer[-A]`, making `A` contravariant implies that for two types `A` and `B` where `A` is a subtype of `B`, `Writer[B]` is a subtype of `Writer[A]`. + +Consider the `Cat`, `Dog`, and `Animal` classes defined above for the following example: + +```tut +abstract class Printer[-A] { + def print(value: A): Unit +} +``` + +A `Printer[A]` is a simple class that knows how to print out some type `A`. Let's define some subclasses for specific types: + +```tut +class AnimalPrinter extends Printer[Animal] { + def print(animal: Animal): Unit = + println("The animal's name is: " + animal.name) +} + +class CatPrinter extends Printer[Cat] { + def print(cat: Cat): Unit = + println("The cat's name is: " + cat.name) +} +``` + +If a `Printer[Cat]` knows how to print any `Cat` to the console, and a `Printer[Animal]` knows how to print any `Animal` to the console, it makes sense that a `Printer[Animal]` would also know how to print any `Cat`. The inverse relationship does not apply, because a `Printer[Cat]` does not know how to print any `Animal` to the console. Therefore, we should be able to substitute a `Printer[Animal]` for a `Printer[Cat]`, if we wish, and making `Printer[A]` contravariant allows us to do exactly that. + +```tut +object ContravarianceTest extends App { + val myCat: Cat = Cat("Boots") + + def printMyCat(printer: Printer[Cat]): Unit = { + printer.print(myCat) + } + + val catPrinter: Printer[Cat] = new CatPrinter + val animalPrinter: Printer[Animal] = new AnimalPrinter + + printMyCat(catPrinter) + printMyCat(animalPrinter) +} +``` + +The output of this program will be: + +``` +The cat's name is: Boots +The animal's name is: Boots +``` + +### Invariance + +Generic classes in Scala are invariant by default. This means that they are neither covariant nor contravariant. In the context of the following example, `Container` class is invariant. A `Container[Cat]` is _not_ a `Container[Animal]`, nor is the reverse true. + +```tut +class Container[A](value: A) { + private var _value: A = value + def getValue: A = _value + def setValue(value: A): Unit = { + _value = value + } +} +``` + +It may seem like a `Container[Cat]` should naturally also be a `Container[Animal]`, but allowing a mutable generic class to be covariant would not be safe. In this example, it is very important that `Container` is invariant. Supposing `Container` was actually covariant, something like this could happen: + +``` +val catContainer: Container[Cat] = new Container(Cat("Felix")) +val animalContainer: Container[Animal] = catContainer +animalContainer.setValue(Dog("Spot")) +val cat: Cat = catContainer.getValue // Oops, we'd end up with a Dog assigned to a Cat +``` + +Fortunately, the compiler stops us long before we could get this far. + +### Other Examples + +Another example that can help one understand variance is `trait Function1[-T, +R]` from the Scala standard library. `Function1` represents a function with one parameter, where the first type parameter `T` represents the parameter type, and the second type parameter `R` represents the return type. A `Function1` is contravariant over its argument type, and covariant over its return type. For this example we'll use the literal notation `A => B` to represent a `Function1[A, B]`. + +Assume the similar `Cat`, `Dog`, `Animal` inheritance tree used earlier, plus the following: + +```tut +abstract class SmallAnimal extends Animal +case class Mouse(name: String) extends SmallAnimal +``` + +Suppose we're working with functions that accept types of animals, and return the types of food they eat. If we would like a `Cat => SmallAnimal` (because cats eat small animals), but are given a `Animal => Mouse` instead, our program will still work. Intuitively an `Animal => Mouse` will still accept a `Cat` as an argument, because a `Cat` is an `Animal`, and it returns a `Mouse`, which is also a `SmallAnimal`. Since we can safely and invisibly substitute the former by the latter, we can say `Animal => Mouse` is a subtype of `Cat => SmallAnimal`. + +### Comparison With Other Languages + +Variance is supported in different ways by some languages that are similar to Scala. For example, variance annotations in Scala closely resemble those in C#, where the annotations are added when a class abstraction is defined (declaration-site variance). In Java, however, variance annotations are given by clients when a class abstraction is used (use-site variance). From 7a3dca92b926c76aec496c20e48d59b404d7582d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 24 Jan 2019 17:31:06 +0900 Subject: [PATCH 004/358] Japanese translation of TOUR OF SCALA basics --- _ja/tour/basics.md | 156 +++++++++++++++++++++++---------------------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index c9a0486c27..4a86970558 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -1,6 +1,7 @@ --- layout: tour -title: Basics +title: 基本 +language: ja discourse: true @@ -13,28 +14,29 @@ previous-page: tour-of-scala redirect_from: "/tutorials/tour/basics.html" --- -In this page, we will cover basics of Scala. +このページでは、Scalaの基本を取り扱います。 -## Trying Scala in the Browser +## Scalaをブラウザで試してみる -You can run Scala in your browser with ScalaFiddle. +ScalaFiddleを利用することでブラウザ上でScalaを実行することができます。 -1. Go to [https://scalafiddle.io](https://scalafiddle.io). -2. Paste `println("Hello, world!")` in the left pane. -3. Hit "Run" button. Output appears in the right pane. +1. [https://scalafiddle.io](https://scalafiddle.io)を開きます。 +2. 左側のパネルに`println("Hello, world!")`を貼り付けます。 +3. "Run"ボタンを押すと、右側のパネルに出力が表示されます。 -This is an easy, zero-setup way to experiment with pieces of Scala code. +このサイトを使えば、簡単にセットアップせずScalaのコードの一部を試すことができます。 -Many of the code examples in this documentation are also integrated with ScalaFiddle, so you -can directly experiment with them simply by clicking the Run-button. +このドキュメントの多くのコードの例はScalaFiddleで開発されています。 +そのため、サンプルコード内のRunボタンをクリックするだけで、そのまま簡単にコードを試すことができます。 -## Expressions +## 式 + +式は計算可能な文です。 -Expressions are computable statements. ``` 1 + 1 ``` -You can output results of expressions using `println`. +`println`を使うことで、式の結果を出力できます。 {% scalafiddle %} ```tut @@ -45,54 +47,55 @@ println("Hello," + " world!") // Hello, world! ``` {% endscalafiddle %} -### Values +### 値 -You can name results of expressions with the `val` keyword. +`val`キーワードを利用することで、式の結果に名前を付けることができます。 ```tut val x = 1 + 1 println(x) // 2 ``` -Named results, such as `x` here, are called values. Referencing -a value does not re-compute it. +ここでいうところの `x` のように、名前をつけられた結果は 値 と呼ばれます。 +値を参照した場合、その値は再計算されません。 -Values cannot be re-assigned. +値は再代入することができません。 ```tut:fail -x = 3 // This does not compile. +x = 3 // この記述はコンパイルされません。 ``` -Types of values can be inferred, but you can also explicitly state the type, like this: +値の型は推測可能ですが、このように型を明示的に宣言することもできます。 ```tut val x: Int = 1 + 1 ``` -Notice how the type declaration `Int` comes after the identifier `x`. You also need a `:`. +型定義では`Int` は識別子`x`の後にくることに注意してください。そして`:`も必要となります。 -### Variables +### 変数 -Variables are like values, except you can re-assign them. You can define a variable with the `var` keyword. +再代入ができおることを除けば、変数は値と似ています。 +`var`キーワードを使うことで、変数は定義できます。 ```tut var x = 1 + 1 -x = 3 // This compiles because "x" is declared with the "var" keyword. +x = 3 // "x"は"var"キーワードで宣言されているので、これはコンパイルされます。 println(x * x) // 9 ``` -As with values, you can explicitly state the type if you want: +値と同様に、型を宣言したければ、明示的に型を宣言することができます。 ```tut var x: Int = 1 + 1 ``` -## Blocks +## ブロック -You can combine expressions by surrounding them with `{}`. We call this a block. +`{}`で囲むことで式をまとめることができます。これをブロックと呼びます。 -The result of the last expression in the block is the result of the overall block, too. +ブロックの最後の式の結果はブロック全体の結果にもなります。 ```tut println({ @@ -101,19 +104,17 @@ println({ }) // 3 ``` -## Functions - -Functions are expressions that take parameters. +## 関数 -You can define an anonymous function (i.e. no name) that returns a given integer plus one: +関数は引数を受け取る式です。 +ここでは与えられた数値に1を足す無名関数(すなわち名前が無い関数)を宣言しています。 ```tut (x: Int) => x + 1 ``` +`=>` の左側は引数のリストになります。右側は引数を含む式になります。 -On the left of `=>` is a list of parameters. On the right is an expression involving the parameters. - -You can also name functions. +関数には名前をつけることもできます。 {% scalafiddle %} ```tut @@ -122,7 +123,7 @@ println(addOne(1)) // 2 ``` {% endscalafiddle %} -Functions may take multiple parameters. +関数は複数の引数をとることもできます。 {% scalafiddle %} ```tut @@ -131,18 +132,18 @@ println(add(1, 2)) // 3 ``` {% endscalafiddle %} -Or it can take no parameters. +また引数を取らないこともありえます。 ```tut val getTheAnswer = () => 42 println(getTheAnswer()) // 42 ``` -## Methods +## メソッド -Methods look and behave very similar to functions, but there are a few key differences between them. +メソッドは関数と見た目、振る舞いがとても似ていますが、それらには違いがいくつかあります。 -Methods are defined with the `def` keyword. `def` is followed by a name, parameter lists, a return type, and a body. +メソッドは `def` キーワードで定義されます。 `def` の後ろには名前、引数リスト、戻り値の型、処理の内容が続きます。 {% scalafiddle %} ```tut @@ -151,9 +152,9 @@ println(add(1, 2)) // 3 ``` {% endscalafiddle %} -Notice how the return type is declared _after_ the parameter list and a colon `: Int`. +戻り値の型は引数リストとコロンの「後ろ」に宣言することに注意してください。`: Int` -Methods can take multiple parameter lists. +メソッドは複数のパラメーターリストを受け取ることができます。 {% scalafiddle %} ```tut @@ -162,27 +163,26 @@ println(addThenMultiply(1, 2)(3)) // 9 ``` {% endscalafiddle %} -Or no parameter lists at all. +また、パラメーターリストを一切受け取らないこともあります。 ```tut def name: String = System.getProperty("user.name") println("Hello, " + name + "!") ``` +メソッドと関数には他にも違いがありますが、今のところは同じようなものと考えて大丈夫です。 -There are some other differences, but for now, you can think of them as something similar to functions. - -Methods can have multi-line expressions as well. +メソッドは複数行の式も持つことができます。 ```tut def getSquareString(input: Double): String = { val square = input * input square.toString } ``` -The last expression in the body is the method's return value. (Scala does have a `return` keyword, but it's rarely used.) +メソッド本体にある最後の式はメソッドの戻り値になります。(Scalaには`return`キーワードはありますが、めったに使われません。) -## Classes +## クラス -You can define classes with the `class` keyword followed by its name and constructor parameters. +`class` キーワードとその後ろに名前、コンストラクタ引数を続けることで、クラスを定義することができます。 ```tut class Greeter(prefix: String, suffix: String) { @@ -190,26 +190,28 @@ class Greeter(prefix: String, suffix: String) { println(prefix + name + suffix) } ``` -The return type of the method `greet` is `Unit`, which says there's nothing meaningful to return. It's used similarly to `void` in Java and C. (A difference is that because every Scala expression must have some value, there is actually a singleton value of type Unit, written (). It carries no information.) +`greet` メソッドの戻り値の型は`Unit`です。`Unit`は意味のある戻り値が存在しないことを意味します。 +それはJavaやC言語の`void`と似たような使われ方をします。(`void`との違いは全てのScalaの式は値をを持たなければなりませんので、 +戻り値はUnit型のシングルトンな値になります。その値には情報はありません。) -You can make an instance of a class with the `new` keyword. +`new` キーワードを使うことで、クラスのインスタンスを生成することができます。 ```tut val greeter = new Greeter("Hello, ", "!") greeter.greet("Scala developer") // Hello, Scala developer! ``` -We will cover classes in depth [later](classes.html). +クラスについては[後で](classes.html)詳しく取り扱います。 -## Case Classes +## ケースクラス -Scala has a special type of class called a "case" class. By default, case classes are immutable and compared by value. You can define case classes with the `case class` keywords. +Scalaには"ケース"クラスという特別な型があります。デフォルトでケースクラスは不変であり、値で比較されます。 +`case class` キーワードを利用して、ケースクラスを定義できます。 ```tut case class Point(x: Int, y: Int) ``` - -You can instantiate case classes without `new` keyword. +`new` キーワードがなくても、ケースクラスのインスタンス化はできます。 ```tut val point = Point(1, 2) @@ -217,29 +219,30 @@ val anotherPoint = Point(1, 2) val yetAnotherPoint = Point(2, 2) ``` -And they are compared by value. +ケースクラスは値で比較されます。 ```tut if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { println(point + " and " + anotherPoint + " are different.") -} // Point(1,2) and Point(1,2) are the same. +} // Point(1,2) と Point(1,2) は同じになります。 if (point == yetAnotherPoint) { println(point + " and " + yetAnotherPoint + " are the same.") } else { println(point + " and " + yetAnotherPoint + " are different.") -} // Point(1,2) and Point(2,2) are different. +} // Point(1,2) と Point(2,2) は異なります。 ``` -There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with them! We will cover them in depth [later](case-classes.html). +ケースクラスについて紹介すべきことはたくさんあり、あなたはケースクラスが大好きになると確信しています。 +それらについては[後で](case-classes.html)詳しく取り扱います。 -## Objects +## オブジェクト -Objects are single instances of their own definitions. You can think of them as singletons of their own classes. +オブジェクトはそれ自身が定義する単一のインスタンスです。それら自身のクラスのシングルトンと考えることができます。 -You can define objects with the `object` keyword. +`object`キーワードを利用してオブジェクトを定義することができます。 ```tut object IdFactory { @@ -251,7 +254,7 @@ object IdFactory { } ``` -You can access an object by referring to its name. +名前を参照することでオブジェクトにアクセスすることができます。 ```tut val newId: Int = IdFactory.create() @@ -260,13 +263,13 @@ val newerId: Int = IdFactory.create() println(newerId) // 2 ``` -We will cover objects in depth [later](singleton-objects.html). +オブジェクトについては [後で](singleton-objects.html)詳しく取り扱います。 -## Traits +## トレイト -Traits are types containing certain fields and methods. Multiple traits can be combined. +トレイトはいくつかのフィールドとメソッドを含む型です。複数のトレイトは結合することができます。 -You can define traits with `trait` keyword. +`trait`キーワードでトレイトを定義することができます。 ```tut trait Greeter { @@ -274,7 +277,7 @@ trait Greeter { } ``` -Traits can also have default implementations. +トレイトはデフォルトの実装を持つことができます。 {% scalafiddle %} ```tut @@ -284,7 +287,7 @@ trait Greeter { } ``` -You can extend traits with the `extends` keyword and override an implementation with the `override` keyword. +`extends`キーワードでトレイトを継承することも、`override` キーワードで実装をオーバーライドすることもできます。 ```tut class DefaultGreeter extends Greeter @@ -303,17 +306,16 @@ customGreeter.greet("Scala developer") // How are you, Scala developer? ``` {% endscalafiddle %} -Here, `DefaultGreeter` extends only a single trait, but it could extend multiple traits. +ここでは、`DefaultGreeter`は一つのトレイトだけを継承していますが、複数のトレイトを継承することもできます。 -We will cover traits in depth [later](traits.html). +トレイトについては [後で](traits.html)詳しく取り扱います。 -## Main Method +## メインメソッド -The main method is an entry point of a program. The Java Virtual -Machine requires a main method to be named `main` and take one -argument, an array of strings. +メインメソッドはプログラムの始点になります。Javaバーチャルマシーンは`main`と名付けられたメインメソッドが必要で、 +それは文字列型を含む配列を一つ引数として受け取ります。 -Using an object, you can define a main method as follows: +オブジェクトを使い、以下のようにメインメソッドを定義することができます。 ```tut object Main { From b6e7bfe570afa862c6728e9739c8348a60ece21d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 29 Jan 2019 17:17:23 +0900 Subject: [PATCH 005/358] Update _ja/tour/basics.md Co-Authored-By: take2webservice --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 4a86970558..ae829bd951 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -205,7 +205,7 @@ greeter.greet("Scala developer") // Hello, Scala developer! ## ケースクラス -Scalaには"ケース"クラスという特別な型があります。デフォルトでケースクラスは不変であり、値で比較されます。 +Scalaには"ケース"クラスという特別な種類のクラスがあります。デフォルトでケースクラスは不変であり、値で比較されます。 `case class` キーワードを利用して、ケースクラスを定義できます。 ```tut From 1e0313558a811e8d4d88b90cd9a7e7cbe1d559d5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 29 Jan 2019 17:17:45 +0900 Subject: [PATCH 006/358] Update _ja/tour/basics.md Co-Authored-By: take2webservice --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index ae829bd951..5ff1826fd5 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -211,7 +211,7 @@ Scalaには"ケース"クラスという特別な種類のクラスがありま ```tut case class Point(x: Int, y: Int) ``` -`new` キーワードがなくても、ケースクラスのインスタンス化はできます。 +ケースクラスは、`new` キーワードなしでインスタンス化できます。 ```tut val point = Point(1, 2) From d0216b5f7b3d8b17eee0d0546933b08bf378b597 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 29 Jan 2019 17:17:57 +0900 Subject: [PATCH 007/358] Update _ja/tour/basics.md Co-Authored-By: take2webservice --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 5ff1826fd5..b73f4d0e7d 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -267,7 +267,7 @@ println(newerId) // 2 ## トレイト -トレイトはいくつかのフィールドとメソッドを含む型です。複数のトレイトは結合することができます。 +トレイトはいくつかのフィールドとメソッドを含む型です。複数のトレイトを結合することもできます。 `trait`キーワードでトレイトを定義することができます。 From ee89e627584360c174ceee0e6a1ced25d039818c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 29 Jan 2019 17:18:28 +0900 Subject: [PATCH 008/358] Update _ja/tour/basics.md Co-Authored-By: take2webservice --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index b73f4d0e7d..a2256f4a0c 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -240,7 +240,7 @@ if (point == yetAnotherPoint) { ## オブジェクト -オブジェクトはそれ自身が定義する単一のインスタンスです。それら自身のクラスのシングルトンと考えることができます。 +オブジェクトはそれ自体が定義である単一のインスタンスです。そのクラスのシングルトンと考えることもできます。 `object`キーワードを利用してオブジェクトを定義することができます。 From 6b8326452e0d8820eb5e8d6baa6e1e86abaa0a73 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 31 Jan 2019 23:14:49 +0900 Subject: [PATCH 009/358] translate unified-types into Japanese --- _ja/tour/unified-types.md | 65 ++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md index 2c4d5bb239..18846b4a3c 100644 --- a/_ja/tour/unified-types.md +++ b/_ja/tour/unified-types.md @@ -1,6 +1,7 @@ --- layout: tour -title: Unified Types +title: Unified型 +language: ja discourse: true @@ -13,36 +14,46 @@ prerequisite-knowledge: classes, basics redirect_from: "/tutorials/tour/unified-types.html" --- - -In Scala, all values have a type, including numerical values and functions. The diagram below illustrates a subset of the type hierarchy. +Scalaでは数値や関数を含め、全ての値は型を持ちます。 +以下の図は型階層の一部を説明しています。 Scala Type Hierarchy -## Scala Type Hierarchy ## +## Scalaの型階層 ## + +[`Any`](http://www.scala-lang.org/api/2.12.1/scala/Any.html) は全ての型のスーパータイプであり、トップ型とも呼ばれます。 +Anyは `equals`、` hashCode`、そして `toString`のようないくつかの普遍的なメソッドを定義しています。 +そして`AnyVal`と`AnyRef` という2つの直系のサブクラスを持ちます。 -[`Any`](http://www.scala-lang.org/api/2.12.1/scala/Any.html) is the supertype of all types, also called the top type. It defines certain universal methods such as `equals`, `hashCode`, and `toString`. `Any` has two direct subclasses: `AnyVal` and `AnyRef`. +`AnyVal` は値型に相当します。 +事前に定義された9つの値型が存在し、それら`Double`, `Float`, `Long`, `Int`, `Short`, `Byte`, `Char`, `Unit`,`Boolean`は +ヌラブル(nullable)ではありません。 -`AnyVal` represents value types. There are nine predefined value types and they are non-nullable: `Double`, `Float`, `Long`, `Int`, `Short`, `Byte`, `Char`, `Unit`, and `Boolean`. `Unit` is a value type which carries no meaningful information. There is exactly one instance of `Unit` which can be declared literally like so: `()`. All functions must return something so sometimes `Unit` is a useful return type. +`Unit`は意味のない情報を返す値型です。明確に`Unit`のインスタンスがあり、`()`のような形で宣言されることがあります。 +全ての関数は必ず何かを返さなければなりません。そのため`Unit`は戻り値の型として時々役立ちます。 -`AnyRef` represents reference types. All non-value types are defined as reference types. Every user-defined type in Scala is a subtype of `AnyRef`. If Scala is used in the context of a Java runtime environment, `AnyRef` corresponds to `java.lang.Object`. +`AnyRef` は参照型を意味します。全ての値型でない型は参照型として定義されます。Scalaでは全てのユーザー定義型は`AnyRef`のサブタイプになります。 +もしScalaがJava実行環境上で利用されるなら、`AnyRef` は `java.lang.Object` に相当します。 -Here is an example that demonstrates that strings, integers, characters, boolean values, and functions are all objects just like every other object: +ここにstring値、integer値、character値、boolean値と関数が他のオブジェクトと同様に全てオブジェクトであるという例があります。 ```tut val list: List[Any] = List( "a string", - 732, // an integer - 'c', // a character - true, // a boolean value - () => "an anonymous function returning a string" + 732, // integer + 'c', // character + true, // boolean value + () => "文字列を返す無名関数" ) list.foreach(element => println(element)) ``` -It defines a variable `list` of type `List[Any]`. The list is initialized with elements of various types, but they all are instance of `scala.Any`, so you can add them to the list. +これは`List[Any]`型の`list`という値を定義します。 +このlistは様々な型の要素で初期化されますが、それら全ては `scala.Any` のインスタンスです。 +そのため、ここではlistにそれらの値を追加することができます。 -Here is the output of the program: +こちらは先程のプログラムの出力です。 ``` a string @@ -52,31 +63,37 @@ true ``` -## Type Casting +## 型変換 +値型は以下の順序で変換することができます。 + Value types can be cast in the following way: -Scala Type Hierarchy +Scalaの型階層 -For example: +例えば、 ```tut val x: Long = 987654321 -val y: Float = x // 9.8765434E8 (note that some precision is lost in this case) +val y: Float = x // 9.8765434E8 (この場合精度が落ちることにが注意してください) val face: Char = '☺' val number: Int = face // 9786 ``` -Casting is unidirectional. This will not compile: +型変換は一方向になります。これはコンパイルができないでしょう。 ``` val x: Long = 987654321 val y: Float = x // 9.8765434E8 -val z: Long = y // Does not conform +val z: Long = y // 一致しない ``` -You can also cast a reference type to a subtype. This will be covered later in the tour. +参照型をサブタイプにキャストすることもできます。こちらはツアーの中で後ほど紹介します。 -## Nothing and Null -`Nothing` is a subtype of all types, also called the bottom type. There is no value that has type `Nothing`. A common use is to signal non-termination such as a thrown exception, program exit, or an infinite loop (i.e., it is the type of an expression which does not evaluate to a value, or a method that does not return normally). +## Nothing と Null +`Nothing`は全ての型のサブタイプであり、ボトム型とも呼ばれます。`Nothing`型を持つ値は存在しません。 +一般的にスローされた例外、プログラム終了、無限ループのような終了していない信号として使われます。 +(例えば、値を評価しない表現や正常に返されないメソッド) -`Null` is a subtype of all reference types (i.e. any subtype of AnyRef). It has a single value identified by the keyword literal `null`. `Null` is provided mostly for interoperability with other JVM languages and should almost never be used in Scala code. We'll cover alternatives to `null` later in the tour. +`Null` は全ての参照型(例えば、全てのAnyRefのサブタイプ)です。`null`というキーワード表記によって単一の値が識別されます。 +`Null` は多くの場合、他のJVM言語との相互運用性のため提供され、Scalaのコード内では決して使われるべきではありません。 +以降のツアーでは`null`の代替手段について説明します。 \ No newline at end of file From 6cef495543bf96ac214203579d525b122a8ef4e6 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 1 Feb 2019 00:42:38 +0900 Subject: [PATCH 010/358] Japanese translation of TOUR OF SCALA classes --- _ja/tour/classes.md | 73 ++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 1f4b80b59e..ff16630877 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -1,6 +1,7 @@ --- layout: tour -title: Classes +title: クラス +language: ja discourse: true @@ -15,18 +16,24 @@ prerequisite-knowledge: no-return-keyword, type-declaration-syntax, string-inter redirect_from: "/tutorials/tour/classes.html" --- -Classes in Scala are blueprints for creating objects. They can contain methods, -values, variables, types, objects, traits, and classes which are collectively called _members_. Types, objects, and traits will be covered later in the tour. +Scalaにおけるクラスはオブジェクトを作るための設計図となります。 +それらはメソッド、値、変数、型、オブジェクト、トレイト、クラスを持ち、まとめて _メンバー_ と呼ばれます。 +型、オブジェクト、トレイトはツアーで後ほど取り扱います。 + +## クラスを定義する + +最小のクラス定義は単純に`class`キーワードと識別子になります。 +クラス名はキャメルケースであるべきです。 -## Defining a class -A minimal class definition is simply the keyword `class` and -an identifier. Class names should be capitalized. ```tut class User val user1 = new User ``` -The keyword `new` is used to create an instance of the class. `User` has a default constructor which takes no arguments because no constructor was defined. However, you'll often want a constructor and class body. Here is an example class definition for a point: +`new`キーワードはクラスのインスタンスを作るために使われます。 +`User`は引数を受け取らないデフォルトコンストラクターを持ちます。なぜならコンストラクターは定義されていないからです。 +しかしながら、コンストラクターとクラス本体は頻繁に欲しくなるでしょう。 +こちらは位置情報のクラス定義の例になります。 ```tut class Point(var x: Int, var y: Int) { @@ -44,35 +51,41 @@ val point1 = new Point(2, 3) point1.x // 2 println(point1) // prints (2, 3) ``` +この`Point`クラスは4つのメンバーを持ちます。 +変数`x` と `y` そしてメソッド `move` と `toString`です。 +多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネイチャになります。 +(シグネイチャはメソッドの名、そのメソッドに対する引数の数と型で構成されています。) +`move`メソッドは2つのInteger引数を受け取り、意味を運ばないUnitの値である`()` を返します。 +これは乱暴に言えば、Javaのような言語における`void`と一致します。 +その一方で`toString`は引数を受け取りませんが、`String`の値を返します。 +なぜなら[`AnyRef`](unified-types.html)の`toString`を`toString`がオーバーライドするためで、それは`override`キーワードでタグ付けされます。 -This `Point` class has four members: the variables `x` and `y` and the methods `move` and -`toString`. Unlike many other languages, the primary constructor is in the class signature `(var x: Int, var y: Int)`. The `move` method takes two integer arguments and returns the Unit value `()`, which carries no information. This corresponds roughly with `void` in Java-like languages. `toString`, on the other hand, does not take any arguments but returns a `String` value. Since `toString` overrides `toString` from [`AnyRef`](unified-types.html), it is tagged with the `override` keyword. - -## Constructors +## コンストラクター -Constructors can have optional parameters by providing a default value like so: +コンストラクターはデフォルト値を提供することで、以下のようにオプションパラメーターを持つことができます。 ```tut class Point(var x: Int = 0, var y: Int = 0) -val origin = new Point // x and y are both set to 0 +val origin = new Point // x と y には共に0がセットされます。 val point1 = new Point(1) -println(point1.x) // prints 1 - +println(point1.x) // 1 が出力されます。 ``` +このバージョンの`Point`クラスでは、`x` と `y` はデフォルト値0を持ち、引数が必須ではありません。 +しかしながらコンストラクタは引数を左から右に読み込むため、もし`y`の値だけを渡したい場合は、パラメーターに名前をつけてやる必要があります。 -In this version of the `Point` class, `x` and `y` have the default value `0` so no arguments are required. However, because the constructor reads arguments left to right, if you just wanted to pass in a `y` value, you would need to name the parameter. ``` class Point(var x: Int = 0, var y: Int = 0) val point2 = new Point(y=2) -println(point2.y) // prints 2 +println(point2.y) // 2 が出力されます。 ``` -This is also a good practice to enhance clarity. +これは明快さを高める良い習慣にもなります。 + +## プライベートメンバーとゲッター/セッター構文 +メンバーはデフォルトではパブリックになります。 +クラスの外から隠したい場合は`private`アクセス修飾詞を使いましょう。 -## Private Members and Getter/Setter Syntax -Members are public by default. Use the `private` access modifier -to hide them from outside of the class. ```tut class Point { private var _x = 0 @@ -94,20 +107,26 @@ class Point { val point1 = new Point point1.x = 99 -point1.y = 101 // prints the warning +point1.y = 101 // 警告が出力されます。 ``` -In this version of the `Point` class, the data is stored in private variables `_x` and `_y`. There are methods `def x` and `def y` for accessing the private data. `def x_=` and `def y_=` are for validating and setting the value of `_x` and `_y`. Notice the special syntax for the setters: the method has `_=` appended to the identifier of the getter and the parameters come after. +このバージョンの`Point`クラスでは、データはプライベート変数 `_x` と `_y` に保存されます。 +メソッド`def x` と `def y` はプライベートなデータにアクセスするためのものです。 +`def x_=` と `def y_=` は `_x` と `_y` の値を検証し設定するためのものになります。 +セッターのための特別な構文に注意してください。 +セッターメソッドはゲッターメソッドの識別子に`_=`を追加し、その後ろにパラメーターを取ります。 + +プライマリコンストラクタの`val` と `var` を持つパラメーターはパブリックになります。 +しかしながら`val` は不変となるため、以下のように記述することはできません。 -Primary constructor parameters with `val` and `var` are public. However, because `val`s are immutable, you can't write the following. ``` class Point(val x: Int, val y: Int) val point = new Point(1, 2) -point.x = 3 // <-- does not compile +point.x = 3 // <-- コンパイルされません。 ``` -Parameters without `val` or `var` are private values, visible only within the class. +`val` や `var` が存在しないパラメーターはクラス内でだけで参照できるプライベートな値や変数となります。 ``` class Point(x: Int, y: Int) val point = new Point(1, 2) -point.x // <-- does not compile +point.x // <-- コンパイルされません。 ``` From 0aa7cfa606ab56677199eb19cc86ef3fe3926b37 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 12 Mar 2019 19:18:02 +0900 Subject: [PATCH 011/358] fix bad translates --- _ja/tour/unified-types.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md index 18846b4a3c..e6898e4581 100644 --- a/_ja/tour/unified-types.md +++ b/_ja/tour/unified-types.md @@ -27,15 +27,15 @@ Anyは `equals`、` hashCode`、そして `toString`のようないくつかの `AnyVal` は値型に相当します。 事前に定義された9つの値型が存在し、それら`Double`, `Float`, `Long`, `Int`, `Short`, `Byte`, `Char`, `Unit`,`Boolean`は -ヌラブル(nullable)ではありません。 +null非許容です。 -`Unit`は意味のない情報を返す値型です。明確に`Unit`のインスタンスがあり、`()`のような形で宣言されることがあります。 +`Unit`は意味のある情報をもたない値型です。`Unit`型のインスタンスはただ1つだけあり、`()`というリテラルで宣言することができます。 全ての関数は必ず何かを返さなければなりません。そのため`Unit`は戻り値の型として時々役立ちます。 `AnyRef` は参照型を意味します。全ての値型でない型は参照型として定義されます。Scalaでは全てのユーザー定義型は`AnyRef`のサブタイプになります。 もしScalaがJava実行環境上で利用されるなら、`AnyRef` は `java.lang.Object` に相当します。 -ここにstring値、integer値、character値、boolean値と関数が他のオブジェクトと同様に全てオブジェクトであるという例があります。 +以下にstring、integer、character、boolean、関数が他のオブジェクトと同様に全てオブジェクトであるという例を示します。 ```tut val list: List[Any] = List( @@ -50,8 +50,7 @@ list.foreach(element => println(element)) ``` これは`List[Any]`型の`list`という値を定義します。 -このlistは様々な型の要素で初期化されますが、それら全ては `scala.Any` のインスタンスです。 -そのため、ここではlistにそれらの値を追加することができます。 +このlistは様々な型の要素で初期化されています。しかしそれらは全て `scala.Any` のインスタンスなのでlistに追加することができています。 こちらは先程のプログラムの出力です。 @@ -63,8 +62,8 @@ true ``` -## 型変換 -値型は以下の順序で変換することができます。 +## 型キャスト +値型は以下の順序でキャストできます。 Value types can be cast in the following way: Scalaの型階層 @@ -91,9 +90,10 @@ val z: Long = y // 一致しない ## Nothing と Null `Nothing`は全ての型のサブタイプであり、ボトム型とも呼ばれます。`Nothing`型を持つ値は存在しません。 -一般的にスローされた例外、プログラム終了、無限ループのような終了していない信号として使われます。 -(例えば、値を評価しない表現や正常に返されないメソッド) +一般的に例外のスロー、プログラム終了、無限ループなど終了していないことを示すのに使われます。 +(すなわち、値として評価されない式や正常に返らないメソッドなどです。) -`Null` は全ての参照型(例えば、全てのAnyRefのサブタイプ)です。`null`というキーワード表記によって単一の値が識別されます。 -`Null` は多くの場合、他のJVM言語との相互運用性のため提供され、Scalaのコード内では決して使われるべきではありません。 -以降のツアーでは`null`の代替手段について説明します。 \ No newline at end of file + +`Null` は全ての参照型のサブタイプ(すなわち、全てのAnyRefのサブタイプ)です。`null`というキーワードリテラルが指す値を1つだけもちます。 +`Null` は、ほぼ他のJVM言語との相互運用性のためだけに提供されているので、Scalaのコード内ではほとんどの場合、使われるべきではありません。 +`null`の代替手段については、後のツアーで説明します。 From e294d563dbb8a2f42fa65d17a4f7f5544b2752ee Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 12 Mar 2019 20:22:40 +0900 Subject: [PATCH 012/358] Japanese translation of TOUR OF SCALA traits --- _ja/tour/traits.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index 121255457d..4cde118156 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -1,6 +1,7 @@ --- layout: tour -title: Traits +title: トレイト +language: ja discourse: true @@ -15,15 +16,18 @@ prerequisite-knowledge: expressions, classes, generics, objects, companion-objec redirect_from: "/tutorials/tour/traits.html" --- -Traits are used to share interfaces and fields between classes. They are similar to Java 8's interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters. +トレイトはクラス間でインターフェースとフィールドを共有するために使います。それらはJava8のインターフェースと似ています。 +クラスとオブジェクトはトレイトを継承することができますが、トレイトはインスタンス化ができません、したがってパラメータを持ちません。 -## Defining a trait +## トレイトを定義する +最小のトレイトはキーワード `trait` と識別子とだけのものです。 A minimal trait is simply the keyword `trait` and an identifier: ```tut trait HairColor ``` +トレイトはジェネリック型として抽象メソッドと共に特に役立ちます。 Traits become especially useful as generic types and with abstract methods. ```tut trait Iterator[A] { @@ -32,10 +36,10 @@ trait Iterator[A] { } ``` -Extending the `trait Iterator[A]` requires a type `A` and implementations of the methods `hasNext` and `next`. +`trait Iterator[A]` を継承することは `A` 型と、`hasNext` と `next` メソッドの実装を必要とします。 -## Using traits -Use the `extends` keyword to extend a trait. Then implement any abstract members of the trait using the `override` keyword: +## トレイトの使い方 +トレイトを継承するには `extends` キーワードを使います。その際に、 `override` キーワードを利用しすべての抽象メンバーを実装します。 ```tut trait Iterator[A] { def hasNext: Boolean @@ -59,10 +63,12 @@ val iterator = new IntIterator(10) iterator.next() // returns 0 iterator.next() // returns 1 ``` -This `IntIterator` class takes a parameter `to` as an upper bound. It `extends Iterator[Int]` which means that the `next` method must return an Int. +ここでの `IntIterator` クラスは上限として引数 `to` を取ります。 +`extends Iterator[Int]` は `next` メソッドは Int を返さなければならないことを意味します。 + +## サブタイピング +トレイトは必要があれば、代りにトレイトのサブタイプを使うことができます。 -## Subtyping -Where a given trait is required, a subtype of the trait can be used instead. ```tut import scala.collection.mutable.ArrayBuffer @@ -81,4 +87,5 @@ animals.append(dog) animals.append(cat) animals.foreach(pet => println(pet.name)) // Prints Harry Sally ``` -The `trait Pet` has an abstract field `name` which gets implemented by Cat and Dog in their constructors. On the last line, we call `pet.name` which must be implemented in any subtype of the trait `Pet`. +`trait Pet` はCat と Dogそれらのコンストラクタの中で実装された抽象フィールド `name`を持ちます。 +最終行では、`Pet` トレイトの全てのサブタイプの中で実装される必要がある `pet.name` を呼んでいます。 From 46f1a8c7aed872fedcd8e37acdc0929832eb8526 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:22:21 +0900 Subject: [PATCH 013/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index ff16630877..c46868775d 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -16,7 +16,7 @@ prerequisite-knowledge: no-return-keyword, type-declaration-syntax, string-inter redirect_from: "/tutorials/tour/classes.html" --- -Scalaにおけるクラスはオブジェクトを作るための設計図となります。 +Scalaにおけるクラスはオブジェクトを作るための設計図です。 それらはメソッド、値、変数、型、オブジェクト、トレイト、クラスを持ち、まとめて _メンバー_ と呼ばれます。 型、オブジェクト、トレイトはツアーで後ほど取り扱います。 From d11914af751fd1142b35b9844f30951b759dbf37 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:23:14 +0900 Subject: [PATCH 014/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index c46868775d..21f0c8955f 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -17,7 +17,7 @@ redirect_from: "/tutorials/tour/classes.html" --- Scalaにおけるクラスはオブジェクトを作るための設計図です。 -それらはメソッド、値、変数、型、オブジェクト、トレイト、クラスを持ち、まとめて _メンバー_ と呼ばれます。 +クラスはメソッド、値、変数、型、オブジェクト、トレイト、クラスを持ち、それらはまとめて _メンバー_ と呼ばれます。 型、オブジェクト、トレイトはツアーで後ほど取り扱います。 ## クラスを定義する From 86378a70b824675f8c389bfc2877ca36b6b2ea42 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:23:43 +0900 Subject: [PATCH 015/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 21f0c8955f..a552ee5876 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -22,7 +22,7 @@ Scalaにおけるクラスはオブジェクトを作るための設計図です ## クラスを定義する -最小のクラス定義は単純に`class`キーワードと識別子になります。 +最小のクラス定義は単純にキーワード`class`と識別子だけというものです。 クラス名はキャメルケースであるべきです。 ```tut From 6cd8e1b82cea1f184a7cbf14302df6802058c453 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:24:00 +0900 Subject: [PATCH 016/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index a552ee5876..11dfe222a1 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -23,7 +23,7 @@ Scalaにおけるクラスはオブジェクトを作るための設計図です ## クラスを定義する 最小のクラス定義は単純にキーワード`class`と識別子だけというものです。 -クラス名はキャメルケースであるべきです。 +クラス名は大文字から始まるべきです。 ```tut class User From bb024636a9d0386301f0153f0ca5231d0506d3b6 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:25:06 +0900 Subject: [PATCH 017/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 11dfe222a1..fdb9c1354b 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -31,7 +31,7 @@ class User val user1 = new User ``` `new`キーワードはクラスのインスタンスを作るために使われます。 -`User`は引数を受け取らないデフォルトコンストラクターを持ちます。なぜならコンストラクターは定義されていないからです。 +`User`はコンストラクターが定義されていないので、引数なしのデフォルトコンストラクターを持ちます。 しかしながら、コンストラクターとクラス本体は頻繁に欲しくなるでしょう。 こちらは位置情報のクラス定義の例になります。 From d3e602a3a90c242924642ed857d534c1c3cf19de Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:26:06 +0900 Subject: [PATCH 018/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index fdb9c1354b..451f661692 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -55,7 +55,7 @@ println(point1) // prints (2, 3) 変数`x` と `y` そしてメソッド `move` と `toString`です。 多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネイチャになります。 (シグネイチャはメソッドの名、そのメソッドに対する引数の数と型で構成されています。) -`move`メソッドは2つのInteger引数を受け取り、意味を運ばないUnitの値である`()` を返します。 +`move`メソッドは2つのInteger引数を受け取り、意味を持たないUnitの値である`()` を返します。 これは乱暴に言えば、Javaのような言語における`void`と一致します。 その一方で`toString`は引数を受け取りませんが、`String`の値を返します。 なぜなら[`AnyRef`](unified-types.html)の`toString`を`toString`がオーバーライドするためで、それは`override`キーワードでタグ付けされます。 From e7836051f53e6e1284b924ec4c4f7134410c4316 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:27:12 +0900 Subject: [PATCH 019/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 451f661692..b67d0e985c 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -62,7 +62,7 @@ println(point1) // prints (2, 3) ## コンストラクター -コンストラクターはデフォルト値を提供することで、以下のようにオプションパラメーターを持つことができます。 +コンストラクターは次のようにデフォルト値を与えると省略可能なパラメーターを持つことができます。 ```tut class Point(var x: Int = 0, var y: Int = 0) From 40646947200d96bbef2b3e6b8be6f790bd19dc5a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:27:34 +0900 Subject: [PATCH 020/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index b67d0e985c..bccb99f09c 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -72,7 +72,7 @@ val point1 = new Point(1) println(point1.x) // 1 が出力されます。 ``` このバージョンの`Point`クラスでは、`x` と `y` はデフォルト値0を持ち、引数が必須ではありません。 -しかしながらコンストラクタは引数を左から右に読み込むため、もし`y`の値だけを渡したい場合は、パラメーターに名前をつけてやる必要があります。 +しかしながらコンストラクタは引数を左から右に読み込むため、もし`y`の値だけを渡したい場合は、パラメーターに名前をつける必要があります。 ``` class Point(var x: Int = 0, var y: Int = 0) From 7962aea531de3786caf56ee4a947040ee4311d6a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:28:07 +0900 Subject: [PATCH 021/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index bccb99f09c..aac8947984 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -80,7 +80,7 @@ val point2 = new Point(y=2) println(point2.y) // 2 が出力されます。 ``` -これは明快さを高める良い習慣にもなります。 +これは明快さを高めるための良い習慣でもあります。 ## プライベートメンバーとゲッター/セッター構文 メンバーはデフォルトではパブリックになります。 From d6dd7ca053df6fc055f057779bffe38d82d9035d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:28:57 +0900 Subject: [PATCH 022/358] Update _ja/tour/classes.md Co-Authored-By: take2webservice --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index aac8947984..705c6e093e 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -110,7 +110,7 @@ point1.x = 99 point1.y = 101 // 警告が出力されます。 ``` このバージョンの`Point`クラスでは、データはプライベート変数 `_x` と `_y` に保存されます。 -メソッド`def x` と `def y` はプライベートなデータにアクセスするためのものです。 +プライベートなデータにアクセスするためのメソッド`def x` と `def y` があります。 `def x_=` と `def y_=` は `_x` と `_y` の値を検証し設定するためのものになります。 セッターのための特別な構文に注意してください。 セッターメソッドはゲッターメソッドの識別子に`_=`を追加し、その後ろにパラメーターを取ります。 From c8f2044676c927ae7b2e5b2d18ffdab64341d9f0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:39:25 +0900 Subject: [PATCH 023/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index 4cde118156..33bfeda42d 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -20,7 +20,7 @@ redirect_from: "/tutorials/tour/traits.html" クラスとオブジェクトはトレイトを継承することができますが、トレイトはインスタンス化ができません、したがってパラメータを持ちません。 ## トレイトを定義する -最小のトレイトはキーワード `trait` と識別子とだけのものです。 +最小のトレイトはキーワード `trait` と識別子だけというものです。 A minimal trait is simply the keyword `trait` and an identifier: ```tut From 3b18e4ec91fd8c24c3e65aff4c97c3f2d97fe52d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:40:29 +0900 Subject: [PATCH 024/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index 33bfeda42d..24bddb5ac0 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -27,7 +27,7 @@ A minimal trait is simply the keyword `trait` and an identifier: trait HairColor ``` -トレイトはジェネリック型として抽象メソッドと共に特に役立ちます。 +トレイトはジェネリック型として、抽象メソッドとあわせて使うと特に便利です。 Traits become especially useful as generic types and with abstract methods. ```tut trait Iterator[A] { From d07a39e816eed5fa84ec8684e956568a907f90b0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:40:39 +0900 Subject: [PATCH 025/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index 24bddb5ac0..cfc76f766a 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -21,7 +21,6 @@ redirect_from: "/tutorials/tour/traits.html" ## トレイトを定義する 最小のトレイトはキーワード `trait` と識別子だけというものです。 -A minimal trait is simply the keyword `trait` and an identifier: ```tut trait HairColor From 27a287ee57e23fc1edc2b1214f36cb82bc0ae7f6 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:41:12 +0900 Subject: [PATCH 026/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index cfc76f766a..f612071388 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -27,7 +27,6 @@ trait HairColor ``` トレイトはジェネリック型として、抽象メソッドとあわせて使うと特に便利です。 -Traits become especially useful as generic types and with abstract methods. ```tut trait Iterator[A] { def hasNext: Boolean From da96d5f4a021a870affb8214ad35bf9412315a23 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:43:11 +0900 Subject: [PATCH 027/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index f612071388..e65c4da46d 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -65,7 +65,7 @@ iterator.next() // returns 1 `extends Iterator[Int]` は `next` メソッドは Int を返さなければならないことを意味します。 ## サブタイピング -トレイトは必要があれば、代りにトレイトのサブタイプを使うことができます。 +あるトレイトが必要とされている場所に、代りにそのトレイトのサブタイプを使うことができます。 ```tut import scala.collection.mutable.ArrayBuffer From 6f41124c36a4e821a38e95a0f4e1a4c7b9617aec Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 13 Mar 2019 00:43:47 +0900 Subject: [PATCH 028/358] Update _ja/tour/traits.md Co-Authored-By: take2webservice --- _ja/tour/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index e65c4da46d..190e81db66 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -85,5 +85,5 @@ animals.append(dog) animals.append(cat) animals.foreach(pet => println(pet.name)) // Prints Harry Sally ``` -`trait Pet` はCat と Dogそれらのコンストラクタの中で実装された抽象フィールド `name`を持ちます。 +`trait Pet` が持つ抽象フィールド `name`は、Cat と Dog のコンストラクタで実装されました。 最終行では、`Pet` トレイトの全てのサブタイプの中で実装される必要がある `pet.name` を呼んでいます。 From 9fe7445aad9c942025e6cad8069e2c62872f9fe9 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 13 Mar 2019 19:19:50 +0900 Subject: [PATCH 029/358] Japanese translation of TOUR OF SCALA tuples --- _ja/tour/tuples.md | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index e945311a0e..a01373b009 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -1,6 +1,7 @@ --- layout: tour -title: Tuples +title: タプル +language: ja discourse: true @@ -14,27 +15,28 @@ topics: tuples redirect_from: "/tutorials/tour/tuples.html" --- -In Scala, a tuple is a class that can hold elements of different types. -Tuples are immutable. +Scalaではタプルは異なる型の要素を持つことができるクラスです。 +タプルは不変です。 -Tuples come in handy when we have to return multiple values from a function. +タプルは関数から複数の値を返す際に役立ちます。 -A tuple can be created as: +タプルは以下のように作ることができます ```tut val ingredient = ("Sugar" , 25):Tuple2[String, Int] ``` -This creates a tuple containing a String element and an Int element. +ここではString要素とInt要素を含むタプルを作っています。 -Tuple in Scala is a series of classes: Tuple2, Tuple3, etc., through Tuple22. -So when we create a tuple with n elements(n lying between 2 and 22), Scala basically instantiates -one of the corresponding classes from the group, parameterized with types of constituent elements. -For eg., ingredient is of type Tuple2[String, Int]. +Scalaではタプルは Tuple2, Tuple3, とTuple22までのクラスの連続です。 +そのためn個の要素(nは2から22)でタプルを作成する時、 -## Accessing the elements +Scalaは基本的に構成要素の型でパラメーター化されたグループに対応するクラスを1つインスタンス化します。 +例えば、 値 ingredient は Tuple2 [String, Int]型です。 -Tuple elements are accessed using underscore syntax. -'tuple._n' gives nth element(given there are that many elements). +## 要素へのアクセス + +タプル要素にはアンダースコア記法を用いてアクセスします。 +(要素がたくさんあることを考えると)'tuple._n' はn番目の要素を与えます。 ```tut println(ingredient._1) // Sugar @@ -42,9 +44,9 @@ println(ingredient._1) // Sugar println(ingredient._2) // 25 ``` -## Destructuring tuple data +## タプルのデータの構造化 -Scala tuple also supports destructuring. +Scalaのタプルは構造化にも対応しています。 ```tut val (name, quantity) = ingredient @@ -54,7 +56,7 @@ println(name) // Sugar println(quantity) // 25 ``` -Tuple destructuring can be used in pattern matching too. +タプルの構造化はパターンマッチングでも使われます。 ```tut val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3)) @@ -78,7 +80,7 @@ planetDistanceFromSun.foreach{ tuple => { } ``` -Or, in 'for' comprehension. +また、for内包表記においては、 ```tut val numPairs = List((2, 5), (3, -7), (20, 56)) @@ -90,6 +92,8 @@ for ((a, b) <- numPairs) { } ``` -The value () of type Unit is conceptually the same as the value () of type Tuple0. There can only be one value of this type since it has no elements. +Unit型の値 () は概念的には Tuple0 型の () の値と同じです。 +要素が無いため、この型の値は1つだけになりえます。 -Users may sometimes find hard to choose between Tuples and case classes. As a rule, case classes are preferred choice if elements carry more meaning. +ユーザーは時々タプルとケースクラスを選ぶのが難しいと感じる時があるかもしれません。 +ルールの通理、ケースクラスは要素がより多くの意味を持つような時に好まれます。 From f9e4926e91735ed646c625e798396697c381ad0d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:05:42 +0900 Subject: [PATCH 030/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index a01373b009..b2c249d0f6 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -15,7 +15,7 @@ topics: tuples redirect_from: "/tutorials/tour/tuples.html" --- -Scalaではタプルは異なる型の要素を持つことができるクラスです。 +Scalaではタプルは異なる型の要素を複数持つことができるクラスです。 タプルは不変です。 タプルは関数から複数の値を返す際に役立ちます。 From f6c0fa7903c9c85edae4d734aff50ec808731fc2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:06:30 +0900 Subject: [PATCH 031/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index b2c249d0f6..b0a511f523 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -25,7 +25,7 @@ Scalaではタプルは異なる型の要素を複数持つことができるク ```tut val ingredient = ("Sugar" , 25):Tuple2[String, Int] ``` -ここではString要素とInt要素を含むタプルを作っています。 +ここではString要素を1つとInt要素を1つ含むタプルを作っています。 Scalaではタプルは Tuple2, Tuple3, とTuple22までのクラスの連続です。 そのためn個の要素(nは2から22)でタプルを作成する時、 From 67c5bdf53ed208835a811cced0f8327847a4b5df Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:07:14 +0900 Subject: [PATCH 032/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index b0a511f523..16ca8e65af 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -27,7 +27,7 @@ val ingredient = ("Sugar" , 25):Tuple2[String, Int] ``` ここではString要素を1つとInt要素を1つ含むタプルを作っています。 -Scalaではタプルは Tuple2, Tuple3, とTuple22までのクラスの連続です。 +Scalaではタプルは Tuple2, Tuple3, … Tuple22 までの一連のクラス群です。 そのためn個の要素(nは2から22)でタプルを作成する時、 Scalaは基本的に構成要素の型でパラメーター化されたグループに対応するクラスを1つインスタンス化します。 From 96e4a789bafb24d623cb98fe685c20fae9419dbb Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:09:34 +0900 Subject: [PATCH 033/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 16ca8e65af..d65e6d8ebb 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -44,7 +44,7 @@ println(ingredient._1) // Sugar println(ingredient._2) // 25 ``` -## タプルのデータの構造化 +## タプルのデータの分割代入 Scalaのタプルは構造化にも対応しています。 From 12cfc0202feec6fe0d50000a845700a50bca7642 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:09:58 +0900 Subject: [PATCH 034/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index d65e6d8ebb..a12a9de6ad 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -46,7 +46,7 @@ println(ingredient._2) // 25 ## タプルのデータの分割代入 -Scalaのタプルは構造化にも対応しています。 +Scalaのタプルは分割代入にも対応しています。 ```tut val (name, quantity) = ingredient From 030f4558691ee9e230e7fd6952c0c4a5e9e616a5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:10:07 +0900 Subject: [PATCH 035/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index a12a9de6ad..a0dd4e8897 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -56,7 +56,7 @@ println(name) // Sugar println(quantity) // 25 ``` -タプルの構造化はパターンマッチングでも使われます。 +タプルの分割代入はパターンマッチングでも使われます。 ```tut val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3)) From 4ff7a2f24f222ff93fdefcc16da7af1576e164d5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:11:17 +0900 Subject: [PATCH 036/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index a0dd4e8897..7f963cc765 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -96,4 +96,4 @@ Unit型の値 () は概念的には Tuple0 型の () の値と同じです。 要素が無いため、この型の値は1つだけになりえます。 ユーザーは時々タプルとケースクラスを選ぶのが難しいと感じる時があるかもしれません。 -ルールの通理、ケースクラスは要素がより多くの意味を持つような時に好まれます。 +原則として、要素に意味をもたせる場合にはケースクラスが好まれます。 From 1b6d6309dd916eb9b7e08b58d5525e6694d9583f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:12:23 +0900 Subject: [PATCH 037/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 7f963cc765..571b08ad5c 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -30,7 +30,7 @@ val ingredient = ("Sugar" , 25):Tuple2[String, Int] Scalaではタプルは Tuple2, Tuple3, … Tuple22 までの一連のクラス群です。 そのためn個の要素(nは2から22)でタプルを作成する時、 -Scalaは基本的に構成要素の型でパラメーター化されたグループに対応するクラスを1つインスタンス化します。 +Scalaは基本的にそれらのクラスの中から構成要素の型に対応するクラスを1つインスタンス化します。 例えば、 値 ingredient は Tuple2 [String, Int]型です。 ## 要素へのアクセス From 4866f591d8df0eb001342b88ec74778d0ae7c13e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 14 Mar 2019 00:14:02 +0900 Subject: [PATCH 038/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 571b08ad5c..7bed992a60 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -93,7 +93,7 @@ for ((a, b) <- numPairs) { ``` Unit型の値 () は概念的には Tuple0 型の () の値と同じです。 -要素が無いため、この型の値は1つだけになりえます。 +要素が無いため、この型の値は1つだけしかありません。 ユーザーは時々タプルとケースクラスを選ぶのが難しいと感じる時があるかもしれません。 原則として、要素に意味をもたせる場合にはケースクラスが好まれます。 From ef8f752d3a29179bfe4ed9c7ad4c47115662c4c5 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 14 Mar 2019 18:10:39 +0900 Subject: [PATCH 039/358] Japanese translation of TOUR OF SCALA mixin-class-composition --- _ja/tour/mixin-class-composition.md | 31 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 35a87dc664..14167d6cf7 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -1,6 +1,7 @@ --- layout: tour -title: Class Composition with Mixins +title: ミックスインを用いたクラス合成 +language: ja discourse: true @@ -13,7 +14,7 @@ prerequisite-knowledge: inheritance, traits, abstract-classes, unified-types redirect_from: "/tutorials/tour/mixin-class-composition.html" --- -Mixins are traits which are used to compose a class. +ミックスインは複数のトレイトを利用し1つのクラスを構成することです。 ```tut abstract class A { @@ -31,9 +32,11 @@ val d = new D println(d.message) // I'm an instance of class B println(d.loudMessage) // I'M AN INSTANCE OF CLASS B ``` -Class `D` has a superclass `B` and a mixin `C`. Classes can only have one superclass but many mixins (using the keywords `extends` and `with` respectively). The mixins and the superclass may have the same supertype. +クラス`D`は スーパークラスを`B` とし、 ミックスイン`C`を持ちます。 +クラスは1つだけしかスーパークラスを持つことができませんが、ミックスインは(`extends`とそれぞれに応じた`with`キーワードを利用し)複数持つことができます。 +ミックスインとスーパークラスは同じスーパータイプを持つことができます。 -Now let's look at a more interesting example starting with an abstract class: +それでは抽象クラスから始まる興味深い例を見てみましょう。 ```tut abstract class AbsIterator { @@ -42,9 +45,8 @@ abstract class AbsIterator { def next(): T } ``` -The class has an abstract type `T` and the standard iterator methods. - -Next, we'll implement a concrete class (all abstract members `T`, `hasNext`, and `next` have implementations): +クラスは抽象型`T`と標準的なイテレーターのメソッドを持ちます。 +次に、(全ての抽象メンバー`T`, `hasNext`, `next`が実装を持つ)具象クラスを実装します。 ```tut class StringIterator(s: String) extends AbsIterator { @@ -58,19 +60,20 @@ class StringIterator(s: String) extends AbsIterator { } } ``` -`StringIterator` takes a `String` and can be used to iterate over the String (e.g. to see if a String contains a certain character). +`StringIterator`は`String`を受け取り、Stringを反復処理するために使うことができます。 +(例:Stringに特定の文字列が含まれているかを確認するために) -Now let's create a trait which also extends `AbsIterator`. +それでは`AbsIterator`を継承したトレイトも作ってみましょう。 ```tut trait RichIterator extends AbsIterator { def foreach(f: T => Unit): Unit = while (hasNext) f(next()) } ``` -This trait implements `foreach` by continually calling the provided function `f: T => Unit` on the next element (`next()`) as long as there are further elements (`while (hasNext)`). Because `RichIterator` is a trait, it doesn't need to implement the abstract members of AbsIterator. - -We would like to combine the functionality of `StringIterator` and `RichIterator` into a single class. +このトレイトは(`while (hasNext)`で)要素がある限り、与えられた関数 `f: T => Unit`を次の要素(`next()`)に対し連続して呼び出す`foreach`メソッドのみを実装します。 +なぜなら`RichIterator`はトレイトであり、`RichIterator`はAbsIteratorの抽象メンバーを実装する必要がないからです。 +`StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせたい場合、 ```tut object StringIteratorTest extends App { class RichStringIter extends StringIterator("Scala") with RichIterator @@ -78,6 +81,6 @@ object StringIteratorTest extends App { richStringIter foreach println } ``` -The new class `RichStringIter` has `StringIterator` as a superclass and `RichIterator` as a mixin. +新しいクラス`RichStringIter`は`StringIterator`をスーパークラスとし、`RichIterator`をミックスインします。 -With single inheritance we would not be able to achieve this level of flexibility. +単一継承ではこのレベルの柔軟性を達成することはできないでしょう。 From 41ed45bf2164ac431fc99f79f709c47aacce4273 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:57:57 +0900 Subject: [PATCH 040/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 14167d6cf7..fa84653ac1 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -14,7 +14,7 @@ prerequisite-knowledge: inheritance, traits, abstract-classes, unified-types redirect_from: "/tutorials/tour/mixin-class-composition.html" --- -ミックスインは複数のトレイトを利用し1つのクラスを構成することです。 +ミックスインはクラスを構成するのに使われるトレイトです。 ```tut abstract class A { From acc431d3285db1c938c55a87854ab07deb3ff9c9 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:58:14 +0900 Subject: [PATCH 041/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index fa84653ac1..22acf47ce1 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -60,7 +60,7 @@ class StringIterator(s: String) extends AbsIterator { } } ``` -`StringIterator`は`String`を受け取り、Stringを反復処理するために使うことができます。 +`StringIterator`は`String`を受け取り、そのStringを反復処理するために使われます。 (例:Stringに特定の文字列が含まれているかを確認するために) それでは`AbsIterator`を継承したトレイトも作ってみましょう。 From 7d1da8e9cdf9eee01fab86de686bc8920283cb67 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:58:24 +0900 Subject: [PATCH 042/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 22acf47ce1..4736b6bea5 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -61,7 +61,7 @@ class StringIterator(s: String) extends AbsIterator { } ``` `StringIterator`は`String`を受け取り、そのStringを反復処理するために使われます。 -(例:Stringに特定の文字列が含まれているかを確認するために) +(例:Stringに特定の文字が含まれているかを確認するために) それでは`AbsIterator`を継承したトレイトも作ってみましょう。 From 5084f3e586779854fd72445aa9dc848bb9d26b32 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:58:32 +0900 Subject: [PATCH 043/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 4736b6bea5..5d07d94a37 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -70,7 +70,7 @@ trait RichIterator extends AbsIterator { def foreach(f: T => Unit): Unit = while (hasNext) f(next()) } ``` -このトレイトは(`while (hasNext)`で)要素がある限り、与えられた関数 `f: T => Unit`を次の要素(`next()`)に対し連続して呼び出す`foreach`メソッドのみを実装します。 +このトレイトは(`while (hasNext)`で)要素がある限り、与えられた関数 `f: T => Unit`を次の要素(`next()`)に対し連続して呼び出す`foreach`メソッドを実装しています。 なぜなら`RichIterator`はトレイトであり、`RichIterator`はAbsIteratorの抽象メンバーを実装する必要がないからです。 `StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせたい場合、 From 5aa33843a41a1d8ce6feaa76ae189b91160033b4 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:59:35 +0900 Subject: [PATCH 044/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 5d07d94a37..71541d4341 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -71,7 +71,7 @@ trait RichIterator extends AbsIterator { } ``` このトレイトは(`while (hasNext)`で)要素がある限り、与えられた関数 `f: T => Unit`を次の要素(`next()`)に対し連続して呼び出す`foreach`メソッドを実装しています。 -なぜなら`RichIterator`はトレイトであり、`RichIterator`はAbsIteratorの抽象メンバーを実装する必要がないからです。 +`RichIterator`はトレイトなので、`RichIterator`はAbsIteratorの抽象メンバーを実装する必要がありません。 `StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせたい場合、 ```tut From b29dd9f4538303edfdd9c1e97608850dc92dedcc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 09:59:48 +0900 Subject: [PATCH 045/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 71541d4341..6734ae4cab 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -81,6 +81,6 @@ object StringIteratorTest extends App { richStringIter foreach println } ``` -新しいクラス`RichStringIter`は`StringIterator`をスーパークラスとし、`RichIterator`をミックスインします。 +新しいクラス`RichStringIter`は`StringIterator`をスーパークラスとし、`RichIterator`をミックスインとしています。 単一継承ではこのレベルの柔軟性を達成することはできないでしょう。 From c097f93c4b5a47cc8f40742d719186a74fcffb31 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Fri, 15 Mar 2019 10:00:02 +0900 Subject: [PATCH 046/358] Update _ja/tour/mixin-class-composition.md Co-Authored-By: take2webservice --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 6734ae4cab..0d84947ea2 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -73,7 +73,7 @@ trait RichIterator extends AbsIterator { このトレイトは(`while (hasNext)`で)要素がある限り、与えられた関数 `f: T => Unit`を次の要素(`next()`)に対し連続して呼び出す`foreach`メソッドを実装しています。 `RichIterator`はトレイトなので、`RichIterator`はAbsIteratorの抽象メンバーを実装する必要がありません。 -`StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせたい場合、 +`StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせてみましょう。 ```tut object StringIteratorTest extends App { class RichStringIter extends StringIterator("Scala") with RichIterator From 01ec74db389989e98c7960524c7b5f42ad8b5ab3 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 15 Mar 2019 17:32:58 +0900 Subject: [PATCH 047/358] Japanese translation of TOUR OF SCALA higher-order-functions --- _ja/tour/higher-order-functions.md | 73 ++++++++++++++---------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index efc8d37db2..7d9beebbce 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -1,6 +1,7 @@ --- layout: tour -title: Higher-order Functions +title: 高階関数 +language: ja discourse: true @@ -13,57 +14,53 @@ previous-page: mixin-class-composition redirect_from: "/tutorials/tour/higher-order-functions.html" --- -Higher order functions take other functions as parameters or return a function as -a result. This is possible because functions are first-class values in Scala. -The terminology can get a bit confusing at this point, and we use the phrase -"higher order function" for both methods and functions that take functions as parameters -or that return a function. +高階関数は他の関数をパラメーターとして受け取る、もしくは結果として関数を返します。 +Scalaでは関数は第一級の値であるため、高階関数が可能となります。 +この時点でその用語は少し混乱を招くことがあります。 +ここでは"高階関数"というフレーズを関数をパラメーターとして受け取る、または関数を返すメソッドと関数の両方に対して使います。 -One of the most common examples is the higher-order -function `map` which is available for collections in Scala. +もっとも一般的な例の1つは、Scalaのコレクションで利用可能な高階関数`map`です。 ```tut val salaries = Seq(20000, 70000, 40000) val doubleSalary = (x: Int) => x * 2 val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000) ``` -`doubleSalary` is a function which takes a single Int, `x`, and returns `x * 2`. In general, the tuple on the left of the arrow `=>` is a parameter list and the value of the expression on the right is what gets returned. On line 3, the function `doubleSalary` gets applied to each element in the -list of salaries. +`doubleSalary`はInt`x`を1つだけ受け取り、`x * 2`を返す関数です。 +一般的に、アロー`=>`の左側のタプルは引数リストであり、右側の値が返されます。 +3行目は給与のリストになります。 -To shrink the code, we could make the function anonymous and pass it directly as -an argument to map: +コードを減らすため、以下のように無名関数を作ることができ、引数として直接mapに渡すことができます ``` val salaries = Seq(20000, 70000, 40000) val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000) ``` -Notice how `x` is not declared as an Int in the above example. That's because the -compiler can infer the type based on the type of function map expects. An even more idiomatic way to write the same piece of code would be: +上記例では`x`をIntとして宣言していないことに注意してください。 +それはmapが期待する関数の型を基にコンパイラーが型を推論できるからです。 +さらに言えば、慣用的には同じコードを以下のように書きます。 ```tut val salaries = Seq(20000, 70000, 40000) val newSalaries = salaries.map(_ * 2) ``` -Since the Scala compiler already knows the type of the parameters (a single Int), - you just need to provide the right side of the function. The only -caveat is that you need to use `_` in place of a parameter name (it was `x` in -the previous example). - -## Coercing methods into functions -It is also possible to pass methods as arguments to higher-order functions because -the Scala compiler will coerce the method into a function. +Scalaコンパイラはパラメーターの型を(Intが1つだけと)既に知っているため、関数の右側だけを提供する必要があります。 +唯一の注意点はパラメータ名の代わりに`_`を使う必要があるということです(先の例では`x`でした)。 + +## メソッドを関数に強制変換 +高階関数には引数としてとしてメソッドを渡すことも可能で、それはScalaコンパイラがメソッドを関数に強制変換するからです。 ``` case class WeeklyWeatherForecast(temperatures: Seq[Double]) { private def convertCtoF(temp: Double) = temp * 1.8 + 32 - def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- passing the method convertCtoF + def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- convertCtoFメソッドが渡されます } ``` -Here the method `convertCtoF` is passed to `forecastInFahrenheit`. This is possible because the compiler coerces `convertCtoF` to the function `x => convertCtoF(x)` (note: `x` will - be a generated name which is guaranteed to be unique within its scope). +ここではメソッド`convertCtoF`は`forecastInFahrenheit`に渡されます。 +これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に強制変換することで実現します。 -## Functions that accept functions -One reason to use higher-order functions is to reduce redundant code. Let's say you wanted some methods that could raise someone's salaries by various factors. Without creating a higher-order function, -it might look something like this: +## 関数を受け取る関数 +高階関数を使う理由の1つは冗長なコードを削減することです。 +いくつかの倍増率から人の給料を上げることができるメソッドが欲しいとすれば、こんな感じになるかもしれません。 ```tut object SalaryRaiser { @@ -79,8 +76,8 @@ object SalaryRaiser { } ``` -Notice how each of the three methods vary only by the multiplication factor. To simplify, -you can extract the repeated code into a higher-order function like so: +3つのメソッドはそれぞれ掛け算の倍増率のみ異なることに気をつけてください。 +簡潔にするため、以下のように繰り返されているコードを高階関数に抽出することができます。 ```tut object SalaryRaiser { @@ -98,14 +95,12 @@ object SalaryRaiser { promotion(salaries, salary => salary * salary) } ``` +新しいメソッド`promotion`はsalariesと`Double => Double`型の関数(すなわち、Doubleを受け取り、Doubleを返す関数)を受け取り、積を返します。 -The new method, `promotion`, takes the salaries plus a function of type `Double => Double` -(i.e. a function that takes a Double and returns a Double) and returns the product. - -## Functions that return functions +## 関数を返す関数 -There are certain cases where you want to generate a function. Here's an example -of a method that returns a function. +関数を生成したい場合がいくつかあります。 +こちらは関数を返すメソッドの例になります。 ```tut def urlBuilder(ssl: Boolean, domainName: String): (String, String) => String = { @@ -120,6 +115,6 @@ val query = "id=1" val url = getURL(endpoint, query) // "https://www.example.com/users?id=1": String ``` -Notice the return type of urlBuilder `(String, String) => String`. This means that -the returned anonymous function takes two Strings and returns a String. In this case, -the returned anonymous function is `(endpoint: String, query: String) => s"https://www.example.com/$endpoint?$query"`. +urlBuilderの戻り値型`(String, String) => String`に注意してください。 +これは返される無名関数はStringを2つ受け取り、Stringを1つ返すことを意味します。 +このケースでは返される無名関数は`(endpoint: String, query: String) => s"https://www.example.com/$endpoint?$query"`です。 From a0f10f5b2efff6baa116b5574f21e5a142c14115 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 15 Mar 2019 17:52:36 +0900 Subject: [PATCH 048/358] Japanese translation of TOUR OF SCALA nested-functions --- _ja/tour/nested-functions.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/_ja/tour/nested-functions.md b/_ja/tour/nested-functions.md index 09e95c742a..582c8f1e25 100644 --- a/_ja/tour/nested-functions.md +++ b/_ja/tour/nested-functions.md @@ -1,6 +1,7 @@ --- layout: tour -title: Nested Methods +title: ネストしたメソッド +language: ja discourse: true @@ -13,7 +14,8 @@ previous-page: higher-order-functions redirect_from: "/tutorials/tour/nested-functions.html" --- -In Scala it is possible to nest method definitions. The following object provides a `factorial` method for computing the factorial of a given number: +Scalaではネストメソッドの定義ができます。 +以下のオブジェクトは与えられた数値の階乗を演算するための`factorial`メソッドを提供します。 {% scalafiddle %} ```tut @@ -30,7 +32,7 @@ In Scala it is possible to nest method definitions. The following object provide ``` {% endscalafiddle %} -The output of this program is: +このプログラムの出力は以下の通りです。 ``` Factorial of 2: 2 From ddc29c8ef49f4f95db3f11c4d64838d90fe4b89c Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sat, 16 Mar 2019 15:44:41 +0900 Subject: [PATCH 049/358] Japanese translation of TOUR OF SCALA multiple-parameter-lists --- _ja/tour/multiple-parameter-lists.md | 53 ++++++++++++++++------------ 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index e8bdb9d86b..ec1fd6dd5a 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -1,6 +1,7 @@ --- layout: tour -title: Multiple Parameter Lists (Currying) +title: 複数パラメータリスト(カリー化) +language: ja discourse: true @@ -13,39 +14,44 @@ previous-page: nested-functions redirect_from: "/tutorials/tour/multiple-parameter-lists.html" --- -Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. This is formally known as [partial application](https://en.wikipedia.org/wiki/Partial_application). +メソッドは複数パラメータリストを定義できます。 +メソッドが少ない数のパラメータリストで呼び出された時、不足しているパラメータリストを引数として受け取る関数が生成されます。 +これは一般的に[部分適用](https://en.wikipedia.org/wiki/Partial_application)として知られています。 -Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections: +こちらはScalaのコレクションのドキュメントの[走査可能トレイト](/overviews/collections/trait-traversable.html) で定義された例になります。 ``` def foldLeft[B](z: B)(op: (B, A) => B): B ``` +`foldLeft`は初期値`z`とこの走査可能な全ての要素に対し左から右に二項演算子`op`を適用していきます。 +以下はその使い方の例です。 -`foldLeft` applies a binary operator `op` to an initial value `z` and all elements of this traversable, going left to right. Shown below is an example of its usage. - -Starting with an initial value of 0, `foldLeft` here applies the function `(m, n) => m + n` to each element in the List and the previous accumulated value. +初期値0から始まり、`foldLeft`はここではリスト内の各要素とその一つ前の累積値に関数`(m, n) => m + n`を適用します。 ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val res = numbers.foldLeft(0)((m, n) => m + n) print(res) // 55 ``` +複数パラメータリストにはより冗長な読み出し構文があり、それゆえ控えめに使う必要があります。 +推奨されるユースケースは次の通りです。 -Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include: - -#### Single functional parameter - In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. Without multiple parameter lists, the code would look like this: +#### 単一機能パラメータ + 単一機能パラメータの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 + 複数パラメータリストがない場合、このコードは以下のようになります。 ``` numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) ``` - - Note that the use of multiple parameter lists here also allows us to take advantage of Scala type inference to make the code more concise as shown below; which would not be possible in a non-curried definition. + + 複数パラメータリストを使うことで、以下のようにコードをより簡潔にすることができ、Scalaの型インターフェースの利点を享受できることに注意してください。 + それはカリー化されていない定義ではできません。 ``` numbers.foldLeft(0)(_ + _) ``` - Above statement `numbers.foldLeft(0)(_ + _)` allows us to fix the parameter `z` and pass around a partial function and reuse it as shown below: + 上記の`numbers.foldLeft(0)(_ + _)`はパラーメーター`z`を固定し、以下のように部分函数を渡し、再利用することができます。 + ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numberFunc = numbers.foldLeft(List[Int]())_ @@ -57,24 +63,25 @@ val cubes = numberFunc((xs, x) => xs:+ x*x*x) print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) ``` - Finally, `foldLeft` and `foldRight` can be used in any of the following terms, + 最後に、`foldLeft`と`foldRight`は以下のいずれかの表現で使うことができます。 + ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) -numbers.foldLeft(0)((sum, item) => sum + item) // Generic Form -numbers.foldRight(0)((sum, item) => sum + item) // Generic Form +numbers.foldLeft(0)((sum, item) => sum + item) // 一般形式 +numbers.foldRight(0)((sum, item) => sum + item) // 一般形式 -numbers.foldLeft(0)(_+_) // Curried Form -numbers.foldRight(0)(_+_) // Curried Form +numbers.foldLeft(0)(_+_) // カリー化形式 +numbers.foldRight(0)(_+_) // カリー化形式 -(0 /: numbers)(_+_) // Used in place of foldLeft -(numbers :\ 0)(_+_) // Used in place of foldRight +(0 /: numbers)(_+_) // foldLeft内での利用 +(numbers :\ 0)(_+_) // foldRight内での利用 ``` -#### Implicit parameters - To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is: - +#### 暗黙のパラメータ + `implicit`としてパラメータリストの中の特定のパラメータを指定するには、複数パラメータリストが使えます。 + こちらの例は以下のようになります。 ``` def execute(arg: Int)(implicit ec: ExecutionContext) = ??? ``` From 2769009a9c97eb24894962719af65b22e13e731d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sat, 16 Mar 2019 17:06:02 +0900 Subject: [PATCH 050/358] Japanese translation of TOUR OF SCALA ja_case-classes --- _ja/tour/case-classes.md | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index 607e449d0e..d8819bccee 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -1,6 +1,7 @@ --- layout: tour -title: Case Classes +title: ケースクラス +language: ja discourse: true @@ -14,29 +15,34 @@ prerequisite-knowledge: classes, basics, mutability redirect_from: "/tutorials/tour/case-classes.html" --- -Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we'll see how they are useful in [pattern matching](pattern-matching.html). +ケースクラスはこれから論じるいくつかの差異はあるものの普通のクラスと似ています。 +ケースクラスはイミュータブルなデータを作ることができます。 +このツアーの次のステップでは、[パターンマッチング](pattern-matching.html)でのそれらの有用性を解説します。 ## Defining a case class -A minimal case class requires the keywords `case class`, an identifier, and a parameter list (which may be empty): + +最小のケースクラスにはキーワード`case class`、識別子、パラメータリスト(空かもしれません)が必要です。 ```tut case class Book(isbn: String) val frankenstein = Book("978-0486282114") ``` -Notice how the keyword `new` was not used to instantiate the `Book` case class. This is because case classes have an `apply` method by default which takes care of object construction. +ケースクラス`Book`をインスタンス化する時キーワード`new`が使われていないことに気をつけてください。 +これはケースクラスが標準でオブジェクトの建造を処理する`apply`メソッドを保有するためです。 -When you create a case class with parameters, the parameters are public `val`s. +パラメータを利用しケースクラスを作る時は、パラメータはパブリック定数となります。 ``` case class Message(sender: String, recipient: String, body: String) val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") -println(message1.sender) // prints guillaume@quebec.ca -message1.sender = "travis@washington.us" // this line does not compile +println(message1.sender) // guillaume@quebec.ca が出力されます +message1.sender = "travis@washington.us" // この行はコンパイルされません ``` -You can't reassign `message1.sender` because it is a `val` (i.e. immutable). It is possible to use `var`s in case classes but this is discouraged. +`message1.sender` に再代入することはできません、なぜなら`val`(つまりイミュータブル)だからです。 +ケースクラスでは`var`も使うことができますが、お勧めできません。 -## Comparison -Case classes are compared by structure and not by reference: +## 比較 +ケースクラスは参照ではなく、構造で比較されます。 ``` case class Message(sender: String, recipient: String, body: String) @@ -44,10 +50,11 @@ val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?") val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?") val messagesAreTheSame = message2 == message3 // true ``` -Even though `message2` and `message3` refer to different objects, the value of each object is equal. +たとえ`message2`と`message3`が異なるオブジェクトを参照していたとしても、それぞれのオブジェクトの値は等価となります。 -## Copying -You can create a (shallow) copy of an instance of a case class simply by using the `copy` method. You can optionally change the constructor arguments. +## コピー +`copy`メソッドを使うことで簡単にケースクラスのインスタンスの(浅い)コピーを作ることができます。 +必要に応じて、コンストラクタ引数を変更することもできます。 ``` case class Message(sender: String, recipient: String, body: String) val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg") @@ -56,4 +63,4 @@ message5.sender // travis@washington.us message5.recipient // claire@bourgogne.fr message5.body // "Me zo o komz gant ma amezeg" ``` -The recipient of `message4` is used as the sender of `message5` but the `body` of `message4` was copied directly. +`message4`の定数recipientは`message5`の定数senderとして使われますが、`message4`の定数`body`は直接コピーされます。 From 3f446b50a0a23ff10b2f3d458850c246b1bfd64d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 17 Mar 2019 17:09:19 +0900 Subject: [PATCH 051/358] Japanese translation of TOUR OF SCALA pattern-matching --- _ja/tour/pattern-matching.md | 69 ++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 5a6d4969c4..af798e7c01 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -1,6 +1,7 @@ --- layout: tour -title: Pattern Matching +title: パターンマッチング +language: ja discourse: true @@ -15,10 +16,13 @@ prerequisite-knowledge: case-classes, string-interpolation, subtyping redirect_from: "/tutorials/tour/pattern-matching.html" --- -Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the `switch` statement in Java and it can likewise be used in place of a series of if/else statements. +パターンマッチングはパターンに対応した値をチェックするための機能です。 +マッチに成功すれば、はその構成要素のパーツを一つの値に分解することもできます。 +Javaの`switch`文の強化バージョンで、if/else文の連続の代わりとして同様に使うことができます。 -## Syntax -A match expression has a value, the `match` keyword, and at least one `case` clause. +## 構文 + +マッチ表現は値、キーワード`match`と最低でも1つの`case`句を持ちます。 ```tut import scala.util.Random @@ -31,9 +35,11 @@ x match { case _ => "many" } ``` -The `val x` above is a random integer between 0 and 10. `x` becomes the left operand of the `match` operator and on the right is an expression with four cases. The last case `_` is a "catch all" case for any number greater than 2. Cases are also called _alternatives_. +上記の`val x`は0から10の間のランダムな整数です。`x`は`match`演算子の左側被演算子となり、右側被演算子は4つのケースで表現されます。 +最後のケース`_`は 2より大きい全ての数字のための"全てを捕捉する"ケースです。 +ケースは*オルタナティブ*とも呼ばれます。 -Match expressions have a value. +マッチ表現は値を持ちます。 ```tut def matchTest(x: Int): String = x match { case 1 => "one" @@ -43,11 +49,12 @@ def matchTest(x: Int): String = x match { matchTest(3) // many matchTest(1) // one ``` -This match expression has a type String because all of the cases return String. Therefore, the function `matchTest` returns a String. +このマッチ表現はString型を持ちます、なぜなら全てのケースはStringを返します。 +そのため関数`matchTest`はStringを返します。 -## Matching on case classes +## ケースクラスでのマッチング -Case classes are especially useful for pattern matching. +ケースクラスはパターンマッチングで特に役立ちます。 ```tut abstract class Notification @@ -58,9 +65,9 @@ case class SMS(caller: String, message: String) extends Notification case class VoiceRecording(contactName: String, link: String) extends Notification - ``` -`Notification` is an abstract super class which has three concrete Notification types implemented with case classes `Email`, `SMS`, and `VoiceRecording`. Now we can do pattern matching on these case classes: +`Notification`はケースクラスで実装された`Email`、 `SMS`、 そして`VoiceRecording`3つの具体的なお知らせの種類を持つ抽象スーパークラスです。 +今、これらのケースクラスでパターンマッチングをすることができます。 ``` def showNotification(notification: Notification): String = { @@ -76,14 +83,17 @@ def showNotification(notification: Notification): String = { val someSms = SMS("12345", "Are you there?") val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123") -println(showNotification(someSms)) // prints You got an SMS from 12345! Message: Are you there? +println(showNotification(someSms)) // You got an SMS from 12345! Message: Are you there? が出力されます。 -println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123 +println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123 が出力されます。 ``` -The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(email, title, _)` the fields `email` and `title` are used in the return value but the `body` field is ignored with `_`. +関数`showNotification`は抽象型`Notification`のパラメータとして受け取り、`Notification` の種類とマッチします(すなわち`Email`、`SMS`、または `VoiceRecording`のいずれであるかを解決します)。 +`case Email(email, title, _)` ではフィールド`email`と`title`が戻り値として使われますが、`_`を使うことでフィールド`body`は無視されます。 + +## パターンガード +パターンガードはケースをより特別にするために使われる簡単な真偽表現です。 +ただ`if `をパターンの後ろに追加するだけです。 -## Pattern guards -Pattern guards are simply boolean expressions which are used to make cases more specific. Just add `if ` after the pattern. ``` def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = { @@ -93,7 +103,7 @@ def showImportantNotification(notification: Notification, importantPeopleInfo: S case SMS(number, _) if importantPeopleInfo.contains(number) => "You got an SMS from special someone!" case other => - showNotification(other) // nothing special, delegate to our original showNotification function + showNotification(other) // 特別なものではなく、オリジナルのshowNotification関数に委任します。 } } @@ -110,10 +120,11 @@ println(showImportantNotification(importantEmail, importantPeopleInfo)) println(showImportantNotification(importantSms, importantPeopleInfo)) ``` -In the `case Email(email, _, _) if importantPeopleInfo.contains(email)`, the pattern is matched only if the `email` is in the list of important people. +`case Email(email, _, _) if importantPeopleInfo.contains(email)`では、パターンは`email`が重要な人のリストに存在して初めてマッチします。 -## Matching on type only -You can match on the type like so: +## 型のみでのマッチング + +以下のように型のみでマッチすることができます。 ```tut abstract class Device case class Phone(model: String) extends Device{ @@ -128,10 +139,13 @@ def goIdle(device: Device) = device match { case c: Computer => c.screenSaverOn } ``` -`def goIdle` has a different behavior depending on the type of `Device`. This is useful when the case needs to call a method on the pattern. It is a convention to use the first letter of the type as the case identifier (`p` and `c` in this case). +`def goIdle`は`Device`のタイプによって異なる振る舞いをします。 +これはケースがパターン内でメソッドを呼び出す必要がある時に役立ちます。 +ケースの識別子には型の最初の一文字(この場合に`p`と`c`)を利用する慣習があります。 -## Sealed classes -Traits and classes can be marked `sealed` which means all subtypes must be declared in the same file. This assures that all subtypes are known. +## シールドクラス +トレイトとクラスは全てのサブタイプは同一ファイル内で宣言されているべきとする`sealed`でマークすることができます。 +これは全てのサブタイプは既知であると保証します。 ```tut sealed abstract class Furniture @@ -143,9 +157,10 @@ def findPlaceToSit(piece: Furniture): String = piece match { case b: Chair => "Sit on the chair" } ``` -This is useful for pattern matching because we don't need a "catch all" case. +これは"全てに対応する"ケースを必要としなくて済むので、パターンマッチングで役立ちます。 + +## 注意 +Scalaのパターンマッチング文は[ケース型](case-classes.html)を通して代数型の表現のマッチングの際に最も役立ちます。 -## Notes -Scala's pattern matching statement is most useful for matching on algebraic types expressed via [case classes](case-classes.html). -Scala also allows the definition of patterns independently of case classes, using `unapply` methods in [extractor objects](extractor-objects.html). +Scalaは[抽出子オブジェクト](extractor-objects.html)で`unapply`メソッドを利用した独立したケースクラスのパターン定義ができます。 From d563106901ef31e77208babffb606fe5aa2590f2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:49:25 +0900 Subject: [PATCH 052/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index af798e7c01..7b3096f6c9 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -16,7 +16,7 @@ prerequisite-knowledge: case-classes, string-interpolation, subtyping redirect_from: "/tutorials/tour/pattern-matching.html" --- -パターンマッチングはパターンに対応した値をチェックするための機能です。 +パターンマッチングはパターンに対応させて値をチェックするための機能です。 マッチに成功すれば、はその構成要素のパーツを一つの値に分解することもできます。 Javaの`switch`文の強化バージョンで、if/else文の連続の代わりとして同様に使うことができます。 From 8a6e92abc1507418eb88328212b34714f31514f6 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:50:30 +0900 Subject: [PATCH 053/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 7b3096f6c9..0194a9f01a 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -22,7 +22,7 @@ Javaの`switch`文の強化バージョンで、if/else文の連続の代わり ## 構文 -マッチ表現は値、キーワード`match`と最低でも1つの`case`句を持ちます。 +マッチ式は値、キーワード`match`と最低でも1つの`case`句を持ちます。 ```tut import scala.util.Random From 904f1817f92073e214b7bbb1f025ed0c2e112b18 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:53:22 +0900 Subject: [PATCH 054/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 0194a9f01a..990adc9aa7 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -35,7 +35,7 @@ x match { case _ => "many" } ``` -上記の`val x`は0から10の間のランダムな整数です。`x`は`match`演算子の左側被演算子となり、右側被演算子は4つのケースで表現されます。 +上記の`val x`は0から10の間のランダムな整数です。`x`は`match`演算子の左オペランドで、右側は4つのケースを持つ式です。 最後のケース`_`は 2より大きい全ての数字のための"全てを捕捉する"ケースです。 ケースは*オルタナティブ*とも呼ばれます。 From 981de99621dd6d86ab6966a5909ac0fecd55ec70 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:53:56 +0900 Subject: [PATCH 055/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 990adc9aa7..22af806f6b 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -17,7 +17,7 @@ redirect_from: "/tutorials/tour/pattern-matching.html" --- パターンマッチングはパターンに対応させて値をチェックするための機能です。 -マッチに成功すれば、はその構成要素のパーツを一つの値に分解することもできます。 +マッチに成功すれば、一つの値をその構成要素のパーツに分解することもできます。 Javaの`switch`文の強化バージョンで、if/else文の連続の代わりとして同様に使うことができます。 ## 構文 From b1a39bfb19c741e09d6720622ae9110b599de85e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:55:30 +0900 Subject: [PATCH 056/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 22af806f6b..26b9cbe34e 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -39,7 +39,7 @@ x match { 最後のケース`_`は 2より大きい全ての数字のための"全てを捕捉する"ケースです。 ケースは*オルタナティブ*とも呼ばれます。 -マッチ表現は値を持ちます。 +マッチ式は値を持ちます。 ```tut def matchTest(x: Int): String = x match { case 1 => "one" From 8da155847a5326ed3adefe37accc3a1cb2d496e3 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:57:56 +0900 Subject: [PATCH 057/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 26b9cbe34e..0e53162978 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -103,7 +103,7 @@ def showImportantNotification(notification: Notification, importantPeopleInfo: S case SMS(number, _) if importantPeopleInfo.contains(number) => "You got an SMS from special someone!" case other => - showNotification(other) // 特別なものではなく、オリジナルのshowNotification関数に委任します。 + showNotification(other) // 特別なものではなく、オリジナルのshowNotification関数に委譲します。 } } From b46594eff74f805a3490258f00fc9491f7a861c1 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:58:12 +0900 Subject: [PATCH 058/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 0e53162978..8c1208fcdd 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -145,7 +145,7 @@ def goIdle(device: Device) = device match { ## シールドクラス トレイトとクラスは全てのサブタイプは同一ファイル内で宣言されているべきとする`sealed`でマークすることができます。 -これは全てのサブタイプは既知であると保証します。 +これは全てのサブタイプは既知であることを保証します。 ```tut sealed abstract class Furniture From 6b69a82da833bba9cde8f18f33a136687d7e0460 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 10:59:28 +0900 Subject: [PATCH 059/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 8c1208fcdd..83d3737d47 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -160,7 +160,7 @@ def findPlaceToSit(piece: Furniture): String = piece match { これは"全てに対応する"ケースを必要としなくて済むので、パターンマッチングで役立ちます。 ## 注意 -Scalaのパターンマッチング文は[ケース型](case-classes.html)を通して代数型の表現のマッチングの際に最も役立ちます。 +Scalaのパターンマッチング文は[ケースクラス](case-classes.html)で表現される代数型のマッチングに最も役立ちます。 Scalaは[抽出子オブジェクト](extractor-objects.html)で`unapply`メソッドを利用した独立したケースクラスのパターン定義ができます。 From 14dbdd89019b7dd4eb3a8a01aef86ec8562c655b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 11:01:57 +0900 Subject: [PATCH 060/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 83d3737d47..5dc32cab50 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -144,7 +144,7 @@ def goIdle(device: Device) = device match { ケースの識別子には型の最初の一文字(この場合に`p`と`c`)を利用する慣習があります。 ## シールドクラス -トレイトとクラスは全てのサブタイプは同一ファイル内で宣言されているべきとする`sealed`でマークすることができます。 +トレイトとクラスに`sealed`をつけると、全てのサブタイプは同一ファイル内で宣言されなければならないという意味になります。 これは全てのサブタイプは既知であることを保証します。 ```tut From a5dbabbc09b57bafed2923f64ceb6a74181b5840 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 20 Mar 2019 11:07:17 +0900 Subject: [PATCH 061/358] Update _ja/tour/pattern-matching.md Co-Authored-By: take2webservice --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 5dc32cab50..a1bebfee0d 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -163,4 +163,4 @@ def findPlaceToSit(piece: Furniture): String = piece match { Scalaのパターンマッチング文は[ケースクラス](case-classes.html)で表現される代数型のマッチングに最も役立ちます。 -Scalaは[抽出子オブジェクト](extractor-objects.html)で`unapply`メソッドを利用した独立したケースクラスのパターン定義ができます。 +Scalaでは[抽出子オブジェクト](extractor-objects.html)の`unapply`メソッドを使うと、ケースクラスのパターンを独自に定義することもできます。 From 0c779165be5f92031092e9d4939c109f8cc21f38 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 20 Mar 2019 12:43:38 +0900 Subject: [PATCH 062/358] Japanese translation of TOUR OF SCALA singleton-objects --- _ja/tour/singleton-objects.md | 56 +++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 2e05c851f2..6f371b180f 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -1,6 +1,7 @@ --- layout: tour -title: Singleton Objects +title: シングルトンオブジェクト +language: ja discourse: true @@ -13,18 +14,18 @@ previous-page: pattern-matching redirect_from: "/tutorials/tour/singleton-objects.html" prerequisite-knowledge: classes, methods, private-methods, packages, option --- -An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val. +オブジェクトは丁度1つのインスタンスを持つクラスです。 +それは遅延評価valのように参照された際に遅れて作られます。 -As a top-level value, an object is a singleton. +オブジェクトはトップレベルの値のようにシングルトンです。 +エンクロージングクラス(外部クラス)のメンバーやローカルの値のように、オブジェクトはまるで遅延評価valのように振る舞います。 -As a member of an enclosing class or as a local value, it behaves exactly like a lazy val. -# Defining a singleton object -An object is a value. The definition of an object looks like a class, but uses the keyword `object`: +# シングルトンオブジェクトの定義 +オブジェクトは値です。オブジェクトの定義はクラスのように見えますが、キーワード`object`を使います。 ```tut object Box ``` - -Here's an example of an object with a method: +これはメソッドを持つオブジェクトの例です。 ``` package logging @@ -32,9 +33,10 @@ object Logger { def info(message: String): Unit = println(s"INFO: $message") } ``` -The method `info` can be imported from anywhere in the program. Creating utility methods like this is a common use case for singleton objects. +`info`メソッドはプログラム上のどこからでもimportすることができます。 +このように便利なメソッドを作ることはシングルトンオブジェクトのユースケースと同じです。 -Let's see how to use `info` in another package: +他のパッケージで`info`がどのように使われるか見てみましょう。 ``` import logging.Logger.info @@ -48,15 +50,21 @@ class Test { } ``` -The `info` method is visible because of the import statement, `import logging.Logger.info`. +import文`import logging.Logger.info`により、`info`メソッドが見えるようになります。 + +import文には取り込むシンボルへの"変動しないパス"が必要であり、オブジェクトは変動しないパスとなります。 -Imports require a "stable path" to the imported symbol, and an object is a stable path. +注意:`オブジェクト`がトップレベルではないが、他のクラスやオブジェクトを必要とする時、オブジェクトは他のメンバーのように"経路依存性"があります。 +これは2種類の飲み物`牛乳 クラス`と`オレンジジュース クラス`が与えられた場合、クラスメンバーの`栄養素 object`はエンクロージングクラス、すなわち牛乳またはオレンジジュースのいずれかのインスタンスに依存することを意味します。 +`milk.NutritionInfo`は`oj.NutritionInfo`とは全く異なります。 -Note: If an `object` is not top-level but is nested in another class or object, then the object is "path-dependent" like any other member. This means that given two kinds of beverages, `class Milk` and `class OrangeJuice`, a class member `object NutritionInfo` "depends" on the enclosing instance, either milk or orange juice. `milk.NutritionInfo` is entirely distinct from `oj.NutritionInfo`. +## コンパニオンオブジェクト -## Companion objects +クラスと同じ名前のオブジェクトは*コンパニオンオブジェクト*と呼ばれます。 +逆にクラスはオブジェクトのコンパニオンクラスとなります。 +コンパニオンクラスやコンパニオンオブジェクトは自身のコンパニオンのプライベートメンバーにアクセスできます。 +コンパニオンクラスのインスタンスの固有ではないメソッドや値にはコンパニオンオブジェクトを使います。 -An object with the same name as a class is called a _companion object_. Conversely, the class is the object's companion class. A companion class or object can access the private members of its companion. Use a companion object for methods and values which are not specific to instances of the companion class. ``` import scala.math._ @@ -74,9 +82,9 @@ val circle1 = new Circle(5.0) circle1.area ``` -The `class Circle` has a member `area` which is specific to each instance, and the singleton `object Circle` has a method `calculateArea` which is available to every instance. +`class Circle`は各インスタンスの固有のメンバー`area`を持ち、シングルトンオブジェクト`object Circle`は全てのインスタンスで利用できる`calculateArea`メソッドを持ちます。 -The companion object can also contain factory methods: +コンパニオンオブジェクトはファクトリーメソッドを含むことができます。 ```tut class Email(val username: String, val domainName: String) @@ -99,12 +107,16 @@ scalaCenterEmail match { case None => println("Error: could not parse email") } ``` -The `object Email` contains a factory `fromString` which creates an `Email` instance from a String. We return it as an `Option[Email]` in case of parsing errors. +`object Email`はファクトリー`fromString`を持ち、Stringから`Email`インスタンスを作ります。 +エラー解析のケース内では`Email`インスタンスを`Option[Email]`として返します。 -Note: If a class or object has a companion, both must be defined in the same file. To define companions in the REPL, either define them on the same line or enter `:paste` mode. +注意:クラスまたはオブジェクトがコンパニオンを持つ場合、クラス、オブジェクトの両方は同じファイルの中に定義されている必要があります。 +REPL内でコンパニオンを定義する場合は、それらを同じ行で定義するか、`:paste`モードに入ります。 -## Notes for Java programmers ## +## Javaプログラマのための注意事項 ## -`static` members in Java are modeled as ordinary members of a companion object in Scala. +Javaにおける`static`メンバーはScalaではコンパニオンオブジェクトの一般メンバーとして作られています。 -When using a companion object from Java code, the members will be defined in a companion class with a `static` modifier. This is called _static forwarding_. It occurs even if you haven't defined a companion class yourself. +Javaのコードからコンパニオンオブジェクトを使う場合、メンバーはコンパニオンクラス内で`static`識別子を用いて定義されます。 +これを*static forwarding*と呼ばれます。 +これはコンパニオンクラスを定義していなかったとしても起きます。 From 80b25e4bbb95bfe56904ef9735b6864bb98baa4b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:27:13 +0900 Subject: [PATCH 063/358] Update _ja/tour/nested-functions.md Co-Authored-By: take2webservice --- _ja/tour/nested-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/nested-functions.md b/_ja/tour/nested-functions.md index 582c8f1e25..86ac4cc607 100644 --- a/_ja/tour/nested-functions.md +++ b/_ja/tour/nested-functions.md @@ -14,7 +14,7 @@ previous-page: higher-order-functions redirect_from: "/tutorials/tour/nested-functions.html" --- -Scalaではネストメソッドの定義ができます。 +Scalaではメソッドの定義をネストすることができます。 以下のオブジェクトは与えられた数値の階乗を演算するための`factorial`メソッドを提供します。 {% scalafiddle %} From 3fd6d46ed5e97e020c6f69dd7794b58d1337fe55 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:29:44 +0900 Subject: [PATCH 064/358] Update _ja/tour/nested-functions.md Co-Authored-By: take2webservice --- _ja/tour/nested-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/nested-functions.md b/_ja/tour/nested-functions.md index 86ac4cc607..c8c4c73b18 100644 --- a/_ja/tour/nested-functions.md +++ b/_ja/tour/nested-functions.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/nested-functions.html" --- Scalaではメソッドの定義をネストすることができます。 -以下のオブジェクトは与えられた数値の階乗を演算するための`factorial`メソッドを提供します。 +以下のコードは与えられた数値の階乗を計算するための`factorial`メソッドを提供します。 {% scalafiddle %} ```tut From bfe2b5ddc5a844daff304f336fa5d76727801ef0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:53:29 +0900 Subject: [PATCH 065/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index d8819bccee..474b133e73 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/case-classes.html" --- ケースクラスはこれから論じるいくつかの差異はあるものの普通のクラスと似ています。 -ケースクラスはイミュータブルなデータを作ることができます。 +ケースクラスは不変なデータを作るのに適しています。 このツアーの次のステップでは、[パターンマッチング](pattern-matching.html)でのそれらの有用性を解説します。 ## Defining a case class From 28a4cbd39021f6fe34bfc249c13ac04c538fd045 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:54:00 +0900 Subject: [PATCH 066/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index 474b133e73..12dd7e4ea1 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -19,7 +19,7 @@ redirect_from: "/tutorials/tour/case-classes.html" ケースクラスは不変なデータを作るのに適しています。 このツアーの次のステップでは、[パターンマッチング](pattern-matching.html)でのそれらの有用性を解説します。 -## Defining a case class +## ケースクラスの宣言 最小のケースクラスにはキーワード`case class`、識別子、パラメータリスト(空かもしれません)が必要です。 ```tut From 0d6d787bd57351761209db92d6e257e8bfd30af3 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:56:03 +0900 Subject: [PATCH 067/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index 12dd7e4ea1..ad7f7eb626 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -28,7 +28,7 @@ case class Book(isbn: String) val frankenstein = Book("978-0486282114") ``` ケースクラス`Book`をインスタンス化する時キーワード`new`が使われていないことに気をつけてください。 -これはケースクラスが標準でオブジェクトの建造を処理する`apply`メソッドを保有するためです。 +これはケースクラスがオブジェクトの生成を行う`apply`メソッドを標準で保有するためです。 パラメータを利用しケースクラスを作る時は、パラメータはパブリック定数となります。 ``` From e4178ecc73660c93fbe636a903344aa9105505ce Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:56:58 +0900 Subject: [PATCH 068/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index ad7f7eb626..d19b2ff2e4 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -30,7 +30,7 @@ val frankenstein = Book("978-0486282114") ケースクラス`Book`をインスタンス化する時キーワード`new`が使われていないことに気をつけてください。 これはケースクラスがオブジェクトの生成を行う`apply`メソッドを標準で保有するためです。 -パラメータを利用しケースクラスを作る時は、パラメータはパブリック定数となります。 +パラメータ有りでケースクラスを作ると、パラメータはパブリックの`val`となります。 ``` case class Message(sender: String, recipient: String, body: String) val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") From f1a212fa6b2bdc7369761946917cd6b9e7ed776d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:57:19 +0900 Subject: [PATCH 069/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index d19b2ff2e4..51c117a36b 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -39,7 +39,7 @@ println(message1.sender) // guillaume@quebec.ca が出力されます message1.sender = "travis@washington.us" // この行はコンパイルされません ``` `message1.sender` に再代入することはできません、なぜなら`val`(つまりイミュータブル)だからです。 -ケースクラスでは`var`も使うことができますが、お勧めできません。 +ケースクラスでは`var`も使うことができますが、推奨されません。 ## 比較 ケースクラスは参照ではなく、構造で比較されます。 From 6cb1f4f58daa78de738efdff35a154ed311d03b8 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:57:48 +0900 Subject: [PATCH 070/358] Update _ja/tour/case-classes.md Co-Authored-By: take2webservice --- _ja/tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/case-classes.md b/_ja/tour/case-classes.md index 51c117a36b..99a1b6e6f0 100644 --- a/_ja/tour/case-classes.md +++ b/_ja/tour/case-classes.md @@ -63,4 +63,4 @@ message5.sender // travis@washington.us message5.recipient // claire@bourgogne.fr message5.body // "Me zo o komz gant ma amezeg" ``` -`message4`の定数recipientは`message5`の定数senderとして使われますが、`message4`の定数`body`は直接コピーされます。 +`message4`のrecipientは`message5`のsenderとして使われますが、`message4`の定数`body`は直接コピーされます。 From 208b5a187f32f4599fc5f8fd2e63e53b5445204e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 16:59:10 +0900 Subject: [PATCH 071/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 6f371b180f..d1821beb4c 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/singleton-objects.html" prerequisite-knowledge: classes, methods, private-methods, packages, option --- オブジェクトは丁度1つのインスタンスを持つクラスです。 -それは遅延評価valのように参照された際に遅れて作られます。 +それはlazy valのように参照された際に遅れて作られます。 オブジェクトはトップレベルの値のようにシングルトンです。 エンクロージングクラス(外部クラス)のメンバーやローカルの値のように、オブジェクトはまるで遅延評価valのように振る舞います。 From cf5c16a6d9119a41ffe68a76b825aaa90576d42a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:11:42 +0900 Subject: [PATCH 072/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index d1821beb4c..c4259c3793 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -17,7 +17,7 @@ prerequisite-knowledge: classes, methods, private-methods, packages, option オブジェクトは丁度1つのインスタンスを持つクラスです。 それはlazy valのように参照された際に遅れて作られます。 -オブジェクトはトップレベルの値のようにシングルトンです。 +トップレベルのオブジェクトは、シングルトンです。 エンクロージングクラス(外部クラス)のメンバーやローカルの値のように、オブジェクトはまるで遅延評価valのように振る舞います。 # シングルトンオブジェクトの定義 From 8e63fc648a517b282109623464a67cb7aaacb5db Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:13:46 +0900 Subject: [PATCH 073/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index c4259c3793..9fc5a5101a 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -18,7 +18,7 @@ prerequisite-knowledge: classes, methods, private-methods, packages, option それはlazy valのように参照された際に遅れて作られます。 トップレベルのオブジェクトは、シングルトンです。 -エンクロージングクラス(外部クラス)のメンバーやローカルの値のように、オブジェクトはまるで遅延評価valのように振る舞います。 +クラスのメンバーやローカル変数としてのオブジェクトは、lazy valと全く同じように振る舞います。 # シングルトンオブジェクトの定義 オブジェクトは値です。オブジェクトの定義はクラスのように見えますが、キーワード`object`を使います。 From 6c6041b833bdb903249aa5c0b876df057d7e753f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:36:32 +0900 Subject: [PATCH 074/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 9fc5a5101a..dd5b13245a 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -55,7 +55,7 @@ import文`import logging.Logger.info`により、`info`メソッドが見える import文には取り込むシンボルへの"変動しないパス"が必要であり、オブジェクトは変動しないパスとなります。 注意:`オブジェクト`がトップレベルではないが、他のクラスやオブジェクトを必要とする時、オブジェクトは他のメンバーのように"経路依存性"があります。 -これは2種類の飲み物`牛乳 クラス`と`オレンジジュース クラス`が与えられた場合、クラスメンバーの`栄養素 object`はエンクロージングクラス、すなわち牛乳またはオレンジジュースのいずれかのインスタンスに依存することを意味します。 +これは2種類の飲み物`class 牛乳`と`class オレンジジュース`が与えられた場合、クラスメンバーの`object 栄養素`はそれが属するインスタンス、すなわち牛乳またはオレンジジュースのいずれかに依存することを意味します。 `milk.NutritionInfo`は`oj.NutritionInfo`とは全く異なります。 ## コンパニオンオブジェクト From 1aec898e5c9aa6fe3ec13bc23b158b4efb8b0130 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:37:54 +0900 Subject: [PATCH 075/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index dd5b13245a..a2f6c09f5b 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -61,7 +61,7 @@ import文には取り込むシンボルへの"変動しないパス"が必要で ## コンパニオンオブジェクト クラスと同じ名前のオブジェクトは*コンパニオンオブジェクト*と呼ばれます。 -逆にクラスはオブジェクトのコンパニオンクラスとなります。 +逆にそのクラスはオブジェクトのコンパニオンクラスと呼ばれます。 コンパニオンクラスやコンパニオンオブジェクトは自身のコンパニオンのプライベートメンバーにアクセスできます。 コンパニオンクラスのインスタンスの固有ではないメソッドや値にはコンパニオンオブジェクトを使います。 From 341afa27e5b9ee0313b1a102e5111f8f280a985c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:39:10 +0900 Subject: [PATCH 076/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index a2f6c09f5b..580515396c 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -63,7 +63,7 @@ import文には取り込むシンボルへの"変動しないパス"が必要で クラスと同じ名前のオブジェクトは*コンパニオンオブジェクト*と呼ばれます。 逆にそのクラスはオブジェクトのコンパニオンクラスと呼ばれます。 コンパニオンクラスやコンパニオンオブジェクトは自身のコンパニオンのプライベートメンバーにアクセスできます。 -コンパニオンクラスのインスタンスの固有ではないメソッドや値にはコンパニオンオブジェクトを使います。 +コンパニオンクラスのインスタンスに特定されないメソッドや値にはコンパニオンオブジェクトを使います。 ``` import scala.math._ From 809ad8a2c0facdb428a6f1ca4c2ee2b318312bed Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:39:36 +0900 Subject: [PATCH 077/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 580515396c..5f06193fc9 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -119,4 +119,4 @@ Javaにおける`static`メンバーはScalaではコンパニオンオブジェ Javaのコードからコンパニオンオブジェクトを使う場合、メンバーはコンパニオンクラス内で`static`識別子を用いて定義されます。 これを*static forwarding*と呼ばれます。 -これはコンパニオンクラスを定義していなかったとしても起きます。 +これはコンパニオンクラスを自分で定義していなかったとしても起きます。 From e534256c031877cf1845001873d743823d1fea09 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:41:04 +0900 Subject: [PATCH 078/358] Update _ja/tour/singleton-objects.md Co-Authored-By: take2webservice --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 5f06193fc9..56ff5b7b66 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -108,7 +108,7 @@ scalaCenterEmail match { } ``` `object Email`はファクトリー`fromString`を持ち、Stringから`Email`インスタンスを作ります。 -エラー解析のケース内では`Email`インスタンスを`Option[Email]`として返します。 +パースエラーの場合も考えて、返り値の型を`Option[Email]`とします。 注意:クラスまたはオブジェクトがコンパニオンを持つ場合、クラス、オブジェクトの両方は同じファイルの中に定義されている必要があります。 REPL内でコンパニオンを定義する場合は、それらを同じ行で定義するか、`:paste`モードに入ります。 From 4607f32a0beebcd03aa9533e3b23fe8c6fb6e329 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:46:12 +0900 Subject: [PATCH 079/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 7d9beebbce..a17673d027 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/higher-order-functions.html" --- 高階関数は他の関数をパラメーターとして受け取る、もしくは結果として関数を返します。 -Scalaでは関数は第一級の値であるため、高階関数が可能となります。 +このようなことができるのは、Scalaでは関数がファーストクラスだからです。 この時点でその用語は少し混乱を招くことがあります。 ここでは"高階関数"というフレーズを関数をパラメーターとして受け取る、または関数を返すメソッドと関数の両方に対して使います。 From 2601aae073908ee497c3b4b8e1599fe157b010dc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:46:43 +0900 Subject: [PATCH 080/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index a17673d027..86760dc3ad 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/higher-order-functions.html" 高階関数は他の関数をパラメーターとして受け取る、もしくは結果として関数を返します。 このようなことができるのは、Scalaでは関数がファーストクラスだからです。 -この時点でその用語は少し混乱を招くことがあります。 +用語が少し紛らわしいかもしれませんが、 ここでは"高階関数"というフレーズを関数をパラメーターとして受け取る、または関数を返すメソッドと関数の両方に対して使います。 もっとも一般的な例の1つは、Scalaのコレクションで利用可能な高階関数`map`です。 From d04d4cd50218e0ecdee202acb32c3dff661d28af Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:47:39 +0900 Subject: [PATCH 081/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 86760dc3ad..71a199bc2d 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -26,7 +26,7 @@ val doubleSalary = (x: Int) => x * 2 val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000) ``` `doubleSalary`はInt`x`を1つだけ受け取り、`x * 2`を返す関数です。 -一般的に、アロー`=>`の左側のタプルは引数リストであり、右側の値が返されます。 +一般的に、アロー`=>`の左側のタプルは引数リストであり、右側の式の値が返されます。 3行目は給与のリストになります。 コードを減らすため、以下のように無名関数を作ることができ、引数として直接mapに渡すことができます From 5baa5950e1f4b109c5c6989b4fa1c92c8e69c8fd Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:48:23 +0900 Subject: [PATCH 082/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 71a199bc2d..8c7d343538 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -27,7 +27,7 @@ val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000) ``` `doubleSalary`はInt`x`を1つだけ受け取り、`x * 2`を返す関数です。 一般的に、アロー`=>`の左側のタプルは引数リストであり、右側の式の値が返されます。 -3行目は給与のリストになります。 +3行目で、給与のリストのそれぞれの値に`doubleSalary`が適用されます。 コードを減らすため、以下のように無名関数を作ることができ、引数として直接mapに渡すことができます ``` From 6bf7e862e4ea2766e3f3c1d5e1afdced66b4e61c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:48:58 +0900 Subject: [PATCH 083/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 8c7d343538..4af5a5150f 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -35,7 +35,7 @@ val salaries = Seq(20000, 70000, 40000) val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000) ``` 上記例では`x`をIntとして宣言していないことに注意してください。 -それはmapが期待する関数の型を基にコンパイラーが型を推論できるからです。 +それはmap関数が期待する型を基にコンパイラーが型を推論できるからです。 さらに言えば、慣用的には同じコードを以下のように書きます。 ```tut From 638d7a5cf424611239d04781284056df0c033b82 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:49:45 +0900 Subject: [PATCH 084/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 4af5a5150f..2b42ef3fca 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -56,7 +56,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { } ``` ここではメソッド`convertCtoF`は`forecastInFahrenheit`に渡されます。 -これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に強制変換することで実現します。 +これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に変換することで実現します。 ## 関数を受け取る関数 高階関数を使う理由の1つは冗長なコードを削減することです。 From 5135a8e08d5ccacbb839f2ed389365bd262b5bc7 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:50:03 +0900 Subject: [PATCH 085/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 2b42ef3fca..137e7cd57e 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -55,7 +55,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { def forecastInFahrenheit: Seq[Double] = temperatures.map(convertCtoF) // <-- convertCtoFメソッドが渡されます } ``` -ここではメソッド`convertCtoF`は`forecastInFahrenheit`に渡されます。 +ここで、メソッド`convertCtoF`が`forecastInFahrenheit`に渡されています。 これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に変換することで実現します。 ## 関数を受け取る関数 From 6b4d63ac3bc7b313c7bc23a1f2986b7f84393b38 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:50:16 +0900 Subject: [PATCH 086/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 137e7cd57e..95a6f5e8aa 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -59,7 +59,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に変換することで実現します。 ## 関数を受け取る関数 -高階関数を使う理由の1つは冗長なコードを削減することです。 +高階関数を使う理由の1つは余分なコードを削減することです。 いくつかの倍増率から人の給料を上げることができるメソッドが欲しいとすれば、こんな感じになるかもしれません。 ```tut From a04c4e3894dbc6c4c965c18ed8da0ea6adc94545 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:50:56 +0900 Subject: [PATCH 087/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 95a6f5e8aa..8c90a52bcb 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -60,7 +60,8 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { ## 関数を受け取る関数 高階関数を使う理由の1つは余分なコードを削減することです。 -いくつかの倍増率から人の給料を上げることができるメソッドが欲しいとすれば、こんな感じになるかもしれません。 +たとえば、何通りかの係数で人の給料を上げるメソッドが欲しいとしましょう。 +高階関数を作らないなら、こんな感じになるかもしれません。 ```tut object SalaryRaiser { From 39328a8c58c8e2811dbcbef71a87f7392ff15b44 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 17:51:12 +0900 Subject: [PATCH 088/358] Update _ja/tour/higher-order-functions.md Co-Authored-By: take2webservice --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 8c90a52bcb..337bb48679 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -77,7 +77,7 @@ object SalaryRaiser { } ``` -3つのメソッドはそれぞれ掛け算の倍増率のみ異なることに気をつけてください。 +3つのメソッドはそれぞれ掛け算の係数のみ異なることに気をつけてください。 簡潔にするため、以下のように繰り返されているコードを高階関数に抽出することができます。 ```tut From 580719e668739ca7637a7bf7e24cd0562ef258a5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:14:32 +0900 Subject: [PATCH 089/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index ec1fd6dd5a..dccdc75f72 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/multiple-parameter-lists.html" メソッドが少ない数のパラメータリストで呼び出された時、不足しているパラメータリストを引数として受け取る関数が生成されます。 これは一般的に[部分適用](https://en.wikipedia.org/wiki/Partial_application)として知られています。 -こちらはScalaのコレクションのドキュメントの[走査可能トレイト](/overviews/collections/trait-traversable.html) で定義された例になります。 +こちらはScalaのコレクションの[Traversable](/overviews/collections/trait-traversable.html) トレイトで定義されている実例です。 ``` def foldLeft[B](z: B)(op: (B, A) => B): B From 282e8d2eb3f518ea47674aee0aa361f1b7251ad0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:14:52 +0900 Subject: [PATCH 090/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index dccdc75f72..4024933c41 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -23,7 +23,7 @@ redirect_from: "/tutorials/tour/multiple-parameter-lists.html" ``` def foldLeft[B](z: B)(op: (B, A) => B): B ``` -`foldLeft`は初期値`z`とこの走査可能な全ての要素に対し左から右に二項演算子`op`を適用していきます。 +`foldLeft`は初期値`z`とこのtraversableの全ての要素に対し左から右に二項演算子`op`を適用していきます。 以下はその使い方の例です。 初期値0から始まり、`foldLeft`はここではリスト内の各要素とその一つ前の累積値に関数`(m, n) => m + n`を適用します。 From c541b4ba5710f9183953f410e393265b4cbf7674 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:16:19 +0900 Subject: [PATCH 091/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 4024933c41..cec90209c3 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -33,7 +33,6 @@ val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val res = numbers.foldLeft(0)((m, n) => m + n) print(res) // 55 ``` -複数パラメータリストにはより冗長な読み出し構文があり、それゆえ控えめに使う必要があります。 推奨されるユースケースは次の通りです。 #### 単一機能パラメータ From 34276b0800f1b976ced03a08123033a1e498b196 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:16:43 +0900 Subject: [PATCH 092/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index cec90209c3..0a1195266b 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -33,6 +33,7 @@ val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val res = numbers.foldLeft(0)((m, n) => m + n) print(res) // 55 ``` +### ユースケース 推奨されるユースケースは次の通りです。 #### 単一機能パラメータ From 560abf95799b7d9ac434b04961d30af5ff31bf88 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:16:51 +0900 Subject: [PATCH 093/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 0a1195266b..a069ccc9e9 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -36,7 +36,7 @@ print(res) // 55 ### ユースケース 推奨されるユースケースは次の通りです。 -#### 単一機能パラメータ +#### 単一の関数パラメータ 単一機能パラメータの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 複数パラメータリストがない場合、このコードは以下のようになります。 From b265f8c49d61229121bb5a9951f5d8b310736233 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:17:03 +0900 Subject: [PATCH 094/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index a069ccc9e9..d9f1e99e59 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -37,7 +37,7 @@ print(res) // 55 推奨されるユースケースは次の通りです。 #### 単一の関数パラメータ - 単一機能パラメータの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 + 関数のパラメータが一つの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 複数パラメータリストがない場合、このコードは以下のようになります。 ``` From f373437408304f4ca86b875b4d8ca767d1f0f25a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:17:25 +0900 Subject: [PATCH 095/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index d9f1e99e59..fa57c00d35 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -44,7 +44,7 @@ print(res) // 55 numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) ``` - 複数パラメータリストを使うことで、以下のようにコードをより簡潔にすることができ、Scalaの型インターフェースの利点を享受できることに注意してください。 + 複数パラメータリストを使うことで、Scalaの型インターフェースの利点を享受でき、以下のようにコードをより簡潔にすることができるのです。 それはカリー化されていない定義ではできません。 ``` From fa3ac012db1515dc7b608fe59c4bed3aabcc5110 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 28 Mar 2019 18:17:56 +0900 Subject: [PATCH 096/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index fa57c00d35..fb6c8dc3c9 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -45,7 +45,7 @@ numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) ``` 複数パラメータリストを使うことで、Scalaの型インターフェースの利点を享受でき、以下のようにコードをより簡潔にすることができるのです。 - それはカリー化されていない定義ではできません。 + これはカリー化されていない定義ではできません。 ``` numbers.foldLeft(0)(_ + _) From 16bfee340926d2f5946a1c1bcb0485e35b299e8b Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 2 Apr 2019 15:34:05 +0900 Subject: [PATCH 097/358] apply cahnges on 4afecb3ca50d77b959cc0f9de7653a29b22dc8b5 --- _ja/tour/multiple-parameter-lists.md | 74 +++++++++++++--------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index fb6c8dc3c9..c78418904e 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -14,75 +14,67 @@ previous-page: nested-functions redirect_from: "/tutorials/tour/multiple-parameter-lists.html" --- -メソッドは複数パラメータリストを定義できます。 -メソッドが少ない数のパラメータリストで呼び出された時、不足しているパラメータリストを引数として受け取る関数が生成されます。 -これは一般的に[部分適用](https://en.wikipedia.org/wiki/Partial_application)として知られています。 +メソッドは複数パラメータリストを持てます。 + +# 例 こちらはScalaのコレクションの[Traversable](/overviews/collections/trait-traversable.html) トレイトで定義されている実例です。 +こちらはScalaのコレクションAPIにて`TraversableOnce`の特徴が定義された例です。 ``` def foldLeft[B](z: B)(op: (B, A) => B): B ``` -`foldLeft`は初期値`z`とこのtraversableの全ての要素に対し左から右に二項演算子`op`を適用していきます。 +`foldLeft`は初期値`z`とこのコレクションの全ての要素に対し左から右に2つのパラメータを取る関数`op`を適用していきます。 以下はその使い方の例です。 初期値0から始まり、`foldLeft`はここではリスト内の各要素とその一つ前の累積値に関数`(m, n) => m + n`を適用します。 +{% scalafiddle %} ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val res = numbers.foldLeft(0)((m, n) => m + n) -print(res) // 55 +println(res) // 55 ``` +{% endscalafiddle %} + ### ユースケース -推奨されるユースケースは次の通りです。 +推奨される複数パラメータリストのユースケースは次の通りです。 #### 単一の関数パラメータ - 関数のパラメータが一つの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 - 複数パラメータリストがない場合、このコードは以下のようになります。 +関数のパラメータが一つの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 +複数パラメータリストがない場合、このコードは以下のようになります。 + ``` -numbers.foldLeft(0, {(m: Int, n: Int) => m + n}) +numbers.foldLeft(0, (m: Int, n: Int) => m + n) ``` - 複数パラメータリストを使うことで、Scalaの型インターフェースの利点を享受でき、以下のようにコードをより簡潔にすることができるのです。 - これはカリー化されていない定義ではできません。 - +複数パラメータリストを使うことで、Scalaの型インターフェースの利点を享受でき、以下のようにコードをより簡潔にすることができるのです。 + ``` numbers.foldLeft(0)(_ + _) ``` - 上記の`numbers.foldLeft(0)(_ + _)`はパラーメーター`z`を固定し、以下のように部分函数を渡し、再利用することができます。 +らScalaコンパイラは関数のパラメータの型推論することができないので、このようなことは単一パラメータリストではできません。 -```tut -val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) -val numberFunc = numbers.foldLeft(List[Int]())_ - -val squares = numberFunc((xs, x) => xs:+ x*x) -print(squares.toString()) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) +#### 暗黙のパラメータ +特定のパラメータを`implicit`として指定するには、それらはそれら自身を`暗黙の`パラメーターリストに置かなければなりません。 +こちらが例です。 -val cubes = numberFunc((xs, x) => xs:+ x*x*x) -print(cubes.toString()) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) +``` +def execute(arg: Int)(implicit ec: scala.concurrent.ExecutionContext) = ??? ``` - 最後に、`foldLeft`と`foldRight`は以下のいずれかの表現で使うことができます。 +#### 部分適用 +メソッドが少ない数のパラメータリストで呼び出された時、不足しているパラメータリストを引数として受け取る関数が生成されます。 +これは一般的に[部分適用](https://en.wikipedia.org/wiki/Partial_application)として知られています。 + +例えば ```tut val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) - -numbers.foldLeft(0)((sum, item) => sum + item) // 一般形式 -numbers.foldRight(0)((sum, item) => sum + item) // 一般形式 - -numbers.foldLeft(0)(_+_) // カリー化形式 -numbers.foldRight(0)(_+_) // カリー化形式 - -(0 /: numbers)(_+_) // foldLeft内での利用 -(numbers :\ 0)(_+_) // foldRight内での利用 -``` - - -#### 暗黙のパラメータ - `implicit`としてパラメータリストの中の特定のパラメータを指定するには、複数パラメータリストが使えます。 - こちらの例は以下のようになります。 -``` -def execute(arg: Int)(implicit ec: ExecutionContext) = ??? -``` - +val numberFunc = numbers.foldLeft(List[Int]()) _ +val squares = numberFunc((xs, x) => xs :+ x*x) +print(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) +val cubes = numberFunc((xs, x) => xs :+ x*x*x) +print(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) +``` \ No newline at end of file From bf85440f2bb3917de9ed934c073808456128f424 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 2 Apr 2019 15:46:10 +0900 Subject: [PATCH 098/358] apply changes of origin tour files to untranslated files --- _ja/tour/abstract-type-members.md | 6 +++--- _ja/tour/annotations.md | 6 +++--- _ja/tour/for-comprehensions.md | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index f60cb61766..90421a4491 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -34,7 +34,7 @@ abstract class SeqBuffer extends Buffer { def length = element.length } ``` -Notice how we can use yet another abstract `type U` as an upper-type-bound. This `class SeqBuffer` allows us to store only sequences in the buffer by stating that type `T` has to be a subtype of `Seq[U]` for a new abstract type `U`. +Notice how we can use yet another abstract type `U` in the specification of an upper-type-bound for `T`. This `class SeqBuffer` allows us to store only sequences in the buffer by stating that type `T` has to be a subtype of `Seq[U]` for a new abstract type `U`. Traits or [classes](classes.html) with abstract type members are often used in combination with anonymous class instantiations. To illustrate this, we now look at a program which deals with a sequence buffer that refers to a list of integers: @@ -53,7 +53,7 @@ val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) println("content = " + buf.element) ``` -Here the factory `newIntSeqBuf` uses an anonymous class implementation of `IntSeqBuf` (i.e. `new IntSeqBuffer`), setting `type T` to a `List[Int]`. +Here the factory `newIntSeqBuf` uses an anonymous class implementation of `IntSeqBuf` (i.e. `new IntSeqBuffer`) to set the abstract type `T` to the concrete type `List[Int]`. It is also possible to turn abstract type members into type parameters of classes and vice versa. Here is a version of the code above which only uses type parameters: @@ -75,4 +75,4 @@ println("length = " + buf.length) println("content = " + buf.element) ``` -Note that we have to use [variance annotations](variances.html) here (`+T <: Seq[U]`) in order to hide the concrete sequence implementation type of the object returned from method `newIntSeqBuf`. Furthermore, there are cases where it is not possible to replace abstract types with type parameters. +Note that we have to use [variance annotations](variances.html) here (`+T <: Seq[U]`) in order to hide the concrete sequence implementation type of the object returned from method `newIntSeqBuf`. Furthermore, there are cases where it is not possible to replace abstract type members with type parameters. diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index b486d7b806..fe44a8c3f0 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -77,7 +77,7 @@ And then apply it as follows ``` @Source(URL = "http://coders.com/", mail = "support@coders.com") -public class MyClass extends HisClass ... +public class MyClass extends TheirClass ... ``` An annotation application in Scala looks like a constructor invocation, for instantiating a Java annotation one has to use named arguments: @@ -101,7 +101,7 @@ And then apply it as follows ``` @SourceURL("http://coders.com/") -public class MyClass extends HisClass ... +public class MyClass extends TheirClass ... ``` In this case, Scala provides the same possibility @@ -116,7 +116,7 @@ The `mail` element was specified with a default value so we need not explicitly ``` @SourceURL(value = "http://coders.com/", mail = "support@coders.com") -public class MyClass extends HisClass ... +public class MyClass extends TheirClass ... ``` Scala provides more flexibility in this respect diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 85f5730cd0..446b0c4830 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -37,19 +37,19 @@ Here is a more complicated example using two generators. It computes all pairs o ```tut def foo(n: Int, v: Int) = for (i <- 0 until n; - j <- i until n if i + j == v) + j <- 0 until n if i + j == v) yield (i, j) foo(10, 10) foreach { case (i, j) => - println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) + println(s"($i, $j) ") // prints (1, 9) (2, 8) (3, 7) (4, 6) (5, 5) (6, 4) (7, 3) (8, 2) (9, 1) } ``` Here `n == 10` and `v == 10`. On the first iteration, `i == 0` and `j == 0` so `i + j != v` and therefore nothing is yielded. `j` gets incremented 9 more times before `i` gets incremented to `1`. Without the `if` guard, this would simply print the following: ``` -(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 1) ... +(0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 0) ... ``` Note that comprehensions are not restricted to lists. Every datatype that supports the operations `withFilter`, `map`, and `flatMap` (with the proper types) can be used in sequence comprehensions. @@ -59,7 +59,7 @@ You can omit `yield` in a comprehension. In that case, comprehension will return ```tut def foo(n: Int, v: Int) = for (i <- 0 until n; - j <- i until n if i + j == v) + j <- 0 until n if i + j == v) println(s"($i, $j)") foo(10, 10) From 802fc9ab54736293c76fe6d60964935bd0ef8731 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 2 Apr 2019 16:48:14 +0900 Subject: [PATCH 099/358] Japanese translation of TOUR OF SCALA regular-expression-patterns --- _ja/tour/regular-expression-patterns.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md index ec07f9f368..1d19dc3da9 100644 --- a/_ja/tour/regular-expression-patterns.md +++ b/_ja/tour/regular-expression-patterns.md @@ -1,6 +1,7 @@ --- layout: tour -title: Regular Expression Patterns +title: 正規表現パターン +language: ja discourse: true @@ -13,8 +14,8 @@ previous-page: singleton-objects redirect_from: "/tutorials/tour/regular-expression-patterns.html" --- - -Regular expressions are strings which can be used to find patterns (or lack thereof) in data. Any string can be converted to a regular expression using the `.r` method. +正規表現はデータの中からパターン(またはその欠如)を探すために使うことができる文字列です。 +どんな文字列も`.r`メソッドを使うことで、正規表現に変換できます。 ```tut import scala.util.matching.Regex @@ -26,11 +27,9 @@ numberPattern.findFirstMatchIn("awesomepassword") match { case None => println("Password must contain a number") } ``` +上記の例では、`numberPattern`は`Regex`(正規表現)型で、パスワードに数字が含まれていることを確認に使います。 -In the above example, the `numberPattern` is a `Regex` -(regular expression) which we use to make sure a password contains a number. - -You can also search for groups of regular expressions using parentheses. +括弧を使うことで、正規表現のグループを探すこともできます。 ```tut import scala.util.matching.Regex @@ -50,7 +49,8 @@ val input: String = for (patternMatch <- keyValPattern.findAllMatchIn(input)) println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}") ``` -Here we parse out the keys and values of a String. Each match has a group of sub-matches. Here is the output: +ここでは、文字列のキーとバリューを解析しています。 +それぞれの合致はサブマッチのグループを持ちます。こちらが出力結果になります。 ``` key: background-color value: #A03300 key: background-image value: url(img From 00a7e7efccb225c7e3cb6a476ff2e29eb8749191 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 2 Apr 2019 18:12:07 +0900 Subject: [PATCH 100/358] Japanese translation of TOUR OF SCALA extractor-objects --- _ja/tour/extractor-objects.md | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md index a346efd6bd..46972e4834 100644 --- a/_ja/tour/extractor-objects.md +++ b/_ja/tour/extractor-objects.md @@ -1,6 +1,7 @@ --- layout: tour -title: Extractor Objects +title: エクストラクタオブジェクト +language: ja discourse: true @@ -13,7 +14,9 @@ previous-page: regular-expression-patterns redirect_from: "/tutorials/tour/extractor-objects.html" --- -An extractor object is an object with an `unapply` method. Whereas the `apply` method is like a constructor which takes arguments and creates an object, the `unapply` takes an object and tries to give back the arguments. This is most often used in pattern matching and partial functions. +エクストラクタオブジェクトは`unapply`メソッドを持つオブジェクトです。 +`apply`メソッドが引数を取り、オブジェクトを作るコンストラクタであるように、`unapply`は1つのオブジェクトを受け取り、引数を返そうとします。 +これはパターンマッチングと部分関数で最も頻繁に使われます。 ```tut import scala.util.Random @@ -35,32 +38,37 @@ customer1ID match { } ``` -The `apply` method creates a `CustomerID` string from a `name`. The `unapply` does the inverse to get the `name` back. When we call `CustomerID("Sukyoung")`, this is shorthand syntax for calling `CustomerID.apply("Sukyoung")`. When we call `case CustomerID(name) => println(name)`, we're calling the unapply method. +`apply`メソッドは`name`から`CustomerID`文字列を作ります。`unapply`は逆に`name`を返します。 + `CustomerID("Sukyoung")`を呼び出すとき、これは`CustomerID.apply("Sukyoung")`を呼び出す速記構文になります。 + `case CustomerID(name) => println(name)`を呼び出すとき、unapplyメソッドを呼び出しています。 -Since a value definition can use a pattern to introduce a new variable, an extractor can be used to initialize the variable, where the unapply method supplies the value. +unapplyメソッドが値を供給する場合、新しい変数を導くパターンを使うことで値定義ができるので、エクストラクタは変数の初期化に使うことができます。 ```tut val customer2ID = CustomerID("Nico") val CustomerID(name) = customer2ID println(name) // prints Nico ``` - -This is equivalent to `val name = CustomerID.unapply(customer2ID).get`. +これは `val name = CustomerID.unapply(customer2ID).get`.と同じです。 ```tut val CustomerID(name2) = "--asdfasdfasdf" ``` - -If there is no match, a `scala.MatchError` is thrown: +もし一致しない場合`scala.MatchError`が投げられます。 ```tut:fail val CustomerID(name3) = "-asdfasdfasdf" ``` +`unapply`の戻り値型は以下のように選ばれなければなりません。 The return type of an `unapply` should be chosen as follows: -* If it is just a test, return a `Boolean`. For instance `case even()`. -* If it returns a single sub-value of type T, return an `Option[T]`. -* If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`. +* ただのテストであれば、`Boolean`を返します。例えば`case even()`。 +* T型のサブバリュー1つを返すのであれば、`Option[T]`を返します。 +* いくつかのサブバリュー`T1,...,Tn`を返したいのであれば、オプショナルタプル`Option[(T1,...,Tn)]`でグループ化します。 -Sometimes, the number of values to extract isn't fixed and we would like to return an arbitrary number of values, depending on the input. For this use case, you can define extractors with an `unapplySeq` method which returns an `Option[Seq[T]]`. Common examples of these patterns include deconstructing a `List` using `case List(x, y, z) =>` and decomposing a `String` using a regular expression `Regex`, such as `case r(name, remainingFields @ _*) =>`. +時々、抽出する値の数が確定せず、入力に応じて任意の数の値を返したいことがあります。 +このユースケースとして、`Option[Seq[T]]`を返す`unapplySeq`メソッドを持つエキストラクターを定義することができます。 +これらのパターンと同様の例として以下のものがあります。 +`case List(x, y, z) =>`を使って`List`を分解する。 +`case r(name, remainingFields @ _*) =>`.のように正規表現`Regex`を使って`String`を分解する。 From 98556f04afdc23109ac914c1b2ed5663d9ed68dd Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 3 Apr 2019 22:44:04 +0900 Subject: [PATCH 101/358] Japanese translation of TOUR OF SCALA for-comprehensions --- _ja/tour/for-comprehensions.md | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 446b0c4830..7624d92d87 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -1,6 +1,7 @@ --- layout: tour -title: For Comprehensions +title: for内包表記 +language: ja discourse: true @@ -13,9 +14,12 @@ previous-page: extractor-objects redirect_from: "/tutorials/tour/for-comprehensions.html" --- -Scala offers a lightweight notation for expressing *sequence comprehensions*. Comprehensions have the form `for (enumerators) yield e`, where `enumerators` refers to a semicolon-separated list of enumerators. An *enumerator* is either a generator which introduces new variables, or it is a filter. A comprehension evaluates the body `e` for each binding generated by the enumerators and returns a sequence of these values. +Scalaは*シーケンス内包表記*を表現するための軽量記法を提供します。 +`enumerators`がセミコロンで区切られたEnumeratorのリストを参照する場合、内含表記には`for (enumerators) yield e`の形式があります。 +1つの*enumerator*は新しい変数を導き出すジェネレータかフィルタのどちらかです。 +内包表記は本体`e`をenumeratorsにより生成された各バインディングとして評価され、これら値の列を返します。 -Here's an example: +こちらは例になります。 ```tut case class User(name: String, age: Int) @@ -26,13 +30,16 @@ val userBase = List(User("Travis", 28), User("Dennis", 23)) val twentySomethings = for (user <- userBase if (user.age >=20 && user.age < 30)) - yield user.name // i.e. add this to a list + yield user.name // すなわちこれをリストに追加する twentySomethings.foreach(name => println(name)) // prints Travis Dennis ``` -The `for` loop used with a `yield` statement actually creates a `List`. Because we said `yield user.name`, it's a `List[String]`. `user <- userBase` is our generator and `if (user.age >=20 && user.age < 30)` is a guard that filters out users who are not in their 20s. +`yield`文と使われるfor`ループ`は実は`List`を生成します。 +`yield user.name`と言ったので、それは`List[String]`です。 +`user <- userBase`はジェネレータであり、`if (user.age >=20 && user.age < 30)`は20代ではないユーザーをフィルターするガードです。 -Here is a more complicated example using two generators. It computes all pairs of numbers between `0` and `n-1` whose sum is equal to a given value `v`: +こちらは2つのジェネレータを使ったより複雑な例です。 +合計が与えられた値`v`と等しくなる、`0`から`n-1`の全てのペアを計算します。 ```tut def foo(n: Int, v: Int) = @@ -46,15 +53,18 @@ foo(10, 10) foreach { } ``` -Here `n == 10` and `v == 10`. On the first iteration, `i == 0` and `j == 0` so `i + j != v` and therefore nothing is yielded. `j` gets incremented 9 more times before `i` gets incremented to `1`. Without the `if` guard, this would simply print the following: +ここで`n == 10`と`v == 10`の場合、最初のイテレータでは`i == 0`と`j == 0`となり、`i + j != v`となるので、何も得られません。 +`i`が`1`に増加する前に、`j`はあと9回にインクリメントされます。`if`ガードがなければ、単純に以下の内容が表示されます。 + ``` (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 0) ... ``` - -Note that comprehensions are not restricted to lists. Every datatype that supports the operations `withFilter`, `map`, and `flatMap` (with the proper types) can be used in sequence comprehensions. - -You can omit `yield` in a comprehension. In that case, comprehension will return `Unit`. This can be useful in case you need to perform side-effects. Here's a program equivalent to the previous one, but without using `yield`: +内包表記はリストに制限されまん。 +演算子`withFilter`、`map`、そして (適切な型を持った)`flatMap`をサポートした全てのデータ型はシーケンス内包表記で使うことができます。 +内包表記の中では`yield`を省略することができます。その場合、内包表記は`Unit`を返します。 +これは副作用をもたらす必要があるときに役立ちます。 +こちらは先に出たプログラムと同等のものですが、`yield`を使っていません。 ```tut def foo(n: Int, v: Int) = From 61b717184185de4c60e7bb1ec801bf7b722626ab Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 3 Apr 2019 22:55:37 +0900 Subject: [PATCH 102/358] repair merge conflict miss --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 56ff5b7b66..e6862e489f 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -17,7 +17,7 @@ prerequisite-knowledge: classes, methods, private-methods, packages, option オブジェクトは丁度1つのインスタンスを持つクラスです。 それはlazy valのように参照された際に遅れて作られます。 -トップレベルのオブジェクトは、シングルトンです。 +トップレベルにあるオブジェクトは、シングルトンです。 クラスのメンバーやローカル変数としてのオブジェクトは、lazy valと全く同じように振る舞います。 # シングルトンオブジェクトの定義 From 50ca9772493c158001b06b5565fb6acc9908ea8b Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 3 Apr 2019 23:47:06 +0900 Subject: [PATCH 103/358] Japanese translation of TOUR OF SCALA generic-classes --- _ja/tour/generic-classes.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 2433cd6b45..a290ec776f 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -1,6 +1,7 @@ --- layout: tour -title: Generic Classes +title: ジェネリッククラス +language: ja discourse: true @@ -13,10 +14,11 @@ assumed-knowledge: classes unified-types redirect_from: "/tutorials/tour/generic-classes.html" --- -Generic classes are classes which take a type as a parameter. They are particularly useful for collection classes. +ジェネリッククラスはパラメータとして型を1つ受け取るクラスです。それらはコレクションクラスで特に役立ちます。 -## Defining a generic class -Generic classes take a type as a parameter within square brackets `[]`. One convention is to use the letter `A` as type parameter identifier, though any parameter name may be used. +## ジェネリッククラスの定義 +ジェネリッククラスは角カッコの中に`[]`パラメータとして型を1つ受け取ります。 +型引数の識別子として文字`A`を使う習慣がありますが、パラメータ名は任意のものを使うことができます。 ```tut class Stack[A] { private var elements: List[A] = Nil @@ -29,11 +31,14 @@ class Stack[A] { } } ``` -This implementation of a `Stack` class takes any type `A` as a parameter. This means the underlying list, `var elements: List[A] = Nil`, can only store elements of type `A`. The procedure `def push` only accepts objects of type `A` (note: `elements = x :: elements` reassigns `elements` to a new list created by prepending `x` to the current `elements`). +この`Stack`クラスの実装はパラメータとして任意の型`A`を受け取ります。 +これは基礎となるリスト`var elements: List[A] = Nil`は型`A`の要素のみを格納できることを意味します。 +手続き`def push`は型`A`のオブジェクトのみを受け取ります +(注意: `elements = x :: elements`は`elements`に`x`を現在の`elements`の先頭に追加した新しいリストを割り当て直します。)。 -## Usage +## 使い方 -To use a generic class, put the type in the square brackets in place of `A`. +ジェネリッククラスの使うには、`A`代わりに角カッコの中に型を入れます。 ``` val stack = new Stack[Int] stack.push(1) @@ -41,6 +46,8 @@ stack.push(2) println(stack.pop) // prints 2 println(stack.pop) // prints 1 ``` +インスタンス`stack`はIntのみを受け取ることができます。 +しかしながら、型がサブタイプを持つ場合、それらは以下のように通すことができます。 The instance `stack` can only take Ints. However, if the type argument had subtypes, those could be passed in: ``` class Fruit @@ -54,6 +61,5 @@ val banana = new Banana stack.push(apple) stack.push(banana) ``` -Class `Apple` and `Banana` both extend `Fruit` so we can push instances `apple` and `banana` onto the stack of `Fruit`. - -_Note: subtyping of generic types is *invariant*. This means that if we have a stack of characters of type `Stack[Char]` then it cannot be used as an integer stack of type `Stack[Int]`. This would be unsound because it would enable us to enter true integers into the character stack. To conclude, `Stack[A]` is only a subtype of `Stack[B]` if and only if `B = A`. Since this can be quite restrictive, Scala offers a [type parameter annotation mechanism](variances.html) to control the subtyping behavior of generic types._ +クラス`Apple`と`Banana`は共に`Fruit`を継承しています。そのため`Fruit`のスタックには`apple`と`banana`のインスタンスを追加できます。 +_注意: ジェネリック型のサブタイプは不変です。もし`Stack[Char]`型の文字スタックがあったとしても、それを`Stack[Int]`型の整数スタックとしては使うことができません。文字スタックに真の整数は入力できるので、このことは不合理に聞こえるかもしれません。最後に、`B = A`の場合に限り、`Stack[A]`はただの`Stack[B]`のサブタイプとなります。これは非常に制限的である場合があり、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ From a3cf9f7399ab09a12e79269699db8b4b4470fe2f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:25:03 +0900 Subject: [PATCH 104/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index c78418904e..e5066b8336 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -19,7 +19,6 @@ redirect_from: "/tutorials/tour/multiple-parameter-lists.html" # 例 こちらはScalaのコレクションの[Traversable](/overviews/collections/trait-traversable.html) トレイトで定義されている実例です。 -こちらはScalaのコレクションAPIにて`TraversableOnce`の特徴が定義された例です。 ``` def foldLeft[B](z: B)(op: (B, A) => B): B @@ -77,4 +76,4 @@ val squares = numberFunc((xs, x) => xs :+ x*x) print(squares) // List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) val cubes = numberFunc((xs, x) => xs :+ x*x*x) print(cubes) // List(1, 8, 27, 64, 125, 216, 343, 512, 729, 1000) -``` \ No newline at end of file +``` From 0bb5660f36dd73f2ebb75d0887e3c1166d1ac3d2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:27:56 +0900 Subject: [PATCH 105/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index e5066b8336..65d00fd171 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -40,7 +40,7 @@ println(res) // 55 推奨される複数パラメータリストのユースケースは次の通りです。 #### 単一の関数パラメータ -関数のパラメータが一つの場合、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを使うと簡潔な構文でメソッドに無名関数を渡すことができます。 +パラメータに関数を一つだけ渡すのであれば、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを利用して簡潔な構文でメソッドに無名関数を渡すことができます。 複数パラメータリストがない場合、このコードは以下のようになります。 From 12a76fa81056d40f54f4a66f98de53e0b0788d6f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:29:01 +0900 Subject: [PATCH 106/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 65d00fd171..9dcaf2d65b 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -53,7 +53,7 @@ numbers.foldLeft(0, (m: Int, n: Int) => m + n) ``` numbers.foldLeft(0)(_ + _) ``` -らScalaコンパイラは関数のパラメータの型推論することができないので、このようなことは単一パラメータリストではできません。 +単一のパラメータリストではScalaコンパイラが関数のパラメータを型推論できないので、このようなことはできません。 #### 暗黙のパラメータ 特定のパラメータを`implicit`として指定するには、それらはそれら自身を`暗黙の`パラメーターリストに置かなければなりません。 From 9f0dab3667b017f6dd85bcbf054fd248f7e35a01 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:29:36 +0900 Subject: [PATCH 107/358] Update _ja/tour/multiple-parameter-lists.md Co-Authored-By: take2webservice --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 9dcaf2d65b..23b978cb04 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -56,7 +56,7 @@ numbers.foldLeft(0)(_ + _) 単一のパラメータリストではScalaコンパイラが関数のパラメータを型推論できないので、このようなことはできません。 #### 暗黙のパラメータ -特定のパラメータを`implicit`として指定するには、それらはそれら自身を`暗黙の`パラメーターリストに置かなければなりません。 +特定のパラメータだけを`implicit`として指定するには、`implicit`のパラメーターリストに入れなければなりません。 こちらが例です。 ``` From c54fb1d1854586d1af1c07448b85d597c0405917 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:35:00 +0900 Subject: [PATCH 108/358] Update _ja/tour/regular-expression-patterns.md Co-Authored-By: take2webservice --- _ja/tour/regular-expression-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md index 1d19dc3da9..a2112d5acb 100644 --- a/_ja/tour/regular-expression-patterns.md +++ b/_ja/tour/regular-expression-patterns.md @@ -27,7 +27,7 @@ numberPattern.findFirstMatchIn("awesomepassword") match { case None => println("Password must contain a number") } ``` -上記の例では、`numberPattern`は`Regex`(正規表現)型で、パスワードに数字が含まれていることを確認に使います。 +上記の例では、`numberPattern`は`Regex`(正規表現)型で、パスワードに数字が含まれていることを確認するのに使います。 括弧を使うことで、正規表現のグループを探すこともできます。 From 9b97eb79faf1a4aa8a4fb26c29b881cc1fd3b7e8 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:35:06 +0900 Subject: [PATCH 109/358] Update _ja/tour/regular-expression-patterns.md Co-Authored-By: take2webservice --- _ja/tour/regular-expression-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md index a2112d5acb..06145f3720 100644 --- a/_ja/tour/regular-expression-patterns.md +++ b/_ja/tour/regular-expression-patterns.md @@ -49,7 +49,7 @@ val input: String = for (patternMatch <- keyValPattern.findAllMatchIn(input)) println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}") ``` -ここでは、文字列のキーとバリューを解析しています。 +ここでは、文字列のキーと値を解析しています。 それぞれの合致はサブマッチのグループを持ちます。こちらが出力結果になります。 ``` key: background-color value: #A03300 From 4553f67c6208f1ef5c9933e751bfc3a19f6db618 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:35:23 +0900 Subject: [PATCH 110/358] Update _ja/tour/regular-expression-patterns.md Co-Authored-By: take2webservice --- _ja/tour/regular-expression-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md index 06145f3720..9596579143 100644 --- a/_ja/tour/regular-expression-patterns.md +++ b/_ja/tour/regular-expression-patterns.md @@ -50,7 +50,7 @@ for (patternMatch <- keyValPattern.findAllMatchIn(input)) println(s"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}") ``` ここでは、文字列のキーと値を解析しています。 -それぞれの合致はサブマッチのグループを持ちます。こちらが出力結果になります。 +それぞれのマッチはサブマッチのグループを持ちます。こちらが出力結果です。 ``` key: background-color value: #A03300 key: background-image value: url(img From 217695fdc93ae1c342b1916fb287b2e538818a88 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:36:43 +0900 Subject: [PATCH 111/358] Update _ja/tour/extractor-objects.md Co-Authored-By: take2webservice --- _ja/tour/extractor-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md index 46972e4834..75622c57ed 100644 --- a/_ja/tour/extractor-objects.md +++ b/_ja/tour/extractor-objects.md @@ -39,7 +39,7 @@ customer1ID match { ``` `apply`メソッドは`name`から`CustomerID`文字列を作ります。`unapply`は逆に`name`を返します。 - `CustomerID("Sukyoung")`を呼び出すとき、これは`CustomerID.apply("Sukyoung")`を呼び出す速記構文になります。 + `CustomerID("Sukyoung")`は、`CustomerID.apply("Sukyoung")`を短く書く構文です。 `case CustomerID(name) => println(name)`を呼び出すとき、unapplyメソッドを呼び出しています。 unapplyメソッドが値を供給する場合、新しい変数を導くパターンを使うことで値定義ができるので、エクストラクタは変数の初期化に使うことができます。 From b647743ec79f82796c063f92222bb63c3fc45a6c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:37:20 +0900 Subject: [PATCH 112/358] Update _ja/tour/extractor-objects.md Co-Authored-By: take2webservice --- _ja/tour/extractor-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md index 75622c57ed..35d6621ef0 100644 --- a/_ja/tour/extractor-objects.md +++ b/_ja/tour/extractor-objects.md @@ -40,7 +40,7 @@ customer1ID match { `apply`メソッドは`name`から`CustomerID`文字列を作ります。`unapply`は逆に`name`を返します。 `CustomerID("Sukyoung")`は、`CustomerID.apply("Sukyoung")`を短く書く構文です。 - `case CustomerID(name) => println(name)`を呼び出すとき、unapplyメソッドを呼び出しています。 + `case CustomerID(name) => println(name)`では、unapplyメソッドを呼んでいます。 unapplyメソッドが値を供給する場合、新しい変数を導くパターンを使うことで値定義ができるので、エクストラクタは変数の初期化に使うことができます。 From de5d2f9521e9857238c5d97e4e4c3d1199f3ce3d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:39:26 +0900 Subject: [PATCH 113/358] Update _ja/tour/extractor-objects.md Co-Authored-By: take2webservice --- _ja/tour/extractor-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md index 35d6621ef0..c9ed18fbe3 100644 --- a/_ja/tour/extractor-objects.md +++ b/_ja/tour/extractor-objects.md @@ -42,7 +42,7 @@ customer1ID match { `CustomerID("Sukyoung")`は、`CustomerID.apply("Sukyoung")`を短く書く構文です。 `case CustomerID(name) => println(name)`では、unapplyメソッドを呼んでいます。 -unapplyメソッドが値を供給する場合、新しい変数を導くパターンを使うことで値定義ができるので、エクストラクタは変数の初期化に使うことができます。 +値を定義する文で、パターン中に新しい変数を使うことができるので、エクストラクタは変数を初期化するのに使えます。この場合unapplyメソッドが初期値を与えます。 ```tut val customer2ID = CustomerID("Nico") From 5b8a4ada53f5af1edff12d1d1a254bd8e4c2e8fe Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:40:21 +0900 Subject: [PATCH 114/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 7624d92d87..da90705221 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -14,7 +14,7 @@ previous-page: extractor-objects redirect_from: "/tutorials/tour/for-comprehensions.html" --- -Scalaは*シーケンス内包表記*を表現するための軽量記法を提供します。 +Scalaは*シーケンス内包表記*を表現するための軽量な記法を提供します。 `enumerators`がセミコロンで区切られたEnumeratorのリストを参照する場合、内含表記には`for (enumerators) yield e`の形式があります。 1つの*enumerator*は新しい変数を導き出すジェネレータかフィルタのどちらかです。 内包表記は本体`e`をenumeratorsにより生成された各バインディングとして評価され、これら値の列を返します。 From cd14c4deb885a6a6f822162770d090156b1cb8db Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:41:52 +0900 Subject: [PATCH 115/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index da90705221..93b2328d4e 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/for-comprehensions.html" --- Scalaは*シーケンス内包表記*を表現するための軽量な記法を提供します。 -`enumerators`がセミコロンで区切られたEnumeratorのリストを参照する場合、内含表記には`for (enumerators) yield e`の形式があります。 +内含表記は`for (enumerators) yield e`という形をとります。`enumerators`はセミコロンで区切られたEnumeratorのリストを指します。 1つの*enumerator*は新しい変数を導き出すジェネレータかフィルタのどちらかです。 内包表記は本体`e`をenumeratorsにより生成された各バインディングとして評価され、これら値の列を返します。 From f8f1364de332fe6a0110ff752528ee8f862dd984 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:42:44 +0900 Subject: [PATCH 116/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 93b2328d4e..d5af2051a9 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -30,7 +30,7 @@ val userBase = List(User("Travis", 28), User("Dennis", 23)) val twentySomethings = for (user <- userBase if (user.age >=20 && user.age < 30)) - yield user.name // すなわちこれをリストに追加する + yield user.name // これをリストに追加する twentySomethings.foreach(name => println(name)) // prints Travis Dennis ``` From 571f282fb6f50000f1d129b31f949bf77667688a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:43:39 +0900 Subject: [PATCH 117/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index d5af2051a9..118b708b2a 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -34,7 +34,7 @@ val twentySomethings = for (user <- userBase if (user.age >=20 && user.age < 30) twentySomethings.foreach(name => println(name)) // prints Travis Dennis ``` -`yield`文と使われるfor`ループ`は実は`List`を生成します。 +この`yield`文と一緒に使われている`for`ループは実際には`List`を生成します。 `yield user.name`と言ったので、それは`List[String]`です。 `user <- userBase`はジェネレータであり、`if (user.age >=20 && user.age < 30)`は20代ではないユーザーをフィルターするガードです。 From ba03ddd0a4c3d300c40bf4990a48b324d745f4bc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:44:23 +0900 Subject: [PATCH 118/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 118b708b2a..8e40e120be 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -35,7 +35,7 @@ val twentySomethings = for (user <- userBase if (user.age >=20 && user.age < 30) twentySomethings.foreach(name => println(name)) // prints Travis Dennis ``` この`yield`文と一緒に使われている`for`ループは実際には`List`を生成します。 -`yield user.name`と言ったので、それは`List[String]`です。 +`yield user.name`を返しているので、型は`List[String]`になります。 `user <- userBase`はジェネレータであり、`if (user.age >=20 && user.age < 30)`は20代ではないユーザーをフィルターするガードです。 こちらは2つのジェネレータを使ったより複雑な例です。 From dd362772ac4ad50c564c5ad601a0e240474d6d68 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 13:46:18 +0900 Subject: [PATCH 119/358] Update _ja/tour/for-comprehensions.md Co-Authored-By: take2webservice --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 8e40e120be..4490efa406 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -53,7 +53,7 @@ foo(10, 10) foreach { } ``` -ここで`n == 10`と`v == 10`の場合、最初のイテレータでは`i == 0`と`j == 0`となり、`i + j != v`となるので、何も得られません。 +この例では`n == 10`で`v == 10`です。最初のイテレーションでは`i == 0`かつ`j == 0`で、`i + j != v`となるので、何も生成されません。 `i`が`1`に増加する前に、`j`はあと9回にインクリメントされます。`if`ガードがなければ、単純に以下の内容が表示されます。 ``` From 47db9adf1d10ce9c88b3acd96d3de5341217bb99 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 14:08:24 +0900 Subject: [PATCH 120/358] Update _ja/tour/generic-classes.md Co-Authored-By: take2webservice --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index a290ec776f..0968e04fe9 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -17,7 +17,7 @@ redirect_from: "/tutorials/tour/generic-classes.html" ジェネリッククラスはパラメータとして型を1つ受け取るクラスです。それらはコレクションクラスで特に役立ちます。 ## ジェネリッククラスの定義 -ジェネリッククラスは角カッコの中に`[]`パラメータとして型を1つ受け取ります。 +ジェネリッククラスは角カッコ`[]`の中にパラメータとして型を1つ受け取ります。 型引数の識別子として文字`A`を使う習慣がありますが、パラメータ名は任意のものを使うことができます。 ```tut class Stack[A] { From 852c0407f3472aa41af5da7972b52b12b0097324 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 14:12:22 +0900 Subject: [PATCH 121/358] Update _ja/tour/generic-classes.md Co-Authored-By: take2webservice --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 0968e04fe9..92f94669d4 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -32,7 +32,7 @@ class Stack[A] { } ``` この`Stack`クラスの実装はパラメータとして任意の型`A`を受け取ります。 -これは基礎となるリスト`var elements: List[A] = Nil`は型`A`の要素のみを格納できることを意味します。 +これはメンバーのリスト`var elements: List[A] = Nil`が型`A`の要素のみを格納できることを意味します。 手続き`def push`は型`A`のオブジェクトのみを受け取ります (注意: `elements = x :: elements`は`elements`に`x`を現在の`elements`の先頭に追加した新しいリストを割り当て直します。)。 From e869c94abe6abfe3487a35a82c71766bb9386bc6 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 14:14:46 +0900 Subject: [PATCH 122/358] Update _ja/tour/generic-classes.md Co-Authored-By: take2webservice --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 92f94669d4..021bda8ea0 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -34,7 +34,7 @@ class Stack[A] { この`Stack`クラスの実装はパラメータとして任意の型`A`を受け取ります。 これはメンバーのリスト`var elements: List[A] = Nil`が型`A`の要素のみを格納できることを意味します。 手続き`def push`は型`A`のオブジェクトのみを受け取ります -(注意: `elements = x :: elements`は`elements`に`x`を現在の`elements`の先頭に追加した新しいリストを割り当て直します。)。 +(注: `elements = x :: elements`は、`x`を現在の`elements`の先頭に追加した新しいリストを`elements`に割り当て直します。)。 ## 使い方 From 2a659ea9d5c72f5285326767315eb33dd6e1dbdc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Tue, 9 Apr 2019 14:15:10 +0900 Subject: [PATCH 123/358] Update _ja/tour/generic-classes.md Co-Authored-By: take2webservice --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 021bda8ea0..54c8e7410e 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -38,7 +38,7 @@ class Stack[A] { ## 使い方 -ジェネリッククラスの使うには、`A`代わりに角カッコの中に型を入れます。 +ジェネリッククラスを使うには、角カッコの中に`A`の代わりに型を入れます。 ``` val stack = new Stack[Int] stack.push(1) From 590b9b96c5f07a4fa619031da476f16597804288 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 9 Apr 2019 15:24:55 +0900 Subject: [PATCH 124/358] Japanese translation of TOUR OF SCALA variances --- _ja/tour/variances.md | 96 +++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index f7f33947bd..5638395caa 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -1,6 +1,7 @@ --- layout: tour -title: Variances +title: 変位指定 +language: ja discourse: true @@ -13,19 +14,27 @@ previous-page: generic-classes redirect_from: "/tutorials/tour/variances.html" --- -Variance is the correlation of subtyping relationships of complex types and the subtyping relationships of their component types. Scala supports variance annotations of type parameters of [generic classes](generic-classes.html), to allow them to be covariant, contravariant, or invariant if no annotations are used. The use of variance in the type system allows us to make intuitive connections between complex types, whereas the lack of variance can restrict the reuse of a class abstraction. +変位指定は複合型のサブタイプ関係とコンポーネント型のサブタイプ関係の相関です。 +Scalaは型パラメータの[ジェネリッククラス](generic-classes.html)の変位指定アノテーションをサポートしています。 +そのためアノテーションを使わなかった場合に、共変、反変、または不変を利用することができます。 +型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 +その一方で変位指定が無ければ抽象性の再利用が制限されます。 + ```tut -class Foo[+A] // A covariant class -class Bar[-A] // A contravariant class -class Baz[A] // An invariant class +class Foo[+A] // 共変クラス +class Bar[-A] // 反変クラス +class Baz[A] // 不変クラス ``` -### Covariance +### 共変 -A type parameter `A` of a generic class can be made covariant by using the annotation `+A`. For some `class List[+A]`, making `A` covariant implies that for two types `A` and `B` where `A` is a subtype of `B`, then `List[A]` is a subtype of `List[B]`. This allows us to make very useful and intuitive subtyping relationships using generics. +ジェネリッククラスの型パラメータ`A`はアノテーション`+A`を使うことで共変にすることができます。 +ある`class List[+A]`がある時`A`を共変にすることは、2つの型`A`と`B`に対して`A`が`B`のサブタイプであることをほのめかします。 +その時、`List[A]`は`List[B]`のサブタイプとなります。 +これによりジェネリックを利用したとても便利で直感的なサブタイプの関係を作ることができます。 -Consider this simple class structure: +このシンプルなクラス構成を考えてみましょう。 ```tut abstract class Animal { @@ -34,10 +43,13 @@ abstract class Animal { case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal ``` +`Cat`と`Dog`は`Animal`のサブタイプです。 +型パラメータ`A`が共変である場合、Scalaの標準ライブラリはジェネリックイミュータブル`sealed abstract class List[+A]`クラスを持ちます。 +これは`List[Cat]`は`List[Animal]`であり、`List[Dog]`も`List[Animal]`であることを意味します。 +直感的に猫のリストと犬のリストはそれれぞれ動物のリストであり、それらを`List[Animal]`で代替できると理解できます。 -Both `Cat` and `Dog` are subtypes of `Animal`. The Scala standard library has a generic immutable `sealed abstract class List[+A]` class, where the type parameter `A` is covariant. This means that a `List[Cat]` is a `List[Animal]` and a `List[Dog]` is also a `List[Animal]`. Intuitively, it makes sense that a list of cats and a list of dogs are each lists of animals, and you should be able to substitute either of them for a `List[Animal]`. - -In the following example, the method `printAnimalNames` will accept a list of animals as an argument and print their names each on a new line. If `List[A]` were not covariant, the last two method calls would not compile, which would severely limit the usefulness of the `printAnimalNames` method. +以下の例では、メソッド`printAnimalNames`は引数に動物のリストを受け取り、新しい行にそれらの名前をプリントします。 +もし`List[A]`が共変でなければ、最後の2つのメソッド呼び出しはコンパイルされず、`printAnimalNames`メソッドの使い勝手はひどく制限されます。 ```tut object CovarianceTest extends App { @@ -60,19 +72,23 @@ object CovarianceTest extends App { } ``` -### Contravariance +### 反変 -A type parameter `A` of a generic class can be made contravariant by using the annotation `-A`. This creates a subtyping relationship between the class and its type parameter that is similar, but opposite to what we get with covariance. That is, for some `class Writer[-A]`, making `A` contravariant implies that for two types `A` and `B` where `A` is a subtype of `B`, `Writer[B]` is a subtype of `Writer[A]`. +型パラメータ`A`のジェネリッククラスはアノテーション`-A`を利用することで、反変にすることができます。 +これはクラスとその型パラメータの間でサブタイプ関係を作ります。 +それは似ていますが、共変で得られるものとは反対のものになります。 -Consider the `Cat`, `Dog`, and `Animal` classes defined above for the following example: +`class Writer[-A]`がある時、`A`を反変とすると、2つの型`A`と`B`に対し、`A`が`B`のサブタイプの場合、`Writer[B]`は`Writer[A]`のサブタイプであることを匂わせます。 + +先に定義された`Cat`、`Dog`、`Animal`クラスを以下の例で検討してみます。 ```tut abstract class Printer[-A] { def print(value: A): Unit } ``` - -A `Printer[A]` is a simple class that knows how to print out some type `A`. Let's define some subclasses for specific types: +`Printer[A]`はある型`A`をどのようにプリントするかを知っている簡単なクラスです。 +特定の型でいくつかのサブクラスを定義してみましょう。 ```tut class AnimalPrinter extends Printer[Animal] { @@ -85,8 +101,11 @@ class CatPrinter extends Printer[Cat] { println("The cat's name is: " + cat.name) } ``` - -If a `Printer[Cat]` knows how to print any `Cat` to the console, and a `Printer[Animal]` knows how to print any `Animal` to the console, it makes sense that a `Printer[Animal]` would also know how to print any `Cat`. The inverse relationship does not apply, because a `Printer[Cat]` does not know how to print any `Animal` to the console. Therefore, we should be able to substitute a `Printer[Animal]` for a `Printer[Cat]`, if we wish, and making `Printer[A]` contravariant allows us to do exactly that. +`Printer[Cat]`はコンソールに任意の`Cat`をプリントする方法を知っています。 +そして`Printer[Animal]`はコンソールに任意の`Animal`をプリントする方法を知っています。 +それは`Printer[Animal]`も任意の`Cat`をプリントする方法を知っていることを意味します。 +逆の関係性は適用されません、それは`Printer[Cat]`がコンソールに任意の`Animal`をプリントする方法を知らないからです。 +そのため`Printer[Animal]`を`Printer[Cat]`代わりに使い、`Printer[A]`を反変にすることができます。 ```tut object ContravarianceTest extends App { @@ -104,16 +123,18 @@ object ContravarianceTest extends App { } ``` -The output of this program will be: +この出力結果は以下のようになります。 ``` The cat's name is: Boots The animal's name is: Boots ``` -### Invariance +### 不変 -Generic classes in Scala are invariant by default. This means that they are neither covariant nor contravariant. In the context of the following example, `Container` class is invariant. A `Container[Cat]` is _not_ a `Container[Animal]`, nor is the reverse true. +Scalaのジェネリッククラスは標準では不変です。 +これは共変でも反変でもないことを意味します。 +以下の例の状況では、`Container`クラスは不変です。`Container[Cat]`は`Container[Animal]`_ではなく_、逆もまた真ではありません。 ```tut class Container[A](value: A) { @@ -124,31 +145,44 @@ class Container[A](value: A) { } } ``` - -It may seem like a `Container[Cat]` should naturally also be a `Container[Animal]`, but allowing a mutable generic class to be covariant would not be safe. In this example, it is very important that `Container` is invariant. Supposing `Container` was actually covariant, something like this could happen: +`Container[Cat]`が `Container[Animal]`でもあることはは当然かもしれませんが、ミュータブルジェネリッククラスが共変であることを許すことは安全ではありません。 +このようなことが起こりえます。 ``` val catContainer: Container[Cat] = new Container(Cat("Felix")) val animalContainer: Container[Animal] = catContainer animalContainer.setValue(Dog("Spot")) -val cat: Cat = catContainer.getValue // Oops, we'd end up with a Dog assigned to a Cat +val cat: Cat = catContainer.getValue // おっと、犬に猫に割り当ててしまった。 ``` -Fortunately, the compiler stops us long before we could get this far. +幸いにも、実行する前にコンパイラが止めてくれます。 -### Other Examples +### 他の例 -Another example that can help one understand variance is `trait Function1[-T, +R]` from the Scala standard library. `Function1` represents a function with one parameter, where the first type parameter `T` represents the parameter type, and the second type parameter `R` represents the return type. A `Function1` is contravariant over its argument type, and covariant over its return type. For this example we'll use the literal notation `A => B` to represent a `Function1[A, B]`. +変位指定を理解することを助けるもう一つの例はScalaの標準ライブラリの`trait Function1[-T, +R]`です。 +`Function1`は1つのパラメータを持つ関数を表します。1つ目の型パラメータ`T`はパラメータの型を表します。 +そして2つ目の型パラメータ`R`は戻り値の型を表します。 +`Function1`はその引数の型を通して反変であり、戻り値の型を通して共変です。 +この例では`Function1[A, B]`を表現するために`A => B`という文字での表記をします。 -Assume the similar `Cat`, `Dog`, `Animal` inheritance tree used earlier, plus the following: +先ほど利用されたものと同様に`Cat`, `Dog`, `Animal`の継承ツリーは以下のものを考えてください。 ```tut abstract class SmallAnimal extends Animal case class Mouse(name: String) extends SmallAnimal ``` -Suppose we're working with functions that accept types of animals, and return the types of food they eat. If we would like a `Cat => SmallAnimal` (because cats eat small animals), but are given a `Animal => Mouse` instead, our program will still work. Intuitively an `Animal => Mouse` will still accept a `Cat` as an argument, because a `Cat` is an `Animal`, and it returns a `Mouse`, which is also a `SmallAnimal`. Since we can safely and invisibly substitute the former by the latter, we can say `Animal => Mouse` is a subtype of `Cat => SmallAnimal`. +動物の種類を受け取り、それらが食べる食料の種類を返す関数を使うとした場合。 + +仮に(猫は小動物を食べるので)`Cat => SmallAnimal`が欲しいとします。 +しかし`Animal => Mouse`を代わりに与えられたとしても、私たちのプログラムはまだ動きます。 +直感的に、`Animal => Mouse` はまだ`Cat`を引数として受け取ります。 +なぜなら`Cat`は`Animal`であり、`SmallAnimal`である`Mouse`を返すからです。 + +安全かつ陰で前者に代えて後者を用いることができるため、`Animal => Mouse`は`Cat => SmallAnimal`のサブタイプと言うことができます。 -### Comparison With Other Languages +### 他の言語との比較 -Variance is supported in different ways by some languages that are similar to Scala. For example, variance annotations in Scala closely resemble those in C#, where the annotations are added when a class abstraction is defined (declaration-site variance). In Java, however, variance annotations are given by clients when a class abstraction is used (use-site variance). +変位指定はScalaに似たいくつかの言語によって他の方法でサポートされます。 +例えば、クラスの抽象化が定義(declaration-site variance)された時にアノテーションが追加されるという点で、Scalaの変位指定アノテーションはC#のそれと非常に似ています。 +しかしながら、Javaでは、クラスの抽象化が使われた時(use-site variance)に変位指定アノテーションはクライアントにより与えられます。 From 52a8caeacc146ac25649514eee7439c09fd2fe23 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 9 Apr 2019 16:50:40 +0900 Subject: [PATCH 125/358] Japanese translation of TOUR OF SCALA upper-type-bounds --- _ja/tour/upper-type-bounds.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/_ja/tour/upper-type-bounds.md b/_ja/tour/upper-type-bounds.md index 30df5918f7..f159a363ea 100644 --- a/_ja/tour/upper-type-bounds.md +++ b/_ja/tour/upper-type-bounds.md @@ -1,6 +1,7 @@ --- layout: tour -title: Upper Type Bounds +title: 上限型境界 +language: ja discourse: true @@ -13,8 +14,11 @@ previous-page: variances redirect_from: "/tutorials/tour/upper-type-bounds.html" --- -In Scala, [type parameters](generic-classes.html) and [abstract type members](abstract-type-members.html) may be constrained by a type bound. Such type bounds limit the concrete values of the type variables and possibly reveal more information about the members of such types. An _upper type bound_ `T <: A` declares that type variable `T` refers to a subtype of type `A`. -Here is an example that demonstrates upper type bound for a type parameter of class `PetContainer`: +Scalaでは [型パラメータ](generic-classes.html)と[抽象型メンバー](abstract-type-members.html) は型境界により制約されることがあります。 +型境界は具体的な値を制限し、そのような型のメンバーに関するより多くの情報を明らかにするかもしれません。 +_上限型境界_ は型変数`T`が型`A`のサブタイプを参照することを宣言します。 + +こちらはクラス`PetContainer`の型パラメータの上限型境界を実演する例です。 ```tut abstract class Animal { @@ -44,11 +48,13 @@ val catContainer = new PetContainer[Cat](new Cat) ``` ```tut:fail -// this would not compile +// これはコンパイルされません val lionContainer = new PetContainer[Lion](new Lion) ``` -The `class PetContainer` take a type parameter `P` which must be a subtype of `Pet`. `Dog` and `Cat` are subtypes of `Pet` so we can create a new `PetContainer[Dog]` and `PetContainer[Cat]`. However, if we tried to create a `PetContainer[Lion]`, we would get the following Error: +`class PetContainer`は型パラメータ`P`を受け取ります。それは`Pet`のサブタイプである必要があります。 + `Dog`と`Cat`は`Pet`のサブタイプです。そのため新たな`PetContainer[Dog]`と`PetContainer[Cat]`を作ることができます。 + しかしながら、もし`PetContainer[Lion]`を作ろうとすると、以下のエラーが返ってきます。 `type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]` -This is because `Lion` is not a subtype of `Pet`. +これは`Lion`は`Pet`のサブタイプではないからです。 From cb055c029c6acc39aeceddd46bcafbaf1e1a2442 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Tue, 9 Apr 2019 19:59:45 +0900 Subject: [PATCH 126/358] Japanese translation of TOUR OF SCALA lower-type-bounds --- _ja/tour/lower-type-bounds.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 47acb7664c..a12365974e 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -1,6 +1,7 @@ --- layout: tour -title: Lower Type Bounds +title: 下限型境界 +language: ja discourse: true @@ -14,9 +15,9 @@ prerequisite-knowledge: upper-type-bounds, generics, variance redirect_from: "/tutorials/tour/lower-type-bounds.html" --- -While [upper type bounds](upper-type-bounds.html) limit a type to a subtype of another type, *lower type bounds* declare a type to be a supertype of another type. The term `B >: A` expresses that the type parameter `B` or the abstract type `B` refer to a supertype of type `A`. In most cases, `A` will be the type parameter of the class and `B` will be the type parameter of a method. + [上限型境界](upper-type-bounds.html) が型を別の型のサブタイプに制限する一方で、*下限型境界*は型が別の型のスーパータイプになるよう宣言します。表現`B >: A`はパラメータ`B`または抽象型`B`が型`A`のスーパータイプを指すことを表します。ほとんどのケースで`A`はそのクラスの型パラメータであり、`B`はメソッドの型パラメータになります。 -Here is an example where this is useful: +以下はこれが役立つ場合の例です。 ```tut:fail trait Node[+B] { @@ -34,11 +35,15 @@ case class Nil[+B]() extends Node[B] { } ``` -This program implements a singly-linked list. `Nil` represents an empty element (i.e. an empty list). `class ListNode` is a node which contains an element of type `B` (`head`) and a reference to the rest of the list (`tail`). The `class Node` and its subtypes are covariant because we have `+B`. +このプログラムは片方向リストを実装します。`Nil`は空の要素(すなわち空のリスト)を意味します。 +`class ListNode`は型`B` (`head`)の要素を含むノードであり、リストの残りの部分(`tail`)への参照です。 +`class Node`とそのスーパータイプは共変であるので、`+B`としています。 -However, this program does _not_ compile because the parameter `elem` in `prepend` is of type `B`, which we declared *co*variant. This doesn't work because functions are *contra*variant in their parameter types and *co*variant in their result types. +しかしながら、このプログラムはコンパイル _されません_。`prepend`の中のパラメータ`elem`が *共* 変と宣言した型`B`だからです。 +これは動きません、なぜなら関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 -To fix this, we need to flip the variance of the type of the parameter `elem` in `prepend`. We do this by introducing a new type parameter `U` that has `B` as a lower type bound. +これを解決するためには、`prepend`の中でパラメータ`elem`の型の変位指定を弾く必要があります。 +これを実現するには、下限型境界として`B`を持つ新しい型パラメータ`U`を導入します。 ```tut trait Node[+B] { @@ -56,7 +61,7 @@ case class Nil[+B]() extends Node[B] { } ``` -Now we can do the following: +今、以下のようなことができます。 ```tut trait Bird case class AfricanSwallow() extends Bird @@ -67,4 +72,5 @@ val africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil()) val birdList: Node[Bird] = africanSwallowList birdList.prepend(new EuropeanSwallow) ``` -The `Node[Bird]` can be assigned the `africanSwallowList` but then accept `EuropeanSwallow`s. +`Node[Bird]`は`africanSwallowList`アサインできますが、その際`EuropeanSwallow`を受け取ります。 + From 37999b26c43054951880ccbdef992e31c2f02566 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 10 Apr 2019 00:31:20 +0900 Subject: [PATCH 127/358] Update _ja/tour/generic-classes.md Co-Authored-By: take2webservice --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 54c8e7410e..a7c355a86a 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -62,4 +62,4 @@ stack.push(apple) stack.push(banana) ``` クラス`Apple`と`Banana`は共に`Fruit`を継承しています。そのため`Fruit`のスタックには`apple`と`banana`のインスタンスを追加できます。 -_注意: ジェネリック型のサブタイプは不変です。もし`Stack[Char]`型の文字スタックがあったとしても、それを`Stack[Int]`型の整数スタックとしては使うことができません。文字スタックに真の整数は入力できるので、このことは不合理に聞こえるかもしれません。最後に、`B = A`の場合に限り、`Stack[A]`はただの`Stack[B]`のサブタイプとなります。これは非常に制限的である場合があり、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ +_注意: ジェネリック型のサブタイプは不変です。`Stack[Char]`型の文字スタックがあるとき、それを`Stack[Int]`型の整数スタックとしては使うことはできません。文字スタックに整数を入れることはできるので、このことは変に思えるしれません。結論としては、`B = A`の場合に限り、`Stack[A]`は`Stack[B]`の唯一のサブタイプとなります。これでは制限が強いので、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ From 03f59ed5981a4553deeff193f5c7c40e38817bb5 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 10 Apr 2019 00:37:02 +0900 Subject: [PATCH 128/358] Update _ja/tour/multiple-parameter-lists.md --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 23b978cb04..70306c1a04 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -39,7 +39,7 @@ println(res) // 55 ### ユースケース 推奨される複数パラメータリストのユースケースは次の通りです。 -#### 単一の関数パラメータ +#### パラメータに関数を一つ渡す場合 パラメータに関数を一つだけ渡すのであれば、上記の`foldLeft`のケースでの`op`のように、複数パラメータリストを利用して簡潔な構文でメソッドに無名関数を渡すことができます。 複数パラメータリストがない場合、このコードは以下のようになります。 From 32719849d2b5d543c74fc514fa15ec31825022ea Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 10 Apr 2019 02:39:27 +0900 Subject: [PATCH 129/358] Japanese translation of TOUR OF SCALA inner-classes --- _ja/tour/inner-classes.md | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 172dd03bcd..4fd114a072 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -1,6 +1,7 @@ --- layout: tour -title: Inner Classes +title: 内部クラス +language: ja discourse: true @@ -13,9 +14,12 @@ previous-page: lower-type-bounds redirect_from: "/tutorials/tour/inner-classes.html" --- -In Scala it is possible to let classes have other classes as members. As opposed to Java-like languages where such inner classes are members of the enclosing class, in Scala such inner classes are bound to the outer object. Suppose we want the compiler to prevent us, at compile time, from mixing up which nodes belong to what graph. Path-dependent types provide a solution. +Scalaではクラスが他のクラスをメンバーとして保持することが可能です。 +Javaのような言語とは対照的に、内部クラスは外側のクラスのメンバーとなる場合、Scalaではそのような内部クラスは外側のオブジェクトに束縛されます。 +コンパイルの際、コンパイラにどのノードがなんのグラフに属しているのか混同することを防いでほしいのです。 +パス依存型はその解決策の1つです。 -To illustrate the difference, we quickly sketch the implementation of a graph datatype: +その違いを図示するために、グラフデータ型の実装をさっと書きます。 ```tut class Graph { @@ -35,7 +39,8 @@ class Graph { } } ``` -This program represents a graph as a list of nodes (`List[Node]`). Each node has a list of other nodes it's connected to (`connectedNodes`). The `class Node` is a _path-dependent type_ because it is nested in the `class Graph`. Therefore, all nodes in the `connectedNodes` must be created using the `newNode` from the same instance of `Graph`. +このプログラムはグラフをノードのリスト(`List[Node]`)で表現しています。いずれのノードも接続している他のノードへのリスト(`connectedNodes`)を保持します。`class Node`は `class Graph`の中にネストしているので、 _パス依存型_ です。 +そのため`connectedNodes`の中にある全てのノードは同じ`Graph`インスタンスから`newNode`を使用し作る必要があります。 ```tut val graph1: Graph = new Graph @@ -45,10 +50,12 @@ val node3: graph1.Node = graph1.newNode node1.connectTo(node2) node3.connectTo(node1) ``` -We have explicitly declared the type of `node1`, `node2`, and `node3` as `graph1.Node` for clarity but the compiler could have inferred it. This is because when we call `graph1.newNode` which calls `new Node`, the method is using the instance of `Node` specific to the instance `graph1`. +`node1`、`node2`、そして `node3`の型が`graph1.Node`であることを明確にするために、明示的に宣言しています。 +これは`graph1.newNode`を呼び出す時、`new Node`を呼び出し、そのメソッドがインスタンス`graph1`特有の`Node`のインスタンスを使用しているからです。 -If we now have two graphs, the type system of Scala does not allow us to mix nodes defined within one graph with the nodes of another graph, since the nodes of the other graph have a different type. -Here is an illegal program: +今2つのグラフがあれば、Scalaの型システムは1つのグラフの中でノードと別のグラフのノードを混ぜることを許しません。 +それは他のグラフのノードは別の型だからです。 +こちらは不正なプログラムです。 ``` val graph1: Graph = new Graph @@ -59,7 +66,10 @@ val graph2: Graph = new Graph val node3: graph2.Node = graph2.newNode node1.connectTo(node3) // illegal! ``` -The type `graph1.Node` is distinct from the type `graph2.Node`. In Java, the last line in the previous example program would have been correct. For nodes of both graphs, Java would assign the same type `Graph.Node`; i.e. `Node` is prefixed with class `Graph`. In Scala such a type can be expressed as well, it is written `Graph#Node`. If we want to be able to connect nodes of different graphs, we have to change the definition of our initial graph implementation in the following way: +型`graph1.Node`は`graph2.Node`とは異なります。Javaでは先のプログラム例の最後の行は正しいでしょう。 +2つのグラフのノードに対して、Javaは同じ型`Graph.Node`を指定します。つまりクラス`Graph`は`Node`の接頭辞です。 +Scalaではそのような型も同様に表現することができ、`Graph#Node`と書きます。 +もし他のグラフのノードに接続できるようにしたければ、以下の方法で最初のグラフ実装の定義を変える必要があります。 ```tut class Graph { From 91ec5c7bafea51360baf23aa04ca24d6fa3ed4d8 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 10 Apr 2019 03:43:14 +0900 Subject: [PATCH 130/358] Japanese translation of TOUR OF SCALA abstract-type-members --- _ja/tour/abstract-type-members.md | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index 90421a4491..3b97ffb15b 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -1,6 +1,7 @@ --- layout: tour -title: Abstract Type Members +title: 抽象型メンバー +language: ja discourse: true @@ -15,9 +16,9 @@ redirect_from: "/tutorials/tour/abstract-types.html" redirect_from: "/tour/abstract-types.html" --- -Abstract types, such as traits and abstract classes, can in turn have abstract type members. -This means that the concrete implementations define the actual types. -Here's an example: +トレイトと抽象クラスのような抽象型は順番に抽象型メンバーを持つことができます。 +これは具体的な実装が明確に型を定義することを意味します。 +こちらが例になります。 ```tut trait Buffer { @@ -25,7 +26,7 @@ trait Buffer { val element: T } ``` -Here we have defined an abstract `type T`. It is used to describe the type of `element`. We can extend this trait in an abstract class, adding an upper-type-bound to `T` to make it more specific. +こちらでは、抽象型`type T`を定義しています。それは`element`の型を記述するために使われます。このトレイトを抽象クラスで継承し、より具体的にするために上限型境界を`T`に追加することができます。 ```tut abstract class SeqBuffer extends Buffer { @@ -34,9 +35,12 @@ abstract class SeqBuffer extends Buffer { def length = element.length } ``` -Notice how we can use yet another abstract type `U` in the specification of an upper-type-bound for `T`. This `class SeqBuffer` allows us to store only sequences in the buffer by stating that type `T` has to be a subtype of `Seq[U]` for a new abstract type `U`. +`T`の上限型境界の仕様において、更に別の抽象型`U`の使い方に気をつけてください。 -Traits or [classes](classes.html) with abstract type members are often used in combination with anonymous class instantiations. To illustrate this, we now look at a program which deals with a sequence buffer that refers to a list of integers: +この`class SeqBuffer`はバッファーの中のシーケンスのみを保存することができます。それは新しい抽象型`U`により型`T`は`Seq[U]`のサブタイプである必要があることを明言しているからです。 + +抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使います。 +これを説明するために、今から整数のリストを参照するシーケンスバッファーを扱うプログラムを見てみます。 ```tut abstract class IntSeqBuffer extends SeqBuffer { @@ -53,9 +57,9 @@ val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) println("content = " + buf.element) ``` -Here the factory `newIntSeqBuf` uses an anonymous class implementation of `IntSeqBuf` (i.e. `new IntSeqBuffer`) to set the abstract type `T` to the concrete type `List[Int]`. +こちらでは抽象型`T`を具体的な型`List[Int]`に設定するために、ファクトリー`newIntSeqBuf`は`IntSeqBuf`(つまり`new IntSeqBuffer`)の無名クラスの実装を使います。 -It is also possible to turn abstract type members into type parameters of classes and vice versa. Here is a version of the code above which only uses type parameters: +これにより抽象型メンバーをクラスの型パラメータに変換したり、その逆も可能となります。以下は上記コードの型パラメータのみを使うバージョンです。 ```tut abstract class Buffer[+T] { @@ -74,5 +78,4 @@ val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) println("content = " + buf.element) ``` - -Note that we have to use [variance annotations](variances.html) here (`+T <: Seq[U]`) in order to hide the concrete sequence implementation type of the object returned from method `newIntSeqBuf`. Furthermore, there are cases where it is not possible to replace abstract type members with type parameters. +ここでは(`+T <: Seq[U]`)をメソッド`newIntSeqBuf`から戻されるオブジェクトの具体的なシーケンス実装の型を隠すために [変位指定アノテーション](variances.html)を使わなければなりません。さらに、抽象型メンバをパラメータで置換することができないケースがあります。 From 2e5bcc72eeecca741cc4176eeac28a7ccaecc963 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 10 Apr 2019 04:11:43 +0900 Subject: [PATCH 131/358] Japanese translation of TOUR OF SCALA compound-types --- _ja/tour/compound-types.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 3e36f604e3..fbbce00689 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -1,6 +1,7 @@ --- layout: tour -title: Compound Types +title: 複合型 +language: ja discourse: true @@ -12,10 +13,10 @@ previous-page: abstract-type-members redirect_from: "/tutorials/tour/compound-types.html" --- +オブジェクトの型がいくつかの他の型のサブタイプであることを表現する必要がある時があります。 +Scalaでは*複合型*の助けにより表現することができます。それはオブジェクト型の交点です。 -Sometimes it is necessary to express that the type of an object is a subtype of several other types. In Scala this can be expressed with the help of *compound types*, which are intersections of object types. - -Suppose we have two traits `Cloneable` and `Resetable`: +2つのトレイト`Cloneable`と`Resetable`があると考えてください。 ```tut trait Cloneable extends java.lang.Cloneable { @@ -28,7 +29,7 @@ trait Resetable { } ``` -Now suppose we want to write a function `cloneAndReset` which takes an object, clones it and resets the original object: +今、関数`cloneAndReset`を書きたいとします。それはオブジェクトを受け取り、それをクローンしオリジナルのオブジェクトをリセットします。 ``` def cloneAndReset(obj: ?): Cloneable = { @@ -38,17 +39,16 @@ def cloneAndReset(obj: ?): Cloneable = { } ``` -The question arises what the type of the parameter `obj` is. If it's `Cloneable` then the object can be `clone`d, but not `reset`; if it's `Resetable` we can `reset` it, but there is no `clone` operation. To avoid type casts in such a situation, we can specify the type of `obj` to be both `Cloneable` and `Resetable`. This compound type is written like this in Scala: `Cloneable with Resetable`. +パラメータ`obj`の型は何かという疑問が上がります。もし`Cloneable`であれば、オブジェクトを`clone`することができますが、`reset`することはできません。もし`Resetable`であれば、`reset`することができますが、`clone`の操作はできません。そのような状態で型キャストを回避するために`obj`の型を`Cloneable`と`Resetable`の両方であると明示することができます。Scalaではこの複合型は`Cloneable with Resetable`のように書くことができます。 -Here's the updated function: +こちらが書き変えた関数です。 ``` def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { //... } ``` +複合型はいくつかのオブジェクトから成り、それらは既存のオブジェクトメンバーの署名を絞り込むための一つの改良点です。 +一般的な形は`A with B with C ... { refinement }`です。 -Compound types can consist of several object types and they may have a single refinement which can be used to narrow the signature of existing object members. -The general form is: `A with B with C ... { refinement }` - -An example for the use of refinements is given on the page about [class composition with mixins](mixin-class-composition.html). +改良点の使い方の例は[ミックスインを用いたクラス合成](mixin-class-composition.html)のページにあります。 From 29b241df615086d3748e67b7996b2ac0bf03f210 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 10 Apr 2019 04:31:33 +0900 Subject: [PATCH 132/358] Japanese translation of TOUR OF SCALA self-types --- _ja/tour/self-types.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index f54e62ae18..baa0671555 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -1,6 +1,7 @@ --- layout: tour -title: Self-type +title: 自己型 +language: ja discourse: true @@ -14,27 +15,28 @@ prerequisite-knowledge: nested-classes, mixin-class-composition redirect_from: "/tutorials/tour/self-types.html" --- -Self-types are a way to declare that a trait must be mixed into another trait, even though it doesn't directly extend it. That makes the members of the dependency available without imports. +自己型はたとえ直接継承していなくても、トレイトが他のトレイトにミックスインさせることを宣言する方法です。 +それはimportなしに、依存関係のメンバーを利用可能とします。 -A self-type is a way to narrow the type of `this` or another identifier that aliases `this`. The syntax looks like normal function syntax but means something entirely different. +自己型は`this`の型、または`this`の別名となる別の識別子を絞り込む方法です。 +その構文は普通の関数構文のように見えますが、全く異なる意味があります。 -To use a self-type in a trait, write an identifier, the type of another trait to mix in, and a `=>` (e.g. `someIdentifier: SomeOtherTrait =>`). +トレイトで自己型を使うために、識別子とミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 ```tut trait User { def username: String } trait Tweeter { - this: User => // reassign this + this: User => // thisが再割り当てされます def tweet(tweetText: String) = println(s"$username: $tweetText") } -class VerifiedTweeter(val username_ : String) extends Tweeter with User { // We mixin User because Tweeter required it +class VerifiedTweeter(val username_ : String) extends Tweeter with User { // TweeterがUserを必要とするためミックスインします。 def username = s"real $username_" } val realBeyoncé = new VerifiedTweeter("Beyoncé") -realBeyoncé.tweet("Just spilled my glass of lemonade") // prints "real Beyoncé: Just spilled my glass of lemonade" +realBeyoncé.tweet("Just spilled my glass of lemonade") // "real Beyoncé: Just spilled my glass of lemonade"とプリントします。 ``` - -Because we said `this: User =>` in `trait Tweeter`, now the variable `username` is in scope for the `tweet` method. This also means that since `VerifiedTweeter` extends `Tweeter`, it must also mix-in `User` (using `with User`). +`trait Tweeter`の中で`this: User =>`と記述したので、今は変数`username`は`tweet`メソッドのスコープ内にあります。これは更に`VerifiedTweeter`は`Tweeter`を継承し、(`with User`を使って)`User`もミックスインしなければならいことを意味します。 From 771ef82bbc7665bab3bcdf85797648def918fd9d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 10 Apr 2019 18:37:28 +0900 Subject: [PATCH 133/358] Japanese translation of TOUR OF SCALA implicit-parameters --- _ja/tour/implicit-parameters.md | 37 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 43befb04e7..f4501923ab 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -1,6 +1,7 @@ --- layout: tour -title: Implicit Parameters +title: 暗黙のパラメータ +language: ja discourse: true @@ -13,16 +14,17 @@ previous-page: self-types redirect_from: "/tutorials/tour/implicit-parameters.html" --- -A method can have an _implicit_ parameter list, marked by the _implicit_ keyword at the start of the parameter list. If the parameters in that parameter list are not passed as usual, Scala will look if it can get an implicit value of the correct type, and if it can, pass it automatically. +メソッドは _暗黙の_ パラメータのリストを持つことができ、パラメータリストの先頭には_implicit_キーワードで印をつけます。 +もしそのパラメータリストの中のパラメータがいつものように渡らなければ、Scalaは正しい型の暗黙値を受け取ることができるかを確認し、可能であれば自動的に渡します。 -The places Scala will look for these parameters fall into two categories: +Scalaがこれらのパラメータを探す場合は2つのカテゴリに分かれます。 -* Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called. -* Then it looks for members marked implicit in all the companion objects associated with the implicit candidate type. +* Scalaは暗黙のパラメータブロックを持つメソッドが呼び出された時点で、まず(プレフィックスなしに)に直接アクセスできる暗黙の定義と暗黙のパラメータを探します。 +* そして全てのコンパニオンオブジェクトの中でimplicitで印をつけられ、暗黙に候補となる型と関連づけられたメンバーを探します。 -A more detailed guide to where scala looks for implicits can be found in [the FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html) +より詳しいScalaがimplicitを探すガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 -In the following example we define a method `sum` which computes the sum of a list of elements using the monoid's `add` and `unit` operations. Please note that implicit values can not be top-level. +以下の例では、モノイドの`add`と`unit`の演算を使い、要素のリストの合計を計算するメソッド`sum`を定義しています。 ```tut abstract class Monoid[A] { @@ -46,27 +48,26 @@ object ImplicitTest { else m.add(xs.head, sum(xs.tail)) def main(args: Array[String]): Unit = { - println(sum(List(1, 2, 3))) // uses intMonoid implicitly - println(sum(List("a", "b", "c"))) // uses stringMonoid implicitly + println(sum(List(1, 2, 3))) // intMonoidを暗に使用 + println(sum(List("a", "b", "c"))) // stringMonoidを暗に使用 } } ``` +`モノイド`はここでは`add`と呼ばれる演算子を定義します。演算子は`A`のペアを組み合わせ、別の`A`を返します。そしてそれは(特別な)`A`を作ることができる`unit`と呼ばれる演算子です。 -`Monoid` defines an operation called `add` here, that combines a pair of `A`s and returns another `A`, together with an operation called `unit` that is able to create some (specific) `A`. +どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトは暗黙に使われうることを指し示します。 -To show how implicit parameters work, we first define monoids `stringMonoid` and `intMonoid` for strings and integers, respectively. The `implicit` keyword indicates that the corresponding object can be used implicitly. +メソッド`sum`は`List[A]`を受け取り、`A`を返します。そしてメソッドは初期値`A`を`unit`から受け取り、リスト中でそれぞれ次の`A`を`add`メソッドで組み合わせます。ここでパラメータ`m`をimplicitにすることは、そのメソッドを呼び出すとき、暗黙のパラメータ`m`を使うためにScalaが暗黙の`Monoid[A]`を見つけることができるなら`xs`パラメータを提供するだけで良いことを意味します。 -The method `sum` takes a `List[A]` and returns an `A`, which takes the initial `A` from `unit`, and combines each next `A` in the list to that with the `add` method. Making the parameter `m` implicit here means we only have to provide the `xs` parameter when we call the method if Scala can find a an implict `Monoid[A]` to use for the implicit `m` parameter. +`main`メソッドにて`sum`を2回呼び出すと、`xs`パラメータだけが提供されます。そうなるとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`List[Int]`を`xs`を渡します。それは `A`が`Int`であることを意味します。`m`と暗黙のパラメータリストは省略しており、Scalaは`Monoid[Int]`型のimplictを探します。最初のルックアップのルールには以下のように書いています。 -In our `main` method we call `sum` twice, and only provide the `xs` parameter. Scala will now look for an implicit in the scope mentioned above. The first call to `sum` passes a `List[Int]` for `xs`, which means that `A` is `Int`. The implicit parameter list with `m` is left out, so Scala will look for an implicit of type `Monoid[Int]`. The first lookup rule reads +> Scalaは暗黙のパラメータブロックを持つメソッドが呼び出された時点で、まず(プレフィックスなしに)に直接アクセスできる暗黙の定義と暗黙のパラメータを探します。 -> Scala will first look for implicit definitions and implicit parameters that can be accessed directly (without a prefix) at the point the method with the implicit parameter block is called. +`intMonoid`は`main`の中で直接アクセスされる暗黙の定義です。それ正しい型であり、`sum`メソッドに自動的に渡されます。 -`intMonoid` is an implicit definition that can be accessed directly in `main`. It is also of the correct type, so it's passed to the `sum` method automatically. +`sum`の2回目の呼び出しは`List[String]`を渡します。それは`A`は`String`であることを意味します。暗黙のルックアップは`Int`の時と同様に動きますが、この時は `stringMonoid`を見つけ、`m`として自動的に渡します。 -The second call to `sum` passes a `List[String]`, which means that `A` is `String`. Implicit lookup will go the same way as with `Int`, but will this time find `stringMonoid`, and passes that automatically as `m`. - -The program will output +そのプログラムは以下を出力します。 ``` 6 abc From 87acfc93c84fce42d568611bbd5aa186ab3edffb Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:41:06 +0900 Subject: [PATCH 134/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 5638395caa..2b915e2f45 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -14,7 +14,7 @@ previous-page: generic-classes redirect_from: "/tutorials/tour/variances.html" --- -変位指定は複合型のサブタイプ関係とコンポーネント型のサブタイプ関係の相関です。 +変位指定は複合型の間の継承関係とそれらの型パラメータ間の継承関係の相関です。 Scalaは型パラメータの[ジェネリッククラス](generic-classes.html)の変位指定アノテーションをサポートしています。 そのためアノテーションを使わなかった場合に、共変、反変、または不変を利用することができます。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 From df8fa67224d863172ff3fe1af0e4e9707f7afed5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:41:45 +0900 Subject: [PATCH 135/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 2b915e2f45..f1917381d5 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/variances.html" --- 変位指定は複合型の間の継承関係とそれらの型パラメータ間の継承関係の相関です。 -Scalaは型パラメータの[ジェネリッククラス](generic-classes.html)の変位指定アノテーションをサポートしています。 +Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータの変位指定アノテーションをサポートしています。 そのためアノテーションを使わなかった場合に、共変、反変、または不変を利用することができます。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 その一方で変位指定が無ければ抽象性の再利用が制限されます。 From 58f10f6a281ae02a797a7f62cd84085e801d638d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:45:50 +0900 Subject: [PATCH 136/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index f1917381d5..55b88e0498 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/variances.html" 変位指定は複合型の間の継承関係とそれらの型パラメータ間の継承関係の相関です。 Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータの変位指定アノテーションをサポートしています。 -そのためアノテーションを使わなかった場合に、共変、反変、または不変を利用することができます。 +変位指定アノテーションにより共変、反変にでき、アノテーション無しなら不変になります。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 その一方で変位指定が無ければ抽象性の再利用が制限されます。 From 629f34dd9d2fd3064b0bce5af4a165840a316d9b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:47:40 +0900 Subject: [PATCH 137/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 55b88e0498..6da1fff1f6 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/variances.html" Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータの変位指定アノテーションをサポートしています。 変位指定アノテーションにより共変、反変にでき、アノテーション無しなら不変になります。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 -その一方で変位指定が無ければ抽象性の再利用が制限されます。 +その一方で変位指定が無いと、クラスを抽象化して再利用しにくくなります。 ```tut From 7e930ab2fe22a536bf60b7d2698bbfa58cbeb20c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:52:17 +0900 Subject: [PATCH 138/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 6da1fff1f6..1140667ed7 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -29,7 +29,7 @@ class Baz[A] // 不変クラス ### 共変 -ジェネリッククラスの型パラメータ`A`はアノテーション`+A`を使うことで共変にすることができます。 +ジェネリッククラスの型パラメータ`A`はアノテーション`+A`を使うと共変になります。 ある`class List[+A]`がある時`A`を共変にすることは、2つの型`A`と`B`に対して`A`が`B`のサブタイプであることをほのめかします。 その時、`List[A]`は`List[B]`のサブタイプとなります。 これによりジェネリックを利用したとても便利で直感的なサブタイプの関係を作ることができます。 From 8e0db255038da2ffdaa41d25698ac953e0086e32 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:53:11 +0900 Subject: [PATCH 139/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 1140667ed7..7e37b6206f 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -162,7 +162,7 @@ val cat: Cat = catContainer.getValue // おっと、犬に猫に割り当てて 変位指定を理解することを助けるもう一つの例はScalaの標準ライブラリの`trait Function1[-T, +R]`です。 `Function1`は1つのパラメータを持つ関数を表します。1つ目の型パラメータ`T`はパラメータの型を表します。 そして2つ目の型パラメータ`R`は戻り値の型を表します。 -`Function1`はその引数の型を通して反変であり、戻り値の型を通して共変です。 +`Function1`はその引数の型に対して反変であり、戻り値の型に対して共変です。 この例では`Function1[A, B]`を表現するために`A => B`という文字での表記をします。 先ほど利用されたものと同様に`Cat`, `Dog`, `Animal`の継承ツリーは以下のものを考えてください。 From 2f87c853ccf7cf77abe3e0e4ac4368b5f4b6f970 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:54:46 +0900 Subject: [PATCH 140/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 7e37b6206f..904b026d3f 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -172,7 +172,7 @@ abstract class SmallAnimal extends Animal case class Mouse(name: String) extends SmallAnimal ``` -動物の種類を受け取り、それらが食べる食料の種類を返す関数を使うとした場合。 +動物の種類を受け取り、それらが食べる食料の種類を返す関数について考えましょう。 仮に(猫は小動物を食べるので)`Cat => SmallAnimal`が欲しいとします。 しかし`Animal => Mouse`を代わりに与えられたとしても、私たちのプログラムはまだ動きます。 From 47b5645d8f9e2832c2785dc35079bbbbcea27c12 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 14:56:17 +0900 Subject: [PATCH 141/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 904b026d3f..5d2ca522e7 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -184,5 +184,5 @@ case class Mouse(name: String) extends SmallAnimal ### 他の言語との比較 変位指定はScalaに似たいくつかの言語によって他の方法でサポートされます。 -例えば、クラスの抽象化が定義(declaration-site variance)された時にアノテーションが追加されるという点で、Scalaの変位指定アノテーションはC#のそれと非常に似ています。 +例えば、Scalaの変位指定アノテーションはC#のそれと非常に似ています。C#ではクラスの抽象性を定義する時にアノテーションが追加されます。(宣言時の変位指定) しかしながら、Javaでは、クラスの抽象化が使われた時(use-site variance)に変位指定アノテーションはクライアントにより与えられます。 From bef6518a8c6414ed2753a2c3db947fa43ea80403 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 15:01:21 +0900 Subject: [PATCH 142/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 5d2ca522e7..10c08a5654 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -185,4 +185,4 @@ case class Mouse(name: String) extends SmallAnimal 変位指定はScalaに似たいくつかの言語によって他の方法でサポートされます。 例えば、Scalaの変位指定アノテーションはC#のそれと非常に似ています。C#ではクラスの抽象性を定義する時にアノテーションが追加されます。(宣言時の変位指定) -しかしながら、Javaでは、クラスの抽象化が使われた時(use-site variance)に変位指定アノテーションはクライアントにより与えられます。 +しかしながら、Javaでは、クラスの抽象性を使う時に変位指定アノテーションが利用側のコードから与えられます。(使用時の変位指定) From fd61bbf6188a8377ae1e24f05d428da5b4929ebb Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Thu, 11 Apr 2019 15:04:06 +0900 Subject: [PATCH 143/358] Update _ja/tour/variances.md Co-Authored-By: take2webservice --- _ja/tour/variances.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 10c08a5654..4f6570d3b5 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -165,7 +165,7 @@ val cat: Cat = catContainer.getValue // おっと、犬に猫に割り当てて `Function1`はその引数の型に対して反変であり、戻り値の型に対して共変です。 この例では`Function1[A, B]`を表現するために`A => B`という文字での表記をします。 -先ほど利用されたものと同様に`Cat`, `Dog`, `Animal`の継承ツリーは以下のものを考えてください。 +先ほど利用された`Cat`, `Dog`, `Animal`の継承ツリーに、以下のものを加えましょう: ```tut abstract class SmallAnimal extends Animal From d82514f9d859296ebc9bfa4e50b45160dda80329 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 11 Apr 2019 16:52:04 +0900 Subject: [PATCH 144/358] Japanese translation of TOUR OF SCALA implicit-conversions --- _ja/tour/implicit-conversions.md | 36 ++++++++++++++++---------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index be7f2edf6f..da2e0c7dac 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -1,6 +1,7 @@ --- layout: tour -title: Implicit Conversions +title: 暗黙の変換 +language: ja discourse: true @@ -13,23 +14,23 @@ previous-page: implicit-parameters redirect_from: "/tutorials/tour/implicit-conversions.html" --- -An implicit conversion from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type. +型`S`から型`T`への暗黙の変換はimplicitな値や、implicitなメソッドにより定義されます。 +implicitな値は`S => T`な関数を持ち、implicitなメソッドはその型の値に変換できます。 -Implicit conversions are applied in two situations: +暗黙の変換は2つの状況で適用されます。 -* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`. -* In a selection `e.m` with `e` of type `S`, if the selector `m` does not denote a member of `S`. +* もし式`e`が型`S`であり、`S`は式の期待する型`T`に準拠しない場合 +* 型`S`の`e`が持つセレクション`e.m`において、セレクター`m`が`S`のメンバーではない場合 -In the first case, a conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`. -In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`. +最初のケースでは変換`c`を検索します。`c`は`e`に適用可能で、その結果の型が`T`に準拠します。 +2つ目のケースでは変換`c`は検索します。`c`は`e`に適用可能で、その結果は`m`と名付けられたメンバー含みます。 -If an implicit method `List[A] => Ordered[List[A]]` is in scope, as well as an implicit method `Int => Ordered[Int]`, the following operation on the two lists of type `List[Int]` is legal: +implicitなメソッド`List[A] => Ordered[List[A]]`がスコープの中にあれば、implicitなメソッド`Int => Ordered[Int]`と同様に、`List[Int]`型の2つのリストにおける以下の処理は正当なものになります。 ``` List(1, 2, 3) <= List(4, 5) ``` - -An implicit method `Int => Ordered[Int]` is provided automatically through `scala.Predef.intWrapper`. An example of an implicit method `List[A] => Ordered[List[A]]` is provided below. +implicitなメソッド`Int => Ordered[Int]`は`scala.Predef.intWrapper`を通じて自動的に提供されます。implicitなメソッドの例`List[A] => Ordered[List[A]]`は以下にあります。 ```tut import scala.language.implicitConversions @@ -41,10 +42,9 @@ implicit def list2ordered[A](x: List[A]) def compare(that: List[A]): Int = 1 } ``` +implicitなインポートされたオブジェクト`scala.Predef`は度々使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)にいくつかの別名を宣言しますが、暗黙のうちにいくつかの変換もします。 -The implicitly imported object `scala.Predef` declares several aliases to frequently used types (e.g. `scala.collection.immutable.Map` is aliased to `Map`) and methods (e.g. `assert`) but also several implicit conversions. - -For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions: +例えば、`java.lang.Integer`を期待してJavaのメソッドを呼び出す時、自由に`scala.Int`を代わりに渡すことができます。それはPredefオブジェクトが以下の暗黙の変換をを含んでいるからです。 ```tut import scala.language.implicitConversions @@ -53,11 +53,11 @@ implicit def int2Integer(x: Int) = java.lang.Integer.valueOf(x) ``` -Because implicit conversions can have pitfalls if used indiscriminately the compiler warns when compiling the implicit conversion definition. +暗黙の変換は落とし穴になり得るため、無差別に使えば、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 -To turn off the warnings take either of these actions: +警告をオフにするには、次のいずれかの操作を行います。 -* Import `scala.language.implicitConversions` into the scope of the implicit conversion definition -* Invoke the compiler with `-language:implicitConversions` +* 暗黙の変換定義のスコープに`scala.language.implicitConversions`をインポートする。 +* コンパイラを`-language:implicitConversions`をつけて起動する -No warning is emitted when the conversion is applied by the compiler. +コンパイラにより変換が適用された時、警告は出ません。 From f34c92fd4c373834f5fb45b7decd2d93ae9f3d36 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 11 Apr 2019 17:24:51 +0900 Subject: [PATCH 145/358] Japanese translation of TOUR OF SCALA polymorphic-methods --- _ja/tour/polymorphic-methods.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 8f9cb4525f..8bdbe3ea18 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -1,6 +1,6 @@ --- layout: tour -title: Polymorphic Methods +title: ポリモフィズムメソッド discourse: true @@ -15,9 +15,10 @@ prerequisite-knowledge: unified-types redirect_from: "/tutorials/tour/polymorphic-methods.html" --- -Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses. +Scalaのメソッドは値と同様に型によってパラメータ化することができます。構文はジェネリッククラスの構文と同じです。 +型パラメータは角カッコで囲まれますが、値パラメータは丸括弧で囲まれます。 -Here is an example: +こちらが例になります。 ```tut def listOfDuplicates[A](x: A, length: Int): List[A] = { @@ -30,8 +31,8 @@ println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3) println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) ``` -The method `listOfDuplicates` takes a type parameter `A` and value parameters `x` and `length`. Value `x` is of type `A`. If `length < 1` we return an empty list. Otherwise we prepend `x` to the the list of duplicates returned by the recursive call. (Note that `::` means prepend an element on the left to a list on the right.) +メソッド`listOfDuplicates`は型パラメータ`A`と値パラメータ`x`と`length`を受け取ります。値`x`は型`A`となります。もし`length < 1`なら空のリストを返します。一方で`x`を再帰呼び出しで返された複写リストの先頭に追加します。(`::`は右側のリストの先頭への左側の要素の追加を意味します。) -In first example call, we explicitly provide the type parameter by writing `[Int]`. Therefore the first argument must be an `Int` and the return type will be `List[Int]`. +最初の呼び出し例のでは、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`[Int]`でなければならず、戻される型は`List[Int]`となります。 -The second example call shows that you don't always need to explicitly provide the type parameter. The compiler can often infer it based on context or on the types of the value arguments. In this example, `"La"` is a `String` so the compiler knows `A` must be `String`. +2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことが判ります。コンパイラはしばしばコンテキストか値引数の型に基づき、型パラメータを推論できます。この例では`"La"`は`String`であり、コンパイラは`A`が`String`でなければならないと気づきます。 From 659a5b0f4732a8e61047d0782c00efa975648118 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 11 Apr 2019 21:55:21 +0900 Subject: [PATCH 146/358] Japanese translation of TOUR OF SCALA type-inference --- _ja/tour/type-inference.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index d949fc4f4b..666e74e2cc 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -1,6 +1,7 @@ --- layout: tour -title: Type Inference +title: 型インターフェイス +language: ja discourse: true @@ -11,29 +12,29 @@ next-page: operators previous-page: polymorphic-methods --- -The Scala compiler can often infer the type of an expression so you don't have to declare it explicitly. +Scalaコンパイラはしばしば式の型推論するので、明示的に型を宣言する必要はありません。 -## Omitting the type +## 型の省略 ```tut val businessName = "Montreux Jazz Café" ``` -The compiler can detect that `businessName` is a String. It works similarly with methods: +コンパイラは`businessName`がStringだと検知できます。これはメソッドでも同様に動きます。 ```tut def squareOf(x: Int) = x * x ``` -The compiler can infer that the return type is an `Int`, so no explicit return type is required. +コンパイラは戻り値の型が`Int`だと推論できるので、明示的な戻り値の型は必要ありません。 -For recursive methods, the compiler is not able to infer a result type. Here is a program which will fail the compiler for this reason: +再帰的メソッドでは、コンパイラは結果の型を推論できません。こちらはこの理由でコンパイラが失敗するプログラムです。 ```tut:fail def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1) ``` -It is also not compulsory to specify type parameters when [polymorphic methods](polymorphic-methods.html) are called or [generic classes](generic-classes.html) are instantiated. The Scala compiler will infer such missing type parameters from the context and from the types of the actual method/constructor parameters. +以下のような場合は型パラメータを指定することは強制されません。それは[ポリモーフフィックメソッド](polymorphic-methods.html)が呼ばれる時や[ジェネリッククラス](generic-classes.html) がインスタンス化される時です。Scalaコンパイラはそのようなコンテキスト、メソッドやコンストラクタのパラメータ型に型パラメータが実際になければ推論します。 -Here are two examples: +こちらは2つの例です。 ```tut case class MyPair[A, B](x: A, y: B); @@ -42,33 +43,32 @@ val p = MyPair(1, "scala") // type: MyPair[Int, String] def id[T](x: T) = x val q = id(1) // type: Int ``` +コンパイラは型`A`と`B`が何であるかを見つけ出すために`MyPair`の引数の型を使用します。`x`の型も同様です。 -The compiler uses the types of the arguments of `MyPair` to figure out what type `A` and `B` are. Likewise for the type of `x`. +## パラメータ -## Parameters - -The compiler never infers method parameter types. However, in certain cases, it can infer anonymous function parameter types when the function is passed as argument. +コンパイラはメソッドのパラメータを決して推論しません。しかしながら、確かなケースとして、無名関数のパラメータ型を推論できます。それは関数に引数が渡された場合です。 ```tut Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) ``` -The parameter for map is `f: A => B`. Because we put integers in the `Seq`, the compiler knows that `A` is `Int` (i.e. that `x` is an integer). Therefore, the compiler can infer from `x * 2` that `B` is type `Int`. +mapのパラメータは`f: A => B`です。`Seq`に整数値を渡すため、コンパイラは`A`が`Int`だと気づきます(つまり、あの`x`は整数です)。そのためコンパイラは`x * 2`から`B`は型`Int`であると推論できます。 -## When _not_ to rely on type inference +## 型推論に頼ら*ない*時 -It is generally considered more readable to declare the type of members exposed in a public API. Therefore, we recommended that you make the type explicit for any APIs that will be exposed to users of your code. +一般的にパブリックなAPIではメンバーの型宣言を公開するとより読みやすいと考えられます。そのため、コードをユーザーに公開するAPIならば型を明白にすることお勧めします。 -Also, type inference can sometimes infer a too-specific type. Suppose we write: +また、型推論は特定の型を推論することがあります。次のように書いたとします。 ```tut var obj = null ``` -We can't then go on and make this reassignment: +これ以上進められず、再割り当てができません。 ```tut:fail obj = new AnyRef ``` -It won't compile, because the type inferred for `obj` was `Null`. Since the only value of that type is `null`, it is impossible to assign a different value. +こちらはコンパイルできません。`obj`の型推論は`Null`だからです。その型の唯一の値が`null`だから、他の値を割り当てられません。 From 9f9b78dc7f397edd18b7f331db9fcdb170fd0e0a Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 11 Apr 2019 22:33:17 +0900 Subject: [PATCH 147/358] Japanese translation of TOUR OF SCALA operators --- _ja/tour/operators.md | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index b3ad662813..90eb4b8ab3 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -1,6 +1,7 @@ --- layout: tour -title: Operators +title: オペレータ +language: ja discourse: true @@ -13,18 +14,21 @@ prerequisite-knowledge: case-classes redirect_from: "/tutorials/tour/operators.html" --- -In Scala, operators are methods. Any method with a single parameter can be used as an _infix operator_. For example, `+` can be called with dot-notation: +Scalaではオペレーターはメソッドです。1つのパラメータを持つメソッドであれば*中置オペレータ*として使えます。例えば、`+`はドット記法で呼び出せます。 + ``` 10.+(1) ``` -However, it's easier to read as an infix operator: +しかしながら、中置オペレータの方が読みやすいです。 + ``` 10 + 1 ``` -## Defining and using operators -You can use any legal identifier as an operator. This includes a name like `add` or a symbol(s) like `+`. +## オペレータの定義方法と使い方 + +有効な識別子であればオペレータとして使用できます。これは `add`のような名前と`+`のようなシンボルも含みます。 ```tut case class Vec(val x: Double, val y: Double) { def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y) @@ -37,7 +41,8 @@ val vector3 = vector1 + vector2 vector3.x // 3.0 vector3.y // 3.0 ``` -The class Vec has a method `+` which we used to add `vector1` and `vector2`. Using parentheses, you can build up complex expressions with readable syntax. Here is the definition of class `MyBool` which includes methods `and` and `or`: +クラスVecはメソッド`+`を持ち、 `vector1`と`vector2`を足しわせるのに使います。丸括弧を使えば、読みやすい構文の複雑な式を作れます。 +こちらはクラス`MyBool`の定義です。クラス`MyBool`はメソッド`and`と`or`を含みます。 ```tut case class MyBool(x: Boolean) { @@ -47,19 +52,20 @@ case class MyBool(x: Boolean) { } ``` -It is now possible to use `and` and `or` as infix operators: +この時、`and`と`or`を中置オペレータとして使えます。 ```tut def not(x: MyBool) = x.negate def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y) ``` -This helps to make the definition of `xor` more readable. +これにより`xor`の定義をより読みやすくします。 + +## 優先順位 -## Precedence -When an expression uses multiple operators, the operators are evaluated based on the priority of the first character: +式が複数のオペレータを使う時、最初の記号の優先度に基づきオペレータは評価されます。 ``` -(characters not shown below) +(以下に表示されていない記号) * / % + - : @@ -68,14 +74,14 @@ When an expression uses multiple operators, the operators are evaluated based on & ^ | -(all letters) +(全ての文字) ``` -This applies to functions you define. For example, the following expression: +これはあなたが定義した関数にも適用できます。 ``` a + b ^? c ?^ d less a ==> b | c ``` -Is equivalent to +は以下と同じ意味です。 ``` ((a + b) ^? (c ?^ d)) less ((a ==> b) | c) ``` -`?^` has the highest precedence because it starts with the character `?`. `+` has the second highest precedence, followed by `==>`, `^?`, `|`, and `less`. +`?^`は最も高い優先順位を持ちます。`?^`は`?`から始まるからです。`+`は二番目に高い優先順位を持ち、その後に`==>`、 `^?`、 `|`、 そして`less`が続きます。 From 5fe632f0d24fda8654323ec154f8ca9316ca343b Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Thu, 11 Apr 2019 23:26:29 +0900 Subject: [PATCH 148/358] Japanese translation of TOUR OF SCALA by-name-parameters --- _ja/tour/by-name-parameters.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index b9648a21f4..73d1e18a98 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -1,6 +1,6 @@ --- layout: tour -title: By-name Parameters +title: 名前付きパラメータ discourse: true @@ -13,13 +13,14 @@ previous-page: operators redirect_from: "/tutorials/tour/by-name-parameters.html" --- -_By-name parameters_ are only evaluated when used. They are in contrast to _by-value parameters_. To make a parameter called by-name, simply prepend `=>` to its type. +*名前付きパラメータ*は使用された時に評価されます。それらは*値つきパラメータ*とは対照的です。名前で呼び出せるパラメータを作るには、単純に`=>`を型の前につけます。 ```tut def calculate(input: => Int) = input * 37 ``` -By-name parameters have the advantage that they are not evaluated if they aren't used in the function body. On the other hand, by-value parameters have the advantage that they are evaluated only once. -Here's an example of how we could implement a while loop: +名前付きパラメータの利点は関数本体の中で使わなければ評価されない点です。一方で、値付きパラメータの利点は1度だけ評価される点です。 + +こちらはwhileループをどのように実装するかの例です。 ```tut def whileLoop(condition: => Boolean)(body: => Unit): Unit = @@ -35,8 +36,9 @@ whileLoop (i > 0) { i -= 1 } // prints 2 1 ``` -The method `whileLoop` uses multiple parameter lists to take a condition and a body of the loop. If the `condition` is true, the `body` is executed and then a recursive call to whileLoop is made. If the `condition` is false, the body is never evaluated because we prepended `=>` to the type of `body`. -Now when we pass `i > 0` as our `condition` and `println(i); i-= 1` as the `body`, it behaves like the standard while loop in many languages. +メソッド`whileLoop`は条件とループの本体を受け取るために複数パラメータリストを使います。もし`condition`がtrueならば、`body`は実行され、そしてwhileループの再帰呼び出しがされます。`condition`がfalseならば、bodyは決して評価されません。それは`body`の型の前に`=>`をつけたからです。 + +ここで、`condition`に`i > 0`、`body`に`println(i); i-= 1`を渡した場合、多くの言語では一般的なwhileループと同じ振る舞いをします。 -This ability to delay evaluation of a parameter until it is used can help performance if the parameter is computationally intensive to evaluate or a longer-running block of code such as fetching a URL. +パラメータが使われるまで評価を遅延させる機能はパフォーマンスを助けます。それはパラメータを評価するための計算が集約的な場合やURLの取得のような時間がかかるコードブロックの場合です。 From b8a6c8d4a3880fee642b18deebf6d383c1414809 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 00:56:39 +0900 Subject: [PATCH 149/358] Japanese translation of TOUR OF SCALA annotations --- _ja/tour/annotations.md | 46 +++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index fe44a8c3f0..130ee0b03a 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -1,6 +1,7 @@ --- layout: tour -title: Annotations +title: アノテーション +language: ja discourse: true @@ -13,7 +14,7 @@ previous-page: by-name-parameters redirect_from: "/tutorials/tour/annotations.html" --- -Annotations associate meta-information with definitions. For example, the annotation `@deprecated` before a method causes the compiler to print a warning if the method is used. +アノテーションはメタ情報と定義を関連づけます。例えば、メソッドの前のアノテーション`@deprecated`はメソッドが使われたらコンパイラに警告を出力させます。 ``` object DeprecationDemo extends App { @deprecated("deprecation message", "release # which deprecates method") @@ -22,13 +23,13 @@ object DeprecationDemo extends App { hello } ``` -This will compile but the compiler will print a warning: "there was one deprecation warning". +これはコンパイルされますが、コンパイラは警告"there was one deprecation warning"を出力します。 -An annotation clause applies to the first definition or declaration following it. More than one annotation clause may precede a definition and declaration. The order in which these clauses are given does not matter. +アノテーション句はそれに続くに最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。与えられたこれらの句の中での順番は重要ではありません。 -## Annotations that ensure correctness of encodings -Certain annotations will actually cause compilation to fail if a condition(s) is not met. For example, the annotation `@tailrec` ensures that a method is [tail-recursive](https://en.wikipedia.org/wiki/Tail_call). Tail-recursion can keep memory requirements constant. Here's how it's used in a method which calculates the factorial: +## エンコーディングの正確性を保証するアノテーション +確かにいくつかのアノテーションは条件が一致すればコンパイルを失敗させます。例えば、アノテーション`@tailrec`はメソッドは[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰は必須メモリを一定に維持します。こちらは階乗を計算するメソッドの中での使われ方です。 ```tut import scala.annotation.tailrec @@ -41,7 +42,7 @@ def factorial(x: Int): Int = { factorialHelper(x, 1) } ``` -The `factorialHelper` method has the `@tailrec` which ensures the method is indeed tail-recursive. If we were to change the implementation of `factorialHelper` to the following, it would fail: +`factorialHelper`メソッドは`@tailrec`を持ちます。`@tailrec`はメソッドが実際に末尾再帰であることを保証します。もし`factorialHelper`の実装を以下のように変更すれば、失敗し ``` import scala.annotation.tailrec @@ -53,17 +54,18 @@ def factorial(x: Int): Int = { factorialHelper(x) } ``` -We would get the message "Recursive call not in tail position". +"Recursive call not in tail position"というメッセージを受け取ります。 -## Annotations affecting code generation -Some annotations like `@inline` affect the generated code (i.e. your jar file might have different bytes than if you hadn't used the annotation). Inlining means inserting the code in a method's body at the call site. The resulting bytecode is longer, but hopefully runs faster. Using the annotation `@inline` does not ensure that a method will be inlined, but it will cause the compiler to do it if and only if some heuristics about the size of the generated code are met. +## コード生成に影響するアノテーション +`@inline`のようなアノテーションは生成されたコードに影響します。(つまり、アノテーションを使わなかった場合とでjarファイルのバイト数が異なる場合があります。)インライン化は呼び出し時にメソッドの本体へのコード挿入を意味します。結果のバイトコードはより長くなりますが、上手くいけば実行が早くなります。アノテーション`@inline`を使ってもメソッドのインライン化はされません。しかし、生成されたコードのサイズに関するヒューリスティックスが満たされた場合に限りコンパイラにインライン化をさせます。 -### Java Annotations ### -When writing Scala code which interoperates with Java, there are a few differences in annotation syntax to note. -**Note:** Make sure you use the `-target:jvm-1.8` option with Java annotations. +### Javaのアノテーション ### +Javaと相互運用するScalaのコードを書いている時、記述するアノテーション構文は少し違います。 -Java has user-defined metadata in the form of [annotations](https://docs.oracle.com/javase/tutorial/java/annotations/). A key feature of annotations is that they rely on specifying name-value pairs to initialize their elements. For instance, if we need an annotation to track the source of some class we might define it as +**注:** Javaアノテーションを使う場合、`-target:jvm-1.8`オプションを使ってください。 + +Javaには[アノテーション](https://docs.oracle.com/javase/tutorial/java/annotations/)の形をしたユーザー定義メタデータがあります。アノテーションの主な機能は要所の初期化にのために名前と値のペアを指定する必要があります。例えば、あるクラスのソースを追いかけるためにアノテーションが必要な場合、以下のようにそれを定義します。 ``` @interface Source { @@ -72,7 +74,7 @@ Java has user-defined metadata in the form of [annotations](https://docs.oracle. } ``` -And then apply it as follows +そして、それは以下のように適用されます。 ``` @Source(URL = "http://coders.com/", @@ -80,7 +82,7 @@ And then apply it as follows public class MyClass extends TheirClass ... ``` -An annotation application in Scala looks like a constructor invocation, for instantiating a Java annotation one has to use named arguments: +Scalaでのアノテーションの適用はコンストラクタの呼び出しと似ています。Javaのアノテーションをインスタンス化するためには名前付き引数を使う必要があります。 ``` @Source(URL = "http://coders.com/", @@ -88,7 +90,7 @@ An annotation application in Scala looks like a constructor invocation, for inst class MyScalaClass ... ``` -This syntax is quite tedious if the annotation contains only one element (without default value) so, by convention, if the name is specified as `value` it can be applied in Java using a constructor-like syntax: +アノテーションが(デフォルト値を除き)要素を1つだけ含む場合、この構文はかなり退屈です。そのため慣例により、名前が`value`と指定されていれば、コンストラクタっぽい構文でJavaに適用できます。 ``` @interface SourceURL { @@ -97,21 +99,21 @@ This syntax is quite tedious if the annotation contains only one element (withou } ``` -And then apply it as follows +そして以下のように適用します。 ``` @SourceURL("http://coders.com/") public class MyClass extends TheirClass ... ``` -In this case, Scala provides the same possibility +この場合、Scalaは同じ可能性を提供します。 ``` -@SourceURL("http://coders.com/") +@SourceURL("http://coders.com/")1 class MyScalaClass ... ``` -The `mail` element was specified with a default value so we need not explicitly provide a value for it. However, if we need to do it we can not mix-and-match the two styles in Java: +`mail`要素はデフォルト値で指定されます。そのためはっきりとそれに値を与える必要がありません。しかしながら、もしそれをする必要があるなら、Javaでは2つのスタイルを混ぜて組み合わせることはできません。 ``` @SourceURL(value = "http://coders.com/", @@ -119,7 +121,7 @@ The `mail` element was specified with a default value so we need not explicitly public class MyClass extends TheirClass ... ``` -Scala provides more flexibility in this respect +Scalaはこの点においてより多くの柔軟性を提供します。 ``` @SourceURL("http://coders.com/", From d5f8c1141f7f6e1e751fb72c0b91ffe2bc188bdc Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 01:44:18 +0900 Subject: [PATCH 150/358] Japanese translation of TOUR OF SCALA default-parameter-values --- _ja/tour/default-parameter-values.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md index 20f701e03d..487fb1ae1e 100644 --- a/_ja/tour/default-parameter-values.md +++ b/_ja/tour/default-parameter-values.md @@ -1,6 +1,7 @@ --- layout: tour -title: Default Parameter Values +title: デフォルト引数 +language: ja discourse: true @@ -14,7 +15,7 @@ prerequisite-knowledge: named-arguments, function syntax redirect_from: "/tutorials/tour/default-parameter-values.html" --- -Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters. +Scalaはパラメータのデフォルト値を与えることができ、呼び出し者はこれらのパラメータを省略できます。 ```tut def log(message: String, level: String = "INFO") = println(s"$level: $message") @@ -23,16 +24,16 @@ log("System starting") // prints INFO: System starting log("User not found", "WARNING") // prints WARNING: User not found ``` -The parameter `level` has a default value so it is optional. On the last line, the argument `"WARNING"` overrides the default argument `"INFO"`. Where you might do overloaded methods in Java, you can use methods with optional parameters to achieve the same effect. However, if the caller omits an argument, any following arguments must be named. +パラメータ`level`はデフォルト値を持ちつので、オプショナルです。最終行では、引数`"WARNING"`はデフォルト値`"INFO"`を上書きます。Javaでオーバーロードされたメソッドを実行する場合、同じ効果を得るためオプショナルなパラメーターでメソッドを使えます。しかしながら呼び出し者が引数を省略すれば、全ての後ろに続く引数に名前が必要です。 ```tut class Point(val x: Double = 0, val y: Double = 0) val point1 = new Point(y = 1) ``` -Here we have to say `y = 1`. +ここで、`y = 1`と明示しなければなりません。 -Note that default parameters in Scala are not optional when called from Java code: +Javaのコードから呼び出された時、Sclaにおけるデフォルトパラメータはオプショナルではありません。 ```tut // Point.scala @@ -43,7 +44,7 @@ class Point(val x: Double = 0, val y: Double = 0) // Main.java public class Main { public static void main(String[] args) { - Point point = new Point(1); // does not compile + Point point = new Point(1); // コンパイルされません } } ``` From a139296728d48afaadd43f6371a4329b385cd435 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 03:20:28 +0900 Subject: [PATCH 151/358] Japanese translation of TOUR OF SCALA named-arguments --- _ja/tour/named-arguments.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md index d8d1d7a88e..86b2a075a9 100644 --- a/_ja/tour/named-arguments.md +++ b/_ja/tour/named-arguments.md @@ -1,6 +1,6 @@ --- layout: tour -title: Named Arguments +title: 名前付き引数 discourse: true @@ -14,6 +14,7 @@ prerequisite-knowledge: function-syntax redirect_from: "/tutorials/tour/named-arguments.html" --- +メソッドを呼ぶ時、以下のように引数にパラメータ名でラベル付が可能です。 When calling methods, you can label the arguments with their parameter names like so: ```tut @@ -25,10 +26,11 @@ printName("John", "Smith") // Prints "John Smith" printName(first = "John", last = "Smith") // Prints "John Smith" printName(last = "Smith", first = "John") // Prints "John Smith" ``` -Notice how the order of named arguments can be rearranged. However, if some arguments are named and others are not, the unnamed arguments must come first and in the order of their parameters in the method signature. + +名前付き引数の順序はどのように並び替えられるかに気をつけましょう。ただし、ある引数は名前をつけられ、他の引数には名前がつけられなかった場合、名前が付いていない引数は最初かつメソッドの署名の中にあるパラメーター並び順でなければなりません。 ```tut:fail printName(last = "Smith", "john") // error: positional after named argument ``` -Note that named arguments do not work with calls to Java methods. +名前付き引数はJavaメソッドを呼び出す時は動きません。 From 6ba235d4d80495729b370aa63f505a558c2cee6a Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 04:02:51 +0900 Subject: [PATCH 152/358] Japanese translation of TOUR OF SCALA packages-and-imports --- _ja/tour/packages-and-imports.md | 42 +++++++++++++++++--------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index 68efdd8083..04416a6964 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -1,6 +1,7 @@ --- layout: tour -title: Packages and Imports +title: パッケージとインポート +language: ja discourse: true @@ -11,18 +12,19 @@ previous-page: named-arguments next-page: package-objects --- -# Packages and Imports -Scala uses packages to create namespaces which allow you to modularize programs. +# パッケージとインポート +Scalaは名前空間を作るためにパッケージを使います。名前空間によりプログラムをモジュール化できます。 -## Creating a package -Packages are created by declaring one or more package names at the top of a Scala file. +## パッケージの作成 +Scalaファイルの先頭で1つ以上のパッケージ名を宣言することでパッケージは作られます。 ``` package users class User ``` -One convention is to name the package the same as the directory containing the Scala file. However, Scala is agnostic to file layout. The directory structure of an sbt project for `package users` might look like this: +パッケージとScalaファイルが含まれるディレクトリは同じ名前をつける習慣があります。しかし、Scalaはファイルのレイアウトを認知できません。`package users`のsbtプロジェクトのディレクトリ構成はこのように見えます。 + ``` - ExampleProject - build.sbt @@ -36,7 +38,8 @@ One convention is to name the package the same as the directory containing the S UserPreferences.scala - test ``` -Notice how the `users` directory is within the `scala` directory and how there are multiple Scala files within the package. Each Scala file in the package could have the same package declaration. The other way to declare packages is by using braces: +`users`ディレクトリがどのように`scala`ディレクトリの中にあり、Scalaファイルがどのようにパッケージ内にあるのかに注意してください。パッケージ内のいずれのScalaファイルも同じパッケージ宣言がされます。他のパッケージ宣言の方法は波括弧を使います。 + ``` package users { package administrators { @@ -47,26 +50,25 @@ package users { } } ``` -As you can see, this allows for package nesting and provides greater control for scope and encapsulation. +見ての通り、この方法はパッケージのネストができ、より大きいスコープとカプセル化のコントロールができます。 -The package name should be all lower case and if the code is being developed within an organization which has a website, it should be the following format convention: `..`. For example, if Google had a project called `SelfDrivingCar`, the package name would look like this: +パッケージ名は全て小文字で書き、もしコードがwebサイトを持つ組織によって開発される場合、慣習として次のフォーマットであるべきです。`<トップレベルドメイン>.<ドメイン名>.<プロジェクト名>`。例えば、Googleが`SelfDrivingCar`と呼ばれるプロジェクトを持っている場合、パッケージ名はこんな風になるでしょう。 ``` package com.google.selfdrivingcar.camera class Lens ``` -This could correspond to the following directory structure: `SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala`. +これは次のディレクトリ構成に対応します。`SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala` -## Imports -`import` clauses are for accessing members (classes, traits, functions, etc.) in other packages. An `import` clause is not required for accessing members of the same package. Import clauses are selective: +## インポート +`import`句は他パッケージのメンバー(クラス、トレイト、関数など)にアクセスするためのものです。同じパッケージのメンバーにアクセスするには`import`句は必要ありません。import句は選択的です。 ``` -import users._ // import everything from the users package -import users.User // import the class User -import users.{User, UserPreferences} // Only imports selected members -import users.{UserPreferences => UPrefs} // import and rename for convenience +import users._ // usersパッケージから全てをインポートする +import users.User // クラスUserをインポートする +import users.{User, UserPreferences} // 選択されたメンバーのみインポートする +import users.{UserPreferences => UPrefs} // インポートし利便性のために名前を変更する ``` - -One way in which Scala is different from Java is that imports can be used anywhere: +ScalaのJavaと異なる利用方法の1つはインポートがどこでも使える点です。 ```tut def sqrtplus1(x: Int) = { @@ -74,7 +76,7 @@ def sqrtplus1(x: Int) = { sqrt(x) + 1.0 } ``` -In the event there is a naming conflict and you need to import something from the root of the project, prefix the package name with `_root_`: +名前競合があり、プロジェクトのルートから何かをインポートする必要がある時、パッケージ名の前に`_root_`をつけます。 ``` package accounts @@ -82,4 +84,4 @@ import _root_.users._ ``` -Note: The `scala` and `java.lang` packages as well as `object Predef` are imported by default. +注:`scala`と`java.lang` パッケージは`object Predef`と同じように標準でインポートされています。 From c3a4c3cc891e1f1d891a95659f65ddd498615050 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 16:13:08 +0900 Subject: [PATCH 153/358] Japanese translation of TOUR OF SCALA package-objects --- _ja/tour/package-objects.md | 43 +++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index 628a56d3a0..db3b6edd80 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -1,6 +1,7 @@ --- layout: tour -title: Package Objects +title: パッケージオブジェクト +language: ja discourse: true @@ -10,25 +11,22 @@ num: 36 previous-page: packages-and-imports --- -# Package objects +# パッケージオブジェクト -Scala provides package objects as a convenient container shared across an entire package. +Scalaはパッケージ全体を通して共有される便利なコンテナとしてパッケージオブジェクトを提供します。 -Package objects -can contain arbitrary definitions, not just variable and method definitions. For instance, they are frequently -used to hold package-wide type aliases and implicit conversions. Package objects can even inherit -Scala classes and traits. +パッケージオブジェクトは任意の定義を含むことができます。それは変数だけではなくメソッド定義も含みます。 +例えば、それらはパッケージ全体の型エイリアスと暗黙の変換を保有するために頻繁に使われます。 +パッケージオブジェクトはScalaクラスとトレイトさえ受け継げます。 -By convention, the source code for a package object is usually put in a source file named `package.scala`. +習慣として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 -Each package is allowed to have one package object. Any definitions placed in a package object are considered -members of the package itself. +いずれのパッケージもパッケージオブジェクトを1つ持てます。パッケージオブジェクト内の全ての定義はパッケージのメンバーであるか検討されます。 -See example below. Assume first a class `Fruit` and three `Fruit` objects in a package -`gardening.fruits`: +以下の例を見てみましょう。まず1つのクラス`Fruit`と3つの`Fruit`オブジェクトがパッケージ`gardening.fruits`にあるとします。 ``` -// in file gardening/fruits/Fruit.scala +// ファイル gardening/fruits/Fruit.scala の中 package gardening.fruits case class Fruit(name: String, color: String) @@ -37,11 +35,11 @@ object Plum extends Fruit("Plum", "blue") object Banana extends Fruit("Banana", "yellow") ``` -Now assume you want to place a variable `planted` and a method `showFruit` directly into package `gardening.fruits`. -Here's how this is done: +ここで、変数`planted`とメソッド`showFruit`を直接パッケージ`gardening.fruits`内に置きたいとします。 +こちらがその方法になります。 ``` -// in file gardening/fruits/package.scala +// ファイル gardening/fruits/package.scala の中 package gardening package object fruits { val planted = List(Apple, Plum, Banana) @@ -51,11 +49,11 @@ package object fruits { } ``` -As an example of how the use site looks, the following object `PrintPlanted` imports `planted` and `showFruit` in exactly the same -way it imports class `Fruit`, using a wildcard import on package gardening.fruits: +利用方法で見た例の通り、以下のオブジェクト`PrintPlanted`は`planted`と`showFruit`をインポートします。 +それはをクラス`Fruit`をインポートするのと全く同じ方法で、パッケージgardening.fruitsでワイルドカードインポートしています。 ``` -// in file PrintPlanted.scala +// ファイル PrintPlanted.scala の中 import gardening.fruits._ object PrintPlanted { def main(args: Array[String]): Unit = { @@ -66,12 +64,11 @@ object PrintPlanted { } ``` -Package objects are like other objects, which means you can use inheritance for building them. For example, one might mix in a couple of traits: +パッケージオブジェクトは他のオブジェクトと似ています。それはビルドのために継承を利用できるという意味です。例えば、一対のトレイトの中でパッケージオブジェクトをミックスします。 ``` package object fruits extends FruitAliases with FruitHelpers { - // helpers and variables follows here + // ヘルパーと変数がここに続きます。 } ``` - -Note that method overloading doesn't work in package objects. +メソッドオーバーライドはパッケージオブジェクト内では動作しないので気をつけましょう。 From c0ce5aed2ec6c2ba14bc01aeaf6f8d31ed4826cd Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 16:38:38 +0900 Subject: [PATCH 154/358] adapt changes on aa641d51873480d9560e310afdcfb133fe1d4558 --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index b3ed67ded7..5d7b4f7643 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -42,7 +42,7 @@ Scalaの[ケースクラス](case-classes.html\\) とその組み込みは多く この際、For内包表記はクエリの定式化に役立ちます。これらの機能によりWebサービスのようなアプリケーション開発の際、Scalaを理想的なものとなります。 ## Scalaは静的型付け ## -Scalaは抽象化が安全で首尾一貫した方法で使われることを静的に強制する表現型システムを備えています。 +Scalaは抽象化が安全で首尾一貫した方法で使われることをコンパイル時に強制する表現型システムを備えています。 特にその型システムは以下をサポートします: * [ジェネリッククラス](generic-classes.html\) From 996b6f15c06de5502e9443fa0e140f03516839ac Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 16:44:39 +0900 Subject: [PATCH 155/358] remove Object extending App from example on japanese file adapt changes on dde4a8d32f0996f5e4c67bfc84f98f710f0905e4 --- _ja/tour/mixin-class-composition.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 0d84947ea2..8ec544930e 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -75,11 +75,9 @@ trait RichIterator extends AbsIterator { `StringIterator`と`RichIterator`の機能を1つのクラスに組み合わせてみましょう。 ```tut -object StringIteratorTest extends App { - class RichStringIter extends StringIterator("Scala") with RichIterator - val richStringIter = new RichStringIter - richStringIter foreach println -} +class RichStringIter extends StringIterator("Scala") with RichIterator +val richStringIter = new RichStringIter +richStringIter foreach println ``` 新しいクラス`RichStringIter`は`StringIterator`をスーパークラスとし、`RichIterator`をミックスインとしています。 From 41178f792c5abd811bfc7b433d7f7b51d8c06dd1 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 17:19:06 +0900 Subject: [PATCH 156/358] fix grammatical mistake: 'they are instances of' to 'each is an instance of' in Japanese file adapt a commit 48ace49f3a068a84453bf4fbfd5090b1a340dd6b --- _ja/tour/unified-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md index e6898e4581..e6edc4e377 100644 --- a/_ja/tour/unified-types.md +++ b/_ja/tour/unified-types.md @@ -50,7 +50,7 @@ list.foreach(element => println(element)) ``` これは`List[Any]`型の`list`という値を定義します。 -このlistは様々な型の要素で初期化されています。しかしそれらは全て `scala.Any` のインスタンスなのでlistに追加することができています。 +このlistは様々な型の要素で初期化されています。しかしそれぞれの要素は `scala.Any` のインスタンスなのでlistに追加することができています。 こちらは先程のプログラムの出力です。 From 2778b594f5df43962b4e77b405f43a96daf1118e Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 12 Apr 2019 17:57:25 +0900 Subject: [PATCH 157/358] adapt latest changes on tuples document of tour of scala. this commit contains changes till 2990b1788e1733388dc52b2179dd641396f6a7ba --- _ja/tour/tuples.md | 74 +++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 7bed992a60..591177f0ba 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -15,85 +15,65 @@ topics: tuples redirect_from: "/tutorials/tour/tuples.html" --- -Scalaではタプルは異なる型の要素を複数持つことができるクラスです。 -タプルは不変です。 +Scalaではタプルは決まった数の要素を含む値であり、それぞれの要素は明確な型を持ちます。 +タプルは不変です -タプルは関数から複数の値を返す際に役立ちます。 +タプルはメソッドから複数の値を返す際に特に役立ちます。 -タプルは以下のように作ることができます +2つの要素を持つタプルは以下のように作ることができます。 ```tut -val ingredient = ("Sugar" , 25):Tuple2[String, Int] +val ingredient = ("Sugar" , 25) ``` -ここではString要素を1つとInt要素を1つ含むタプルを作っています。 +ここでは`String`要素を1つと`Int`要素を1つ含むタプルを作っています。 -Scalaではタプルは Tuple2, Tuple3, … Tuple22 までの一連のクラス群です。 -そのためn個の要素(nは2から22)でタプルを作成する時、 +`ingredient`の推論型は`(String, Int)`であり、これは`Tuple2[String, Int]`の簡単な表記法です。 -Scalaは基本的にそれらのクラスの中から構成要素の型に対応するクラスを1つインスタンス化します。 -例えば、 値 ingredient は Tuple2 [String, Int]型です。 +タプルを表すためには、Scalaは`Tuple2`, `Tuple3`,から `Tuple22`までのクラス群を使います。 +それぞれのクラスは要素の数と同じ数の型パラメータを持ちます。 ## 要素へのアクセス -タプル要素にはアンダースコア記法を用いてアクセスします。 -(要素がたくさんあることを考えると)'tuple._n' はn番目の要素を与えます。 +タプル要素へのアクセス方法の1つは位置の利用です。 +個々の要素は`_1`、`_2`などと名付けられます。 ```tut println(ingredient._1) // Sugar - println(ingredient._2) // 25 ``` - -## タプルのデータの分割代入 - -Scalaのタプルは分割代入にも対応しています。 +## タプルでのパターンマッチング +タプルはパターンマッチングを使って分解することもできます。 ```tut val (name, quantity) = ingredient - println(name) // Sugar - println(quantity) // 25 ``` -タプルの分割代入はパターンマッチングでも使われます。 +ここでは`name`の型推論は`String`で、`quantity`の型推論は`Int`です。 + +こちらはタプルのパターンマッチングの他の例です。 ```tut -val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3)) - -planetDistanceFromSun.foreach{ tuple => { - - tuple match { - - case ("Mercury", distance) => println(s"Mercury is $distance millions km far from Sun") - - case p if(p._1 == "Venus") => println(s"Venus is ${p._2} millions km far from Sun") - - case p if(p._1 == "Earth") => println(s"Blue planet is ${p._2} millions km far from Sun") - - case _ => println("Too far....") - - } - - } - +val planets = + List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6), + ("Mars", 227.9), ("Jupiter", 778.3)) +planets.foreach{ + case ("Earth", distance) => + println(s"Our planet is $distance million kilometers from the sun") + case _ => } ``` -また、for内包表記においては、 +または、`for`の中では ```tut val numPairs = List((2, 5), (3, -7), (20, 56)) - for ((a, b) <- numPairs) { - println(a * b) - } ``` -Unit型の値 () は概念的には Tuple0 型の () の値と同じです。 -要素が無いため、この型の値は1つだけしかありません。 - -ユーザーは時々タプルとケースクラスを選ぶのが難しいと感じる時があるかもしれません。 -原則として、要素に意味をもたせる場合にはケースクラスが好まれます。 +## タプルとケースクラス +ユーザーは時々、タプルとケースクラスの選択を難しいと思うかもしれません。ケースクラスには名前付き要素があります。その名前はある種のコードの可読性改善します。 +上記の惑星の例ではタプルを使うのではなく、`case class Planet(name: String, distance: Double)`の定義もできます。 From a031f681f9e79ee570b0623658b1466cbfc4d9c6 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:08:55 +0900 Subject: [PATCH 158/358] Update _ja/tour/upper-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/upper-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/upper-type-bounds.md b/_ja/tour/upper-type-bounds.md index f159a363ea..e5e9db1501 100644 --- a/_ja/tour/upper-type-bounds.md +++ b/_ja/tour/upper-type-bounds.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/upper-type-bounds.html" Scalaでは [型パラメータ](generic-classes.html)と[抽象型メンバー](abstract-type-members.html) は型境界により制約されることがあります。 型境界は具体的な値を制限し、そのような型のメンバーに関するより多くの情報を明らかにするかもしれません。 -_上限型境界_ は型変数`T`が型`A`のサブタイプを参照することを宣言します。 +_上限型境界_ `T <: A` は型変数`T`が型`A`のサブタイプであることを宣言します。 こちらはクラス`PetContainer`の型パラメータの上限型境界を実演する例です。 From a7f79cffccd01bb81ca73b288fc9f8b215c28d36 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:10:23 +0900 Subject: [PATCH 159/358] Update _ja/tour/upper-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/upper-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/upper-type-bounds.md b/_ja/tour/upper-type-bounds.md index e5e9db1501..8ff0a29e8a 100644 --- a/_ja/tour/upper-type-bounds.md +++ b/_ja/tour/upper-type-bounds.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/upper-type-bounds.html" --- Scalaでは [型パラメータ](generic-classes.html)と[抽象型メンバー](abstract-type-members.html) は型境界により制約されることがあります。 -型境界は具体的な値を制限し、そのような型のメンバーに関するより多くの情報を明らかにするかもしれません。 +型境界は型変数に入れられる値を制限して、時にはそれらの型についてより多くの情報を与えます。 _上限型境界_ `T <: A` は型変数`T`が型`A`のサブタイプであることを宣言します。 こちらはクラス`PetContainer`の型パラメータの上限型境界を実演する例です。 From a1e055fda500a8c8c7e01b57cc13465165c28394 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:21:53 +0900 Subject: [PATCH 160/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index a12365974e..2edfc2daae 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -36,7 +36,7 @@ case class Nil[+B]() extends Node[B] { ``` このプログラムは片方向リストを実装します。`Nil`は空の要素(すなわち空のリスト)を意味します。 -`class ListNode`は型`B` (`head`)の要素を含むノードであり、リストの残りの部分(`tail`)への参照です。 +`class ListNode`は型`B` (`head`)の要素と、リストの残りの部分(`tail`)への参照を持つノードです。 `class Node`とそのスーパータイプは共変であるので、`+B`としています。 しかしながら、このプログラムはコンパイル _されません_。`prepend`の中のパラメータ`elem`が *共* 変と宣言した型`B`だからです。 From c6b838ba5ea65a2a43d232bf21fd88df997e6dc1 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:24:18 +0900 Subject: [PATCH 161/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 2edfc2daae..0fa13c5476 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -37,7 +37,7 @@ case class Nil[+B]() extends Node[B] { このプログラムは片方向リストを実装します。`Nil`は空の要素(すなわち空のリスト)を意味します。 `class ListNode`は型`B` (`head`)の要素と、リストの残りの部分(`tail`)への参照を持つノードです。 -`class Node`とそのスーパータイプは共変であるので、`+B`としています。 +`class Node`とそのサブタイプは、`+B`とあるので、共変です。 しかしながら、このプログラムはコンパイル _されません_。`prepend`の中のパラメータ`elem`が *共* 変と宣言した型`B`だからです。 これは動きません、なぜなら関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 From 9b0730e24e87b9968ba854d512e84cf7e86eb2a5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:26:40 +0900 Subject: [PATCH 162/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 0fa13c5476..b6342bb375 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -39,7 +39,7 @@ case class Nil[+B]() extends Node[B] { `class ListNode`は型`B` (`head`)の要素と、リストの残りの部分(`tail`)への参照を持つノードです。 `class Node`とそのサブタイプは、`+B`とあるので、共変です。 -しかしながら、このプログラムはコンパイル _されません_。`prepend`の中のパラメータ`elem`が *共* 変と宣言した型`B`だからです。 +しかしながら、このプログラムはコンパイル _されません_。`prepend`のパラメータ`elem`が、宣言時に*共* 変と宣言した型`B`になっているからです。 これは動きません、なぜなら関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 これを解決するためには、`prepend`の中でパラメータ`elem`の型の変位指定を弾く必要があります。 From 01ac2cae7ee680171ee4db11d893cb6a3b9acfe5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:28:04 +0900 Subject: [PATCH 163/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index b6342bb375..29f910bdbb 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -40,7 +40,7 @@ case class Nil[+B]() extends Node[B] { `class Node`とそのサブタイプは、`+B`とあるので、共変です。 しかしながら、このプログラムはコンパイル _されません_。`prepend`のパラメータ`elem`が、宣言時に*共* 変と宣言した型`B`になっているからです。 -これは動きません、なぜなら関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 +なぜ通らないかというと、関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 これを解決するためには、`prepend`の中でパラメータ`elem`の型の変位指定を弾く必要があります。 これを実現するには、下限型境界として`B`を持つ新しい型パラメータ`U`を導入します。 From 3dab31d82632f724486ef17f419d206c2aea74dc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:29:35 +0900 Subject: [PATCH 164/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 29f910bdbb..b095c0936f 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -42,7 +42,7 @@ case class Nil[+B]() extends Node[B] { しかしながら、このプログラムはコンパイル _されません_。`prepend`のパラメータ`elem`が、宣言時に*共* 変と宣言した型`B`になっているからです。 なぜ通らないかというと、関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 -これを解決するためには、`prepend`の中でパラメータ`elem`の型の変位指定を弾く必要があります。 +これを解決するためには、`prepend`のパラメータ`elem`の型の変位指定を逆転させる必要があります。 これを実現するには、下限型境界として`B`を持つ新しい型パラメータ`U`を導入します。 ```tut From cc3ec57ef9f2865f12733ecdbf03f395d8e81277 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:29:44 +0900 Subject: [PATCH 165/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index b095c0936f..5e68833e13 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -61,7 +61,7 @@ case class Nil[+B]() extends Node[B] { } ``` -今、以下のようなことができます。 +すると、以下のようなことができます。 ```tut trait Bird case class AfricanSwallow() extends Bird From f397bda8171efcbc194a611963e2fb7506ba8380 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:30:50 +0900 Subject: [PATCH 166/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 5e68833e13..3cfa234a6d 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -72,5 +72,5 @@ val africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil()) val birdList: Node[Bird] = africanSwallowList birdList.prepend(new EuropeanSwallow) ``` -`Node[Bird]`は`africanSwallowList`アサインできますが、その際`EuropeanSwallow`を受け取ります。 +`Node[Bird]`は`africanSwallowList`をアサインできますが、その後`EuropeanSwallow`を受け入れられます。 From 7118dd4df86d6c2fb5479b247ff18cd53df00994 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 14 Apr 2019 22:31:21 +0900 Subject: [PATCH 167/358] Update _ja/tour/lower-type-bounds.md Co-Authored-By: take2webservice --- _ja/tour/lower-type-bounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 3cfa234a6d..8fee86a2c8 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -15,7 +15,7 @@ prerequisite-knowledge: upper-type-bounds, generics, variance redirect_from: "/tutorials/tour/lower-type-bounds.html" --- - [上限型境界](upper-type-bounds.html) が型を別の型のサブタイプに制限する一方で、*下限型境界*は型が別の型のスーパータイプになるよう宣言します。表現`B >: A`はパラメータ`B`または抽象型`B`が型`A`のスーパータイプを指すことを表します。ほとんどのケースで`A`はそのクラスの型パラメータであり、`B`はメソッドの型パラメータになります。 + [上限型境界](upper-type-bounds.html) が型を別の型のサブタイプに制限する一方で、*下限型境界*は型が別の型のスーパータイプになるよう宣言します。表現`B >: A`はパラメータ`B`または抽象型`B`が型`A`のスーパータイプであることを表します。ほとんどのケースで`A`はそのクラスの型パラメータであり、`B`はメソッドの型パラメータになります。 以下はこれが役立つ場合の例です。 From 98308783b5f0b4d63c1623e58e873265ee92282a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:49:34 +0900 Subject: [PATCH 168/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index 3b97ffb15b..fb892b22b1 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/abstract-types.html" redirect_from: "/tour/abstract-types.html" --- -トレイトと抽象クラスのような抽象型は順番に抽象型メンバーを持つことができます。 +トレイトや抽象クラスのような抽象型は抽象型メンバーを持つことができます。 これは具体的な実装が明確に型を定義することを意味します。 こちらが例になります。 From 5dcd08436c4f3f0b99ca46379090bed5e3676417 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:50:42 +0900 Subject: [PATCH 169/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index fb892b22b1..c10e74e50e 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -17,7 +17,7 @@ redirect_from: "/tour/abstract-types.html" --- トレイトや抽象クラスのような抽象型は抽象型メンバーを持つことができます。 -これは具体的な実装が明確に型を定義することを意味します。 +これは具体的な実装で実際の型を定義するという意味です。 こちらが例になります。 ```tut From 76a9e8e4b40015c4bcec762ae975f25eb499d7ee Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:51:12 +0900 Subject: [PATCH 170/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index c10e74e50e..18682afb2a 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -18,7 +18,7 @@ redirect_from: "/tour/abstract-types.html" トレイトや抽象クラスのような抽象型は抽象型メンバーを持つことができます。 これは具体的な実装で実際の型を定義するという意味です。 -こちらが例になります。 +こちらが例です。 ```tut trait Buffer { From 3853e93c274182084ba90a553c494ae0f0aed799 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:51:52 +0900 Subject: [PATCH 171/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index 18682afb2a..4750a21eb3 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -35,7 +35,7 @@ abstract class SeqBuffer extends Buffer { def length = element.length } ``` -`T`の上限型境界の仕様において、更に別の抽象型`U`の使い方に気をつけてください。 +`T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。 この`class SeqBuffer`はバッファーの中のシーケンスのみを保存することができます。それは新しい抽象型`U`により型`T`は`Seq[U]`のサブタイプである必要があることを明言しているからです。 From 7389bead5d0c6a5fea20dcde4e3c7f55348be2c1 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:53:20 +0900 Subject: [PATCH 172/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index 4750a21eb3..a31aed7e6f 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -37,7 +37,7 @@ abstract class SeqBuffer extends Buffer { ``` `T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。 -この`class SeqBuffer`はバッファーの中のシーケンスのみを保存することができます。それは新しい抽象型`U`により型`T`は`Seq[U]`のサブタイプである必要があることを明言しているからです。 +この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプである必要があると明示しているからです。 抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使います。 これを説明するために、今から整数のリストを参照するシーケンスバッファーを扱うプログラムを見てみます。 From 9b47e8a89572762cbab359bb379fd5c00b385acc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:55:42 +0900 Subject: [PATCH 173/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index a31aed7e6f..ff6fcd0676 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -57,7 +57,7 @@ val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) println("content = " + buf.element) ``` -こちらでは抽象型`T`を具体的な型`List[Int]`に設定するために、ファクトリー`newIntSeqBuf`は`IntSeqBuf`(つまり`new IntSeqBuffer`)の無名クラスの実装を使います。 +ここで、ファクトリー`newIntSeqBuf`は抽象型`T`を具体的な型`List[Int]`に設定するために、`IntSeqBuf`(つまり`new IntSeqBuffer`)を無名クラスで実装します。 これにより抽象型メンバーをクラスの型パラメータに変換したり、その逆も可能となります。以下は上記コードの型パラメータのみを使うバージョンです。 From 29cff3cb4123c871d6f2d9be9a3edbd85ec90ffb Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:56:30 +0900 Subject: [PATCH 174/358] Update _ja/tour/abstract-type-members.md Co-Authored-By: take2webservice --- _ja/tour/abstract-type-members.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index ff6fcd0676..2265487d97 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -59,7 +59,7 @@ println("content = " + buf.element) ``` ここで、ファクトリー`newIntSeqBuf`は抽象型`T`を具体的な型`List[Int]`に設定するために、`IntSeqBuf`(つまり`new IntSeqBuffer`)を無名クラスで実装します。 -これにより抽象型メンバーをクラスの型パラメータに変換したり、その逆も可能となります。以下は上記コードの型パラメータのみを使うバージョンです。 +抽象型メンバーをクラスの型パラメータに変えることも、その逆も可能です。以下は上記コードの型パラメータのみを使うバージョンです。 ```tut abstract class Buffer[+T] { From fca0364727a5506168167dc850b609599813f103 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 01:59:38 +0900 Subject: [PATCH 175/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 4fd114a072..1c5bb7e102 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/inner-classes.html" --- Scalaではクラスが他のクラスをメンバーとして保持することが可能です。 -Javaのような言語とは対照的に、内部クラスは外側のクラスのメンバーとなる場合、Scalaではそのような内部クラスは外側のオブジェクトに束縛されます。 +Javaのような、内部クラスが外側のクラスのメンバーとなる言語とは対照的に、Scalaでは、そのような内部クラスは外側のオブジェクトに束縛されます。 コンパイルの際、コンパイラにどのノードがなんのグラフに属しているのか混同することを防いでほしいのです。 パス依存型はその解決策の1つです。 From d291349972b18d39d09007deb2dfa156580b4197 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:01:42 +0900 Subject: [PATCH 176/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 1c5bb7e102..2b55761237 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/inner-classes.html" Scalaではクラスが他のクラスをメンバーとして保持することが可能です。 Javaのような、内部クラスが外側のクラスのメンバーとなる言語とは対照的に、Scalaでは、そのような内部クラスは外側のオブジェクトに束縛されます。 -コンパイルの際、コンパイラにどのノードがなんのグラフに属しているのか混同することを防いでほしいのです。 +どのノードがなんのグラフに属しているのかを私達が混同しないように、コンパイラがコンパイル時に防いでほしいのです。 パス依存型はその解決策の1つです。 その違いを図示するために、グラフデータ型の実装をさっと書きます。 From 2c9f72a490c412e7e94e84c10d700bb7575ba4e5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:02:29 +0900 Subject: [PATCH 177/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 2b55761237..c7fcfc93b1 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -19,7 +19,7 @@ Javaのような、内部クラスが外側のクラスのメンバーとなる どのノードがなんのグラフに属しているのかを私達が混同しないように、コンパイラがコンパイル時に防いでほしいのです。 パス依存型はその解決策の1つです。 -その違いを図示するために、グラフデータ型の実装をさっと書きます。 +その違いを示すために、グラフデータ型の実装をさっと書きます。 ```tut class Graph { From 482d90e1ab98663cc25cc8a4d3305d0a8258801c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:05:18 +0900 Subject: [PATCH 178/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index c7fcfc93b1..4025a507d9 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -50,7 +50,7 @@ val node3: graph1.Node = graph1.newNode node1.connectTo(node2) node3.connectTo(node1) ``` -`node1`、`node2`、そして `node3`の型が`graph1.Node`であることを明確にするために、明示的に宣言しています。 +わかりやすくするため`node1`、`node2`、`node3`の型を`graph1.Node`と明示的に宣言しましたが、なくてもコンパイラは推論できます。 これは`graph1.newNode`を呼び出す時、`new Node`を呼び出し、そのメソッドがインスタンス`graph1`特有の`Node`のインスタンスを使用しているからです。 今2つのグラフがあれば、Scalaの型システムは1つのグラフの中でノードと別のグラフのノードを混ぜることを許しません。 From dde02fc212cbaf7265aaa87d196af7cf3ad6301f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:06:53 +0900 Subject: [PATCH 179/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 4025a507d9..641693d612 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -51,7 +51,7 @@ node1.connectTo(node2) node3.connectTo(node1) ``` わかりやすくするため`node1`、`node2`、`node3`の型を`graph1.Node`と明示的に宣言しましたが、なくてもコンパイラは推論できます。 -これは`graph1.newNode`を呼び出す時、`new Node`を呼び出し、そのメソッドがインスタンス`graph1`特有の`Node`のインスタンスを使用しているからです。 +これは`new Node`を呼んでいる`graph1.newNode`を呼び出す時、そのメソッドが`Node`のインスタンス`graph1`を使用しているからです。 今2つのグラフがあれば、Scalaの型システムは1つのグラフの中でノードと別のグラフのノードを混ぜることを許しません。 それは他のグラフのノードは別の型だからです。 From 23b2344944a840f08dab6ce2d18d1d4252ac1a5b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:07:54 +0900 Subject: [PATCH 180/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 641693d612..d309599baa 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -53,7 +53,7 @@ node3.connectTo(node1) わかりやすくするため`node1`、`node2`、`node3`の型を`graph1.Node`と明示的に宣言しましたが、なくてもコンパイラは推論できます。 これは`new Node`を呼んでいる`graph1.newNode`を呼び出す時、そのメソッドが`Node`のインスタンス`graph1`を使用しているからです。 -今2つのグラフがあれば、Scalaの型システムは1つのグラフの中でノードと別のグラフのノードを混ぜることを許しません。 +2つのグラフがあるとき、Scalaの型システムは1つのグラフの中で定義されたノードと別のグラフで定義されたノードを混ぜることを許しません。 それは他のグラフのノードは別の型だからです。 こちらは不正なプログラムです。 From f8de5bb302808b47690efc79f440ce901d06fbab Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:10:26 +0900 Subject: [PATCH 181/358] Update _ja/tour/inner-classes.md Co-Authored-By: take2webservice --- _ja/tour/inner-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index d309599baa..32e253b783 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -54,7 +54,7 @@ node3.connectTo(node1) これは`new Node`を呼んでいる`graph1.newNode`を呼び出す時、そのメソッドが`Node`のインスタンス`graph1`を使用しているからです。 2つのグラフがあるとき、Scalaの型システムは1つのグラフの中で定義されたノードと別のグラフで定義されたノードを混ぜることを許しません。 -それは他のグラフのノードは別の型だからです。 +それは別のグラフのノードは別の型を持つからです。 こちらは不正なプログラムです。 ``` From 141c7c69a86a9d50e5bc721b79c9afbd61fa1fde Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:12:26 +0900 Subject: [PATCH 182/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index fbbce00689..fbd15dd33c 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -13,7 +13,7 @@ previous-page: abstract-type-members redirect_from: "/tutorials/tour/compound-types.html" --- -オブジェクトの型がいくつかの他の型のサブタイプであることを表現する必要がある時があります。 +オブジェクトの型が複数の他の型のサブタイプであることを表現する必要がある時があります。 Scalaでは*複合型*の助けにより表現することができます。それはオブジェクト型の交点です。 2つのトレイト`Cloneable`と`Resetable`があると考えてください。 From 8c07f53d3bc31cf74e7fc8beb6e072a294870871 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:13:38 +0900 Subject: [PATCH 183/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index fbd15dd33c..0dc1dd4b61 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -14,7 +14,7 @@ previous-page: abstract-type-members redirect_from: "/tutorials/tour/compound-types.html" --- オブジェクトの型が複数の他の型のサブタイプであることを表現する必要がある時があります。 -Scalaでは*複合型*の助けにより表現することができます。それはオブジェクト型の交点です。 +Scalaでは*複合型*の助けにより表現できます。複合型とはオブジェクトの型同士を重ねることです。 2つのトレイト`Cloneable`と`Resetable`があると考えてください。 From 4c0dc3beca148bc9fe28b680a31c1d48df4c47b7 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:13:57 +0900 Subject: [PATCH 184/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 0dc1dd4b61..5ef77b6bc6 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/compound-types.html" オブジェクトの型が複数の他の型のサブタイプであることを表現する必要がある時があります。 Scalaでは*複合型*の助けにより表現できます。複合型とはオブジェクトの型同士を重ねることです。 -2つのトレイト`Cloneable`と`Resetable`があると考えてください。 +2つのトレイト`Cloneable`と`Resetable`があるとしましょう。 ```tut trait Cloneable extends java.lang.Cloneable { From 7d6a7ad163841bb7da6cddd77865dc0b3c3cc8c2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:14:12 +0900 Subject: [PATCH 185/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 5ef77b6bc6..6920797378 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -29,7 +29,7 @@ trait Resetable { } ``` -今、関数`cloneAndReset`を書きたいとします。それはオブジェクトを受け取り、それをクローンしオリジナルのオブジェクトをリセットします。 +今、関数`cloneAndReset`を書きたいとします。それはオブジェクトを受け取り、それをクローンして、元のオブジェクトをリセットします。 ``` def cloneAndReset(obj: ?): Cloneable = { From 3d29ea4830a55136ab4f6b67bdea80e644e61edf Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:14:56 +0900 Subject: [PATCH 186/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 6920797378..62c36fda3e 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -39,7 +39,7 @@ def cloneAndReset(obj: ?): Cloneable = { } ``` -パラメータ`obj`の型は何かという疑問が上がります。もし`Cloneable`であれば、オブジェクトを`clone`することができますが、`reset`することはできません。もし`Resetable`であれば、`reset`することができますが、`clone`の操作はできません。そのような状態で型キャストを回避するために`obj`の型を`Cloneable`と`Resetable`の両方であると明示することができます。Scalaではこの複合型は`Cloneable with Resetable`のように書くことができます。 +パラメータ`obj`の型は何かという疑問が上がります。もし`Cloneable`であれば、オブジェクトを`clone`することができますが、`reset`することはできません。もし`Resetable`であれば、`reset`することができますが、`clone`の操作はできません。そのような状態で型キャストを回避するために`obj`の型を`Cloneable`と`Resetable`の両方であると指定することができます。Scalaではこの複合型は`Cloneable with Resetable`のように書くことができます。 こちらが書き変えた関数です。 From 6481baf2664ca28365a95636033218eb9f9606e3 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:17:51 +0900 Subject: [PATCH 187/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 62c36fda3e..7b57a1c1ff 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -48,7 +48,7 @@ def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { //... } ``` -複合型はいくつかのオブジェクトから成り、それらは既存のオブジェクトメンバーの署名を絞り込むための一つの改良点です。 +複合型は複数のオブジェクトの型からなり、一つだけの細別型(refinement)を持てます。細別型は既存オブジェクトのメンバーのシグネチャを絞り込むのに使えます。 一般的な形は`A with B with C ... { refinement }`です。 改良点の使い方の例は[ミックスインを用いたクラス合成](mixin-class-composition.html)のページにあります。 From 4d97063af5185cd66a56761fefb5625ce0a4f631 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:18:28 +0900 Subject: [PATCH 188/358] Update _ja/tour/compound-types.md Co-Authored-By: take2webservice --- _ja/tour/compound-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 7b57a1c1ff..677871cd5d 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -51,4 +51,4 @@ def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { 複合型は複数のオブジェクトの型からなり、一つだけの細別型(refinement)を持てます。細別型は既存オブジェクトのメンバーのシグネチャを絞り込むのに使えます。 一般的な形は`A with B with C ... { refinement }`です。 -改良点の使い方の例は[ミックスインを用いたクラス合成](mixin-class-composition.html)のページにあります。 +細別の使い方の例は[ミックスインを用いたクラス合成](mixin-class-composition.html)のページにあります。 From 77a722c8a25b81badc71dfe2bc6d7bafab0949cf Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:21:08 +0900 Subject: [PATCH 189/358] Update _ja/tour/self-types.md Co-Authored-By: take2webservice --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index baa0671555..0528139a16 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -15,7 +15,7 @@ prerequisite-knowledge: nested-classes, mixin-class-composition redirect_from: "/tutorials/tour/self-types.html" --- -自己型はたとえ直接継承していなくても、トレイトが他のトレイトにミックスインさせることを宣言する方法です。 +自己型はたとえ直接継承していなくても、トレイトが他のトレイトにミックスインされていることを宣言する方法です。 それはimportなしに、依存関係のメンバーを利用可能とします。 自己型は`this`の型、または`this`の別名となる別の識別子を絞り込む方法です。 From e1cab5e4b3edd62e24dc4d7d61ae588eb8836229 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:24:10 +0900 Subject: [PATCH 190/358] Update _ja/tour/self-types.md Co-Authored-By: take2webservice --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index 0528139a16..c5989e71e8 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -16,7 +16,7 @@ prerequisite-knowledge: nested-classes, mixin-class-composition redirect_from: "/tutorials/tour/self-types.html" --- 自己型はたとえ直接継承していなくても、トレイトが他のトレイトにミックスインされていることを宣言する方法です。 -それはimportなしに、依存関係のメンバーを利用可能とします。 +それはimportなしに、依存先のメンバーを利用可能とします。 自己型は`this`の型、または`this`の別名となる別の識別子を絞り込む方法です。 その構文は普通の関数構文のように見えますが、全く異なる意味があります。 From 9da0361f3fda2b2da69fe756a0ab5123b3b6ebef Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:24:44 +0900 Subject: [PATCH 191/358] Update _ja/tour/self-types.md Co-Authored-By: take2webservice --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index c5989e71e8..a8fd16a31c 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -21,7 +21,7 @@ redirect_from: "/tutorials/tour/self-types.html" 自己型は`this`の型、または`this`の別名となる別の識別子を絞り込む方法です。 その構文は普通の関数構文のように見えますが、全く異なる意味があります。 -トレイトで自己型を使うために、識別子とミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 +トレイトで自己型を使うには、識別子、ミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 ```tut trait User { def username: String From 42c1e0daebf35d6bda1ce8631d6e53dd331b8275 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:24:57 +0900 Subject: [PATCH 192/358] Update _ja/tour/self-types.md Co-Authored-By: take2webservice --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index a8fd16a31c..31517548af 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -37,6 +37,6 @@ class VerifiedTweeter(val username_ : String) extends Tweeter with User { // Tw } val realBeyoncé = new VerifiedTweeter("Beyoncé") -realBeyoncé.tweet("Just spilled my glass of lemonade") // "real Beyoncé: Just spilled my glass of lemonade"とプリントします。 +realBeyoncé.tweet("Just spilled my glass of lemonade") // "real Beyoncé: Just spilled my glass of lemonade"と出力します。 ``` `trait Tweeter`の中で`this: User =>`と記述したので、今は変数`username`は`tweet`メソッドのスコープ内にあります。これは更に`VerifiedTweeter`は`Tweeter`を継承し、(`with User`を使って)`User`もミックスインしなければならいことを意味します。 From 4cdeb5772b3a07dc5e1036efb6353f0e485390d8 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:25:24 +0900 Subject: [PATCH 193/358] Update _ja/tour/self-types.md Co-Authored-By: take2webservice --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index 31517548af..dddf7c2750 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -39,4 +39,4 @@ class VerifiedTweeter(val username_ : String) extends Tweeter with User { // Tw val realBeyoncé = new VerifiedTweeter("Beyoncé") realBeyoncé.tweet("Just spilled my glass of lemonade") // "real Beyoncé: Just spilled my glass of lemonade"と出力します。 ``` -`trait Tweeter`の中で`this: User =>`と記述したので、今は変数`username`は`tweet`メソッドのスコープ内にあります。これは更に`VerifiedTweeter`は`Tweeter`を継承し、(`with User`を使って)`User`もミックスインしなければならいことを意味します。 +`trait Tweeter`の中で`this: User =>`と記述したので、今は変数`username`は`tweet`メソッドのスコープ内にあります。これはさらに、`VerifiedTweeter`が`Tweeter`を継承する際、(`with User`を使って)`User`もミックスインしなければならいことを意味します。 From 1c2374ead77b40060c55894c6595428afe248ac9 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:26:43 +0900 Subject: [PATCH 194/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index f4501923ab..891cfd1cef 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -19,7 +19,7 @@ redirect_from: "/tutorials/tour/implicit-parameters.html" Scalaがこれらのパラメータを探す場合は2つのカテゴリに分かれます。 -* Scalaは暗黙のパラメータブロックを持つメソッドが呼び出された時点で、まず(プレフィックスなしに)に直接アクセスできる暗黙の定義と暗黙のパラメータを探します。 +* Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 * そして全てのコンパニオンオブジェクトの中でimplicitで印をつけられ、暗黙に候補となる型と関連づけられたメンバーを探します。 より詳しいScalaがimplicitを探すガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 From f818da0bd5081709fe32812bda43be7767a20e41 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:27:58 +0900 Subject: [PATCH 195/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 891cfd1cef..18afdbdda5 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -20,7 +20,7 @@ redirect_from: "/tutorials/tour/implicit-parameters.html" Scalaがこれらのパラメータを探す場合は2つのカテゴリに分かれます。 * Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 -* そして全てのコンパニオンオブジェクトの中でimplicitで印をつけられ、暗黙に候補となる型と関連づけられたメンバーを探します。 +* 次に、候補となる型に関連づけられた全てのコンパニオンオブジェクトの中でimplicitと宣言されているメンバーを探します。 より詳しいScalaがimplicitを探すガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 From 84ab042380907f0c43293403b0c04ab30a63b909 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:28:32 +0900 Subject: [PATCH 196/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 18afdbdda5..f75c5394b4 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -22,7 +22,7 @@ Scalaがこれらのパラメータを探す場合は2つのカテゴリに分 * Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 * 次に、候補となる型に関連づけられた全てのコンパニオンオブジェクトの中でimplicitと宣言されているメンバーを探します。 -より詳しいScalaがimplicitを探すガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 +Scalaがimplicitをどこから見つけるかについてのより詳しいガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 以下の例では、モノイドの`add`と`unit`の演算を使い、要素のリストの合計を計算するメソッド`sum`を定義しています。 From 57266f2d7fc1ae5a92dcbe2206a5bb7450d86876 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:29:37 +0900 Subject: [PATCH 197/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index f75c5394b4..6a1c10227f 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -25,6 +25,7 @@ Scalaがこれらのパラメータを探す場合は2つのカテゴリに分 Scalaがimplicitをどこから見つけるかについてのより詳しいガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 以下の例では、モノイドの`add`と`unit`の演算を使い、要素のリストの合計を計算するメソッド`sum`を定義しています。 +implicitの値がトップレベルでないことに注意してください。 ```tut abstract class Monoid[A] { From 24e4a6ca680fa64d44be3a8ec593847b4ffb162e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:30:48 +0900 Subject: [PATCH 198/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 6a1c10227f..cb31c4a9f5 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -54,7 +54,7 @@ object ImplicitTest { } } ``` -`モノイド`はここでは`add`と呼ばれる演算子を定義します。演算子は`A`のペアを組み合わせ、別の`A`を返します。そしてそれは(特別な)`A`を作ることができる`unit`と呼ばれる演算子です。 +`モノイド`はここでは`add`と呼ばれる処理を定義します。この処理は`A`のペアを結合して、別の`A`を返します。また、(特定の)`A`を作ることができる`unit`と呼ばれる処理も定義します。 どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトは暗黙に使われうることを指し示します。 From fc8167ff4c62b070492e383459478f353215779f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:32:57 +0900 Subject: [PATCH 199/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index cb31c4a9f5..fae9ffbe11 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -58,7 +58,7 @@ object ImplicitTest { どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトは暗黙に使われうることを指し示します。 -メソッド`sum`は`List[A]`を受け取り、`A`を返します。そしてメソッドは初期値`A`を`unit`から受け取り、リスト中でそれぞれ次の`A`を`add`メソッドで組み合わせます。ここでパラメータ`m`をimplicitにすることは、そのメソッドを呼び出すとき、暗黙のパラメータ`m`を使うためにScalaが暗黙の`Monoid[A]`を見つけることができるなら`xs`パラメータを提供するだけで良いことを意味します。 +メソッド`sum`は`List[A]`を受け取り、`A`を返します。このメソッドは初期値`A`を`unit`から受け取り、リスト中の`A`を順番に`add`メソッドで結合します。ここでパラメータ`m`をimplicitにしているのは、そのメソッドを呼び出すとき、Scalaが暗黙のパラメータ`m`として暗黙の`Monoid[A]`を見つけることができるなら、私達は`xs`パラメータを提供するだけで良いということです。 `main`メソッドにて`sum`を2回呼び出すと、`xs`パラメータだけが提供されます。そうなるとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`List[Int]`を`xs`を渡します。それは `A`が`Int`であることを意味します。`m`と暗黙のパラメータリストは省略しており、Scalaは`Monoid[Int]`型のimplictを探します。最初のルックアップのルールには以下のように書いています。 From 8b8354f149500560955ec69a8891bb40b667d657 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:35:57 +0900 Subject: [PATCH 200/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index fae9ffbe11..041d42a7b3 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -60,7 +60,7 @@ object ImplicitTest { メソッド`sum`は`List[A]`を受け取り、`A`を返します。このメソッドは初期値`A`を`unit`から受け取り、リスト中の`A`を順番に`add`メソッドで結合します。ここでパラメータ`m`をimplicitにしているのは、そのメソッドを呼び出すとき、Scalaが暗黙のパラメータ`m`として暗黙の`Monoid[A]`を見つけることができるなら、私達は`xs`パラメータを提供するだけで良いということです。 -`main`メソッドにて`sum`を2回呼び出すと、`xs`パラメータだけが提供されます。そうなるとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`List[Int]`を`xs`を渡します。それは `A`が`Int`であることを意味します。`m`と暗黙のパラメータリストは省略しており、Scalaは`Monoid[Int]`型のimplictを探します。最初のルックアップのルールには以下のように書いています。 +`main`メソッドでは`sum`を2回呼んでいて、`xs`パラメータだけを与えています。するとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`xs`として`List[Int]`を渡します。それは `A`が`Int`であることを意味します。暗黙のパラメータリスト`m`が省略されているので、Scalaは暗黙の`Monoid[Int]`を探します。最初の探索ルールはこうでした。 > Scalaは暗黙のパラメータブロックを持つメソッドが呼び出された時点で、まず(プレフィックスなしに)に直接アクセスできる暗黙の定義と暗黙のパラメータを探します。 From 9532f24e507f2a6cd13136c83fe4e4ed0f6ffbb0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:37:49 +0900 Subject: [PATCH 201/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 041d42a7b3..a5cc5af381 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -62,7 +62,7 @@ object ImplicitTest { `main`メソッドでは`sum`を2回呼んでいて、`xs`パラメータだけを与えています。するとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`xs`として`List[Int]`を渡します。それは `A`が`Int`であることを意味します。暗黙のパラメータリスト`m`が省略されているので、Scalaは暗黙の`Monoid[Int]`を探します。最初の探索ルールはこうでした。 -> Scalaは暗黙のパラメータブロックを持つメソッドが呼び出された時点で、まず(プレフィックスなしに)に直接アクセスできる暗黙の定義と暗黙のパラメータを探します。 +> Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 `intMonoid`は`main`の中で直接アクセスされる暗黙の定義です。それ正しい型であり、`sum`メソッドに自動的に渡されます。 From bf07773cbbf6773fe686a895d6ca48313127fbb4 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:38:20 +0900 Subject: [PATCH 202/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index a5cc5af381..1f83ce6493 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -64,7 +64,7 @@ object ImplicitTest { > Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 -`intMonoid`は`main`の中で直接アクセスされる暗黙の定義です。それ正しい型であり、`sum`メソッドに自動的に渡されます。 +`intMonoid`は`main`の中で直接アクセスできる暗黙の定義です。型も一致しているので、`sum`メソッドに自動的に渡されます。 `sum`の2回目の呼び出しは`List[String]`を渡します。それは`A`は`String`であることを意味します。暗黙のルックアップは`Int`の時と同様に動きますが、この時は `stringMonoid`を見つけ、`m`として自動的に渡します。 From 0af24c7158a1b14885b6ae67e6ec9308e2c2a28b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:38:37 +0900 Subject: [PATCH 203/358] Update _ja/tour/implicit-parameters.md Co-Authored-By: take2webservice --- _ja/tour/implicit-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 1f83ce6493..1df511b4c9 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -66,7 +66,7 @@ object ImplicitTest { `intMonoid`は`main`の中で直接アクセスできる暗黙の定義です。型も一致しているので、`sum`メソッドに自動的に渡されます。 -`sum`の2回目の呼び出しは`List[String]`を渡します。それは`A`は`String`であることを意味します。暗黙のルックアップは`Int`の時と同様に動きますが、この時は `stringMonoid`を見つけ、`m`として自動的に渡します。 +`sum`の2回目の呼び出しは`List[String]`を渡します。それは`A`は`String`であることを意味します。暗黙の値の探索は`Int`の時と同様に動きますが、今回は `stringMonoid`を見つけ、`m`として自動的に渡します。 そのプログラムは以下を出力します。 ``` From 8b1152edb376befadc512198d6c505a43a3e45d8 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:41:38 +0900 Subject: [PATCH 204/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index da2e0c7dac..7ccf2d4e80 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -14,7 +14,7 @@ previous-page: implicit-parameters redirect_from: "/tutorials/tour/implicit-conversions.html" --- -型`S`から型`T`への暗黙の変換はimplicitな値や、implicitなメソッドにより定義されます。 +型`S`から型`T`への暗黙の変換は`S => T`という型のimplicit値や、その型に一致するimplicitメソッドで定義されます。 implicitな値は`S => T`な関数を持ち、implicitなメソッドはその型の値に変換できます。 暗黙の変換は2つの状況で適用されます。 From ae17f5e7c33836534dbe524af7ea2906ef65bb7f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:42:01 +0900 Subject: [PATCH 205/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 7ccf2d4e80..ca4deb6223 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -15,7 +15,6 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" --- 型`S`から型`T`への暗黙の変換は`S => T`という型のimplicit値や、その型に一致するimplicitメソッドで定義されます。 -implicitな値は`S => T`な関数を持ち、implicitなメソッドはその型の値に変換できます。 暗黙の変換は2つの状況で適用されます。 From 731a43e9b2a26a3e0b0ea34cc904e3a9965427d1 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:44:30 +0900 Subject: [PATCH 206/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index ca4deb6223..68c8913bac 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -19,7 +19,7 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" 暗黙の変換は2つの状況で適用されます。 * もし式`e`が型`S`であり、`S`は式の期待する型`T`に準拠しない場合 -* 型`S`の`e`が持つセレクション`e.m`において、セレクター`m`が`S`のメンバーではない場合 +* 型`S`の`e`を使う表記`e.m`があって、セレクター`m`が`S`のメンバーではない場合 最初のケースでは変換`c`を検索します。`c`は`e`に適用可能で、その結果の型が`T`に準拠します。 2つ目のケースでは変換`c`は検索します。`c`は`e`に適用可能で、その結果は`m`と名付けられたメンバー含みます。 From 7aececd8539a0cb53e12aa167eef29c3b9ed1b6e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:47:24 +0900 Subject: [PATCH 207/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 68c8913bac..51ff406225 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -21,7 +21,7 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" * もし式`e`が型`S`であり、`S`は式の期待する型`T`に準拠しない場合 * 型`S`の`e`を使う表記`e.m`があって、セレクター`m`が`S`のメンバーではない場合 -最初のケースでは変換`c`を検索します。`c`は`e`に適用可能で、その結果の型が`T`に準拠します。 +最初のケースでは、`e`を渡せて、戻り値の型が`T`に適合するような変換`c`を検索します。 2つ目のケースでは変換`c`は検索します。`c`は`e`に適用可能で、その結果は`m`と名付けられたメンバー含みます。 implicitなメソッド`List[A] => Ordered[List[A]]`がスコープの中にあれば、implicitなメソッド`Int => Ordered[Int]`と同様に、`List[Int]`型の2つのリストにおける以下の処理は正当なものになります。 From 15a724957d767fb79a847f80d3e6290169f7bbe2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:47:37 +0900 Subject: [PATCH 208/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 51ff406225..1612ea7b19 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" 暗黙の変換は2つの状況で適用されます。 -* もし式`e`が型`S`であり、`S`は式の期待する型`T`に準拠しない場合 +* もし式`e`が型`S`であり、`S`は式の期待する型`T`に適合しない場合 * 型`S`の`e`を使う表記`e.m`があって、セレクター`m`が`S`のメンバーではない場合 最初のケースでは、`e`を渡せて、戻り値の型が`T`に適合するような変換`c`を検索します。 From 773badc1e731fe8e3441b750e4f3b7fae672ad85 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:48:03 +0900 Subject: [PATCH 209/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 1612ea7b19..36e49ec248 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -22,7 +22,7 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" * 型`S`の`e`を使う表記`e.m`があって、セレクター`m`が`S`のメンバーではない場合 最初のケースでは、`e`を渡せて、戻り値の型が`T`に適合するような変換`c`を検索します。 -2つ目のケースでは変換`c`は検索します。`c`は`e`に適用可能で、その結果は`m`と名付けられたメンバー含みます。 +2つ目のケースでは、`e`を渡せて、戻り値が`m`というメンバーを持つような変換`c`を検索します。 implicitなメソッド`List[A] => Ordered[List[A]]`がスコープの中にあれば、implicitなメソッド`Int => Ordered[Int]`と同様に、`List[Int]`型の2つのリストにおける以下の処理は正当なものになります。 From 44b1139a23ccf5c04a2a470aea23bc2f16eb2749 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:50:19 +0900 Subject: [PATCH 210/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 36e49ec248..178b8b815f 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -24,7 +24,7 @@ redirect_from: "/tutorials/tour/implicit-conversions.html" 最初のケースでは、`e`を渡せて、戻り値の型が`T`に適合するような変換`c`を検索します。 2つ目のケースでは、`e`を渡せて、戻り値が`m`というメンバーを持つような変換`c`を検索します。 -implicitなメソッド`List[A] => Ordered[List[A]]`がスコープの中にあれば、implicitなメソッド`Int => Ordered[Int]`と同様に、`List[Int]`型の2つのリストにおける以下の処理は正当なものになります。 +implicitなメソッド`List[A] => Ordered[List[A]]`と`Int => Ordered[Int]`がスコープの中にあれば、`List[Int]`型の2つのリストにおける以下の処理は正当なものになります。 ``` List(1, 2, 3) <= List(4, 5) From 6784f92ad06fbe13a879d4ae87ace5fec8f11f99 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:51:54 +0900 Subject: [PATCH 211/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 178b8b815f..2eec46e830 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -41,7 +41,7 @@ implicit def list2ordered[A](x: List[A]) def compare(that: List[A]): Int = 1 } ``` -implicitなインポートされたオブジェクト`scala.Predef`は度々使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)にいくつかの別名を宣言しますが、暗黙のうちにいくつかの変換もします。 +暗黙にインポートされているオブジェクト`scala.Predef`は頻繁に使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)といくつかの暗黙の型変換を宣言しています。 例えば、`java.lang.Integer`を期待してJavaのメソッドを呼び出す時、自由に`scala.Int`を代わりに渡すことができます。それはPredefオブジェクトが以下の暗黙の変換をを含んでいるからです。 From af2649427b09ea6824f8fb3c8c56a9b534fba74a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:52:09 +0900 Subject: [PATCH 212/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 2eec46e830..133302ff31 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -43,7 +43,7 @@ implicit def list2ordered[A](x: List[A]) ``` 暗黙にインポートされているオブジェクト`scala.Predef`は頻繁に使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)といくつかの暗黙の型変換を宣言しています。 -例えば、`java.lang.Integer`を期待してJavaのメソッドを呼び出す時、自由に`scala.Int`を代わりに渡すことができます。それはPredefオブジェクトが以下の暗黙の変換をを含んでいるからです。 +例えば、`java.lang.Integer`を受け取るようなJavaのメソッドを呼び出す時、自由に`scala.Int`を代わりに渡すことができます。それはPredefオブジェクトが以下の暗黙の変換をを含んでいるからです。 ```tut import scala.language.implicitConversions From 0059b559fac1f9d702ff1e0db03aa9a405820ca4 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:52:59 +0900 Subject: [PATCH 213/358] Update _ja/tour/implicit-conversions.md Co-Authored-By: take2webservice --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 133302ff31..4a328829c6 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -52,7 +52,7 @@ implicit def int2Integer(x: Int) = java.lang.Integer.valueOf(x) ``` -暗黙の変換は落とし穴になり得るため、無差別に使えば、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 +暗黙の変換は無差別に使うと落とし穴になり得るため、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 警告をオフにするには、次のいずれかの操作を行います。 From 5ffd5e2e15624e81d809355c582a9d429458ec06 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 24 Apr 2019 02:54:53 +0900 Subject: [PATCH 214/358] Update _ja/tour/polymorphic-methods.md --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 8bdbe3ea18..03cf410d00 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -1,6 +1,6 @@ --- layout: tour -title: ポリモフィズムメソッド +title: ポリモーフフィックメソッド discourse: true From 21b6e15b05c2e416148a807d3071e07b06ed6080 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:56:03 +0900 Subject: [PATCH 215/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 03cf410d00..20f567999a 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -15,7 +15,7 @@ prerequisite-knowledge: unified-types redirect_from: "/tutorials/tour/polymorphic-methods.html" --- -Scalaのメソッドは値と同様に型によってパラメータ化することができます。構文はジェネリッククラスの構文と同じです。 +Scalaのメソッドは値と同様に型によってパラメータ化することができます。構文はジェネリッククラスの構文と似ています。 型パラメータは角カッコで囲まれますが、値パラメータは丸括弧で囲まれます。 こちらが例になります。 From 85627c12f5d9d007abddd60c30a43d50add8cf52 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:56:36 +0900 Subject: [PATCH 216/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 20f567999a..7aa2ef1528 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/polymorphic-methods.html" --- Scalaのメソッドは値と同様に型によってパラメータ化することができます。構文はジェネリッククラスの構文と似ています。 -型パラメータは角カッコで囲まれますが、値パラメータは丸括弧で囲まれます。 +値パラメータは丸括弧で囲まれるのに対して、型パラメータは角カッコで囲まれます。 こちらが例になります。 From 7a2320c8c6c2bd22e2f8f5db3773314653ea7a42 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:56:49 +0900 Subject: [PATCH 217/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 7aa2ef1528..7c4f312481 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/polymorphic-methods.html" Scalaのメソッドは値と同様に型によってパラメータ化することができます。構文はジェネリッククラスの構文と似ています。 値パラメータは丸括弧で囲まれるのに対して、型パラメータは角カッコで囲まれます。 -こちらが例になります。 +こちらが例です。 ```tut def listOfDuplicates[A](x: A, length: Int): List[A] = { From 3877971db64c130c74a48b3b05accdba3bc6bb92 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:57:51 +0900 Subject: [PATCH 218/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 7c4f312481..d5e0dd2ee7 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -31,7 +31,7 @@ println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3) println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) ``` -メソッド`listOfDuplicates`は型パラメータ`A`と値パラメータ`x`と`length`を受け取ります。値`x`は型`A`となります。もし`length < 1`なら空のリストを返します。一方で`x`を再帰呼び出しで返された複写リストの先頭に追加します。(`::`は右側のリストの先頭への左側の要素の追加を意味します。) +メソッド`listOfDuplicates`は型パラメータ`A`と値パラメータ`x`と`length`を受け取ります。値`x`の型は`A`です。もし`length < 1`なら空のリストを返します。それ以外の場合は`x`を再帰呼び出しで返された複写リストの先頭に追加します。(`::`の意味は、左辺の要素を右辺のリストの先頭に追加することです。) 最初の呼び出し例のでは、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`[Int]`でなければならず、戻される型は`List[Int]`となります。 From c3db42c5b45bda949fa197769bcffd8ed12c74dc Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 02:58:18 +0900 Subject: [PATCH 219/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index d5e0dd2ee7..6587f40b58 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -33,6 +33,6 @@ println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) メソッド`listOfDuplicates`は型パラメータ`A`と値パラメータ`x`と`length`を受け取ります。値`x`の型は`A`です。もし`length < 1`なら空のリストを返します。それ以外の場合は`x`を再帰呼び出しで返された複写リストの先頭に追加します。(`::`の意味は、左辺の要素を右辺のリストの先頭に追加することです。) -最初の呼び出し例のでは、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`[Int]`でなければならず、戻される型は`List[Int]`となります。 +最初の呼び出し例では、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`Int`でなければならず、戻される型は`List[Int]`となります。 2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことが判ります。コンパイラはしばしばコンテキストか値引数の型に基づき、型パラメータを推論できます。この例では`"La"`は`String`であり、コンパイラは`A`が`String`でなければならないと気づきます。 From b0e045d2bbf8761a1256c34af30a6dd6925d0555 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Wed, 24 Apr 2019 03:00:03 +0900 Subject: [PATCH 220/358] Update _ja/tour/polymorphic-methods.md Co-Authored-By: take2webservice --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 6587f40b58..cdd0706dec 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -35,4 +35,4 @@ println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) 最初の呼び出し例では、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`Int`でなければならず、戻される型は`List[Int]`となります。 -2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことが判ります。コンパイラはしばしばコンテキストか値引数の型に基づき、型パラメータを推論できます。この例では`"La"`は`String`であり、コンパイラは`A`が`String`でなければならないと気づきます。 +2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことを示しています。コンパイラは文脈または値引数の型に基づき、型パラメータを推論できることが多いです。この例では`"La"`が`String`なので、コンパイラは`A`が`String`にちがいないことがわかります。 From 943b8789142b48255dbece435beb46aafc0ad483 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:13:10 +0900 Subject: [PATCH 221/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 666e74e2cc..08c52fd5bb 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -1,6 +1,6 @@ --- layout: tour -title: 型インターフェイス +title: 型推論 language: ja discourse: true From b658b9b88c3600f846a6a14d9b38d86422857667 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:14:33 +0900 Subject: [PATCH 222/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 08c52fd5bb..3d7410ca92 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -12,7 +12,7 @@ next-page: operators previous-page: polymorphic-methods --- -Scalaコンパイラはしばしば式の型推論するので、明示的に型を宣言する必要はありません。 +Scalaコンパイラが式の型を推論できることが多いので、明示的に型を宣言する必要はありません。 ## 型の省略 From 9c35586ab4bea8ad7018a691d8486b6432eeecb8 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:16:13 +0900 Subject: [PATCH 223/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 3d7410ca92..e4d04ee268 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -32,7 +32,7 @@ def squareOf(x: Int) = x * x def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1) ``` -以下のような場合は型パラメータを指定することは強制されません。それは[ポリモーフフィックメソッド](polymorphic-methods.html)が呼ばれる時や[ジェネリッククラス](generic-classes.html) がインスタンス化される時です。Scalaコンパイラはそのようなコンテキスト、メソッドやコンストラクタのパラメータ型に型パラメータが実際になければ推論します。 +次のような場合は型パラメータを指定することは強制されません。[ポリモーフフィックメソッド](polymorphic-methods.html)が呼ばれる時や[ジェネリッククラス](generic-classes.html) がインスタンス化される時です。Scalaコンパイラは文脈あるいはメソッドやコンストラクタの実際の引数から、指定されていない型パラメータを推論します。 こちらは2つの例です。 From a8034fa302b1b68e385a4f3363a7fae7cdfa33b3 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:17:10 +0900 Subject: [PATCH 224/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index e4d04ee268..18e9531071 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -47,7 +47,7 @@ val q = id(1) // type: Int ## パラメータ -コンパイラはメソッドのパラメータを決して推論しません。しかしながら、確かなケースとして、無名関数のパラメータ型を推論できます。それは関数に引数が渡された場合です。 +コンパイラはメソッドのパラメータを決して推論しません。しかし、関数が引数として渡されている場合は、無名関数のパラメータ型を推論できます。 ```tut Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) From ab90954e7c67f13e196017da6faa694e551f7280 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:31:16 +0900 Subject: [PATCH 225/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 18e9531071..2163ff2d3d 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -53,7 +53,7 @@ val q = id(1) // type: Int Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) ``` -mapのパラメータは`f: A => B`です。`Seq`に整数値を渡すため、コンパイラは`A`が`Int`だと気づきます(つまり、あの`x`は整数です)。そのためコンパイラは`x * 2`から`B`は型`Int`であると推論できます。 +mapのパラメータは`f: A => B`です。`Seq`の中に整数が入っているので、コンパイラは`A`が`Int`だと知っています(つまり、この`x`は整数です)。したがってコンパイラは`x * 2`から`B`が型`Int`であると推論できます。 ## 型推論に頼ら*ない*時 From 44b3d8e9b6d5514f966357fd9984e26d76c1e67a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:32:14 +0900 Subject: [PATCH 226/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 2163ff2d3d..1a0b4a9ec2 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -71,4 +71,4 @@ var obj = null obj = new AnyRef ``` -こちらはコンパイルできません。`obj`の型推論は`Null`だからです。その型の唯一の値が`null`だから、他の値を割り当てられません。 +こちらはコンパイルできません。`obj`に推論された型は`Null`だからです。その型の唯一の値が`null`なので、他の値を代入できれません。 From 37d2bb3f47d64392a0fb1b100d12206cab4a3379 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:33:10 +0900 Subject: [PATCH 227/358] Update _ja/tour/type-inference.md Co-Authored-By: take2webservice --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index 1a0b4a9ec2..ea919e3207 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -57,7 +57,7 @@ mapのパラメータは`f: A => B`です。`Seq`の中に整数が入ってい ## 型推論に頼ら*ない*時 -一般的にパブリックなAPIではメンバーの型宣言を公開するとより読みやすいと考えられます。そのため、コードをユーザーに公開するAPIならば型を明白にすることお勧めします。 +一般的には、パブリックなAPIで公開されているメンバーの型を宣言したほうが読みやすいと考えられています。そのため、ユーザーに公開するAPIではあなたのコードの型を明示することをお勧めします。 また、型推論は特定の型を推論することがあります。次のように書いたとします。 From a5f5b53993aa7f0dece43f147598480df8331f90 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:54:16 +0900 Subject: [PATCH 228/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 90eb4b8ab3..32213b2d72 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -1,6 +1,6 @@ --- layout: tour -title: オペレータ +title: 演算子 language: ja discourse: true From 4bcd828febd9247fa999c0bd4018c3ea90f5eede Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:54:28 +0900 Subject: [PATCH 229/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 32213b2d72..cc27b1b999 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -14,7 +14,7 @@ prerequisite-knowledge: case-classes redirect_from: "/tutorials/tour/operators.html" --- -Scalaではオペレーターはメソッドです。1つのパラメータを持つメソッドであれば*中置オペレータ*として使えます。例えば、`+`はドット記法で呼び出せます。 +Scalaではオペレーターはメソッドです。1つのパラメータを持つメソッドであれば*中置演算子*として使えます。例えば、`+`はドット記法で呼び出せます。 ``` 10.+(1) From fb29d19346e0afce39026d2531442b1a8338d84d Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:54:35 +0900 Subject: [PATCH 230/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index cc27b1b999..93e7df0d75 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -26,7 +26,7 @@ Scalaではオペレーターはメソッドです。1つのパラメータを 10 + 1 ``` -## オペレータの定義方法と使い方 +## 演算子の定義方法と使い方 有効な識別子であればオペレータとして使用できます。これは `add`のような名前と`+`のようなシンボルも含みます。 ```tut From a496eb2b6139fe5c1d3e53bb0e0539afddb38e8f Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:54:42 +0900 Subject: [PATCH 231/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 93e7df0d75..cd697f47af 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -52,7 +52,7 @@ case class MyBool(x: Boolean) { } ``` -この時、`and`と`or`を中置オペレータとして使えます。 +この時、`and`と`or`を中置演算子として使えます。 ```tut def not(x: MyBool) = x.negate From 41417f4ee94fcb32d11f72592a1f1eb682cdaf5b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:54:55 +0900 Subject: [PATCH 232/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index cd697f47af..736643a78e 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -59,7 +59,7 @@ def not(x: MyBool) = x.negate def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y) ``` -これにより`xor`の定義をより読みやすくします。 +これにより`xor`の定義がより読みやすくなります。 ## 優先順位 From 09a0c32c489f52f2fbb72d6f7e2ed397007220e7 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:57:05 +0900 Subject: [PATCH 233/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 736643a78e..d268d5ddc3 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -63,7 +63,7 @@ def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y) ## 優先順位 -式が複数のオペレータを使う時、最初の記号の優先度に基づきオペレータは評価されます。 +式が複数の演算子を使う時、それらの演算子は最初の文字の(以下に挙げる)優先度に基づき評価されます。 ``` (以下に表示されていない記号) * / % From b8c296b9dd0b97843fd68488121dc0dabf1f70e5 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:57:39 +0900 Subject: [PATCH 234/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index d268d5ddc3..4deb3a5d97 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -77,6 +77,7 @@ def xor(x: MyBool, y: MyBool) = (x or y) and not(x and y) (全ての文字) ``` これはあなたが定義した関数にも適用できます。 +たとえば、以下の式 ``` a + b ^? c ?^ d less a ==> b | c ``` From a2041c404eb104c0320981d457fb44f56fff931c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 13:58:05 +0900 Subject: [PATCH 235/358] Update _ja/tour/operators.md Co-Authored-By: take2webservice --- _ja/tour/operators.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 4deb3a5d97..ac09316f8c 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -28,7 +28,7 @@ Scalaではオペレーターはメソッドです。1つのパラメータを ## 演算子の定義方法と使い方 -有効な識別子であればオペレータとして使用できます。これは `add`のような名前と`+`のようなシンボルも含みます。 +有効な識別子であれば演算子として使用できます。これは `add`のような名前と`+`のような記号も含みます。 ```tut case class Vec(val x: Double, val y: Double) { def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y) From 58036ba227f6a0c1a752024ba7de7453ce7ce1ce Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:06:59 +0900 Subject: [PATCH 236/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 73d1e18a98..13cd979553 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -1,6 +1,6 @@ --- layout: tour -title: 名前付きパラメータ +title: 名前渡しパラメータ discourse: true From 3936af8c20e7634ec30663e494c302e95f39f96e Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:07:16 +0900 Subject: [PATCH 237/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 13cd979553..d9bf79f800 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -13,7 +13,7 @@ previous-page: operators redirect_from: "/tutorials/tour/by-name-parameters.html" --- -*名前付きパラメータ*は使用された時に評価されます。それらは*値つきパラメータ*とは対照的です。名前で呼び出せるパラメータを作るには、単純に`=>`を型の前につけます。 +*名前渡しのパラメータ*は使用された時に評価されます。それらは*値渡しパラメータ*とは対照的です。名前渡しのパラメータを作るには、単純に`=>`を型の前につけます。 ```tut def calculate(input: => Int) = input * 37 ``` From 9c0e642fb09c379293833d50863a1c650ade151a Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:07:26 +0900 Subject: [PATCH 238/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index d9bf79f800..21524b9db7 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/by-name-parameters.html" def calculate(input: => Int) = input * 37 ``` -名前付きパラメータの利点は関数本体の中で使わなければ評価されない点です。一方で、値付きパラメータの利点は1度だけ評価される点です。 +名前渡しパラメータの利点は関数本体の中で使わなければ評価されない点です。一方で、値渡しパラメータの利点は1度しか評価されない点です。 こちらはwhileループをどのように実装するかの例です。 From 720fcbce827fa4589ad714b5ad721c51507c1803 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:08:00 +0900 Subject: [PATCH 239/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 21524b9db7..11b4480602 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -37,7 +37,7 @@ whileLoop (i > 0) { } // prints 2 1 ``` -メソッド`whileLoop`は条件とループの本体を受け取るために複数パラメータリストを使います。もし`condition`がtrueならば、`body`は実行され、そしてwhileループの再帰呼び出しがされます。`condition`がfalseならば、bodyは決して評価されません。それは`body`の型の前に`=>`をつけたからです。 +このメソッド`whileLoop`は条件とループの本体を受け取るために複数パラメータリストを使います。もし`condition`がtrueならば、`body`が実行され、次にwhileLoopが再帰的に呼ばれます。`condition`がfalseならば、bodyは決して評価されません。それは`body`の型の前に`=>`をつけたからです。 ここで、`condition`に`i > 0`、`body`に`println(i); i-= 1`を渡した場合、多くの言語では一般的なwhileループと同じ振る舞いをします。 From 5fd09234da1b81b8dd33bca92fd7e3d54ae8bf80 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:08:16 +0900 Subject: [PATCH 240/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 11b4480602..1a5ac63e31 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -39,6 +39,6 @@ whileLoop (i > 0) { このメソッド`whileLoop`は条件とループの本体を受け取るために複数パラメータリストを使います。もし`condition`がtrueならば、`body`が実行され、次にwhileLoopが再帰的に呼ばれます。`condition`がfalseならば、bodyは決して評価されません。それは`body`の型の前に`=>`をつけたからです。 -ここで、`condition`に`i > 0`、`body`に`println(i); i-= 1`を渡した場合、多くの言語では一般的なwhileループと同じ振る舞いをします。 +ここで、`condition`に`i > 0`、`body`に`println(i); i-= 1`を渡した場合、多くの言語で一般的なwhileループと同じ振る舞いをします。 パラメータが使われるまで評価を遅延させる機能はパフォーマンスを助けます。それはパラメータを評価するための計算が集約的な場合やURLの取得のような時間がかかるコードブロックの場合です。 From e17b616e47afd366175e11d8b4ec449b91c05d15 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:10:12 +0900 Subject: [PATCH 241/358] Update _ja/tour/by-name-parameters.md Co-Authored-By: take2webservice --- _ja/tour/by-name-parameters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 1a5ac63e31..4eb343bcbc 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -41,4 +41,4 @@ whileLoop (i > 0) { ここで、`condition`に`i > 0`、`body`に`println(i); i-= 1`を渡した場合、多くの言語で一般的なwhileループと同じ振る舞いをします。 -パラメータが使われるまで評価を遅延させる機能はパフォーマンスを助けます。それはパラメータを評価するための計算が集約的な場合やURLの取得のような時間がかかるコードブロックの場合です。 +パラメータが使われるまで評価を遅延する機能はパフォーマンスの助けになることがあります。それはパラメータを評価するのに多くの計算が必要な場合や、URLの取得のような時間がかかるコードブロックの場合です。 From 77df071778b2d5af389c30121b6a030d284a0b53 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:11:37 +0900 Subject: [PATCH 242/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 130ee0b03a..b0f4d13ff8 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -25,7 +25,7 @@ object DeprecationDemo extends App { ``` これはコンパイルされますが、コンパイラは警告"there was one deprecation warning"を出力します。 -アノテーション句はそれに続くに最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。与えられたこれらの句の中での順番は重要ではありません。 +アノテーション句はそれに続くに最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。これらの句の順番は重要ではありません。 ## エンコーディングの正確性を保証するアノテーション From bfb32cd21178a19a557959e534f903091b1313d9 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:12:36 +0900 Subject: [PATCH 243/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index b0f4d13ff8..0283b942fc 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -29,7 +29,7 @@ object DeprecationDemo extends App { ## エンコーディングの正確性を保証するアノテーション -確かにいくつかのアノテーションは条件が一致すればコンパイルを失敗させます。例えば、アノテーション`@tailrec`はメソッドは[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰は必須メモリを一定に維持します。こちらは階乗を計算するメソッドの中での使われ方です。 +条件が一致しない場合にコンパイルを失敗させるアノテーションもあります。例えば、アノテーション`@tailrec`はメソッドは[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰ではメモリ使用量が一定になります。こちらは階乗を計算するメソッドの中での使われ方です。 ```tut import scala.annotation.tailrec From 1c90bd9691102558d7490b3006e2ee579f5cff52 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:13:26 +0900 Subject: [PATCH 244/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 0283b942fc..9ab2713b1e 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -58,7 +58,7 @@ def factorial(x: Int): Int = { ## コード生成に影響するアノテーション -`@inline`のようなアノテーションは生成されたコードに影響します。(つまり、アノテーションを使わなかった場合とでjarファイルのバイト数が異なる場合があります。)インライン化は呼び出し時にメソッドの本体へのコード挿入を意味します。結果のバイトコードはより長くなりますが、上手くいけば実行が早くなります。アノテーション`@inline`を使ってもメソッドのインライン化はされません。しかし、生成されたコードのサイズに関するヒューリスティックスが満たされた場合に限りコンパイラにインライン化をさせます。 +`@inline`のようなアノテーションは生成されたコードに影響します(つまり、アノテーションを使わなかった場合とでjarファイルのバイト数が異なる場合があります)。インライン化とは、メソッドを呼び出している箇所にメソッド本体のコードを挿入することを意味します。結果のバイトコードはより長くなりますが、上手くいけば実行が早くなります。アノテーション`@inline`を使ってもメソッドのインライン化が保証されるわけではありません。しかし、生成されたコードのサイズに関するヒューリスティックスが満たされた場合に限りコンパイラにインライン化をさせます。 ### Javaのアノテーション ### Javaと相互運用するScalaのコードを書いている時、記述するアノテーション構文は少し違います。 From 7d371b1505a7aae63928e761cf0af8c73bd9b3a4 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:14:52 +0900 Subject: [PATCH 245/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 9ab2713b1e..f8cb59f9ef 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -65,7 +65,7 @@ Javaと相互運用するScalaのコードを書いている時、記述する **注:** Javaアノテーションを使う場合、`-target:jvm-1.8`オプションを使ってください。 -Javaには[アノテーション](https://docs.oracle.com/javase/tutorial/java/annotations/)の形をしたユーザー定義メタデータがあります。アノテーションの主な機能は要所の初期化にのために名前と値のペアを指定する必要があります。例えば、あるクラスのソースを追いかけるためにアノテーションが必要な場合、以下のようにそれを定義します。 +Javaには[アノテーション](https://docs.oracle.com/javase/tutorial/java/annotations/)の形をしたユーザー定義メタデータがあります。Javaのアノテーションの特徴は、要素の初期化のために名前と値のペアを指定する必要があることです。例えば、クラスのソースを追いかけるためのアノテーションが必要なとき、以下のように定義するかもしれません。 ``` @interface Source { From 5c8bb1bc5b2887c8269e33b7876e24a878f0506c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:15:07 +0900 Subject: [PATCH 246/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index f8cb59f9ef..e6d01a4e3b 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -90,7 +90,7 @@ Scalaでのアノテーションの適用はコンストラクタの呼び出し class MyScalaClass ... ``` -アノテーションが(デフォルト値を除き)要素を1つだけ含む場合、この構文はかなり退屈です。そのため慣例により、名前が`value`と指定されていれば、コンストラクタっぽい構文でJavaに適用できます。 +アノテーションが(デフォルト値を除き)要素を1つだけ含む場合、この構文はかなり退屈です。そのため慣例により、名前が`value`と指定されていれば、コンストラクタのような構文でJavaに適用できます。 ``` @interface SourceURL { From 5b95d3b5464462aa6083750d6afd4f340bf3a22b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:15:18 +0900 Subject: [PATCH 247/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index e6d01a4e3b..6e490279bd 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -106,7 +106,7 @@ class MyScalaClass ... public class MyClass extends TheirClass ... ``` -この場合、Scalaは同じ可能性を提供します。 +この場合、Scalaでも同じことができます。 ``` @SourceURL("http://coders.com/")1 From c1471cdee4acb58e0a94c65aab11b257e94acf75 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:15:32 +0900 Subject: [PATCH 248/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 6e490279bd..866e6839e1 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -109,7 +109,7 @@ public class MyClass extends TheirClass ... この場合、Scalaでも同じことができます。 ``` -@SourceURL("http://coders.com/")1 +@SourceURL("http://coders.com/") class MyScalaClass ... ``` From 5a970f749c7935f2188c208c6991a98bbd834596 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:15:58 +0900 Subject: [PATCH 249/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 866e6839e1..209198e31e 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -113,7 +113,7 @@ public class MyClass extends TheirClass ... class MyScalaClass ... ``` -`mail`要素はデフォルト値で指定されます。そのためはっきりとそれに値を与える必要がありません。しかしながら、もしそれをする必要があるなら、Javaでは2つのスタイルを混ぜて組み合わせることはできません。 +`mail`要素はデフォルト値つきで定義されているので、明示的に値を指定する必要がありません。しかしながら、もし値を指定する必要がある場合、Javaでは2つのスタイルを混ぜて組み合わせることはできません。 ``` @SourceURL(value = "http://coders.com/", From 4ed05a7f1d2fb203d151e9e7da0b1a5904cbcd48 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 28 Apr 2019 14:16:41 +0900 Subject: [PATCH 250/358] Update _ja/tour/annotations.md Co-Authored-By: take2webservice --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index 209198e31e..b62132440d 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -121,7 +121,7 @@ class MyScalaClass ... public class MyClass extends TheirClass ... ``` -Scalaはこの点においてより多くの柔軟性を提供します。 +Scalaはこの点においてより柔軟です。 ``` @SourceURL("http://coders.com/", From 2519b5f7ec6d6e6327b0c5c6bbc5341358924ffa Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:42:13 +0900 Subject: [PATCH 251/358] Update _ja/tour/default-parameter-values.md Co-Authored-By: take2webservice --- _ja/tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md index 487fb1ae1e..7dc60e8bfe 100644 --- a/_ja/tour/default-parameter-values.md +++ b/_ja/tour/default-parameter-values.md @@ -15,7 +15,7 @@ prerequisite-knowledge: named-arguments, function syntax redirect_from: "/tutorials/tour/default-parameter-values.html" --- -Scalaはパラメータのデフォルト値を与えることができ、呼び出し者はこれらのパラメータを省略できます。 +Scalaはパラメータのデフォルト値を与えることができ、呼び出す側はこれらのパラメータを省略できます。 ```tut def log(message: String, level: String = "INFO") = println(s"$level: $message") From a1b79efd9c67b57449b7efd281e9acda63dfc8fe Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:45:15 +0900 Subject: [PATCH 252/358] Update _ja/tour/default-parameter-values.md Co-Authored-By: take2webservice --- _ja/tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md index 7dc60e8bfe..d0b8641635 100644 --- a/_ja/tour/default-parameter-values.md +++ b/_ja/tour/default-parameter-values.md @@ -24,7 +24,7 @@ log("System starting") // prints INFO: System starting log("User not found", "WARNING") // prints WARNING: User not found ``` -パラメータ`level`はデフォルト値を持ちつので、オプショナルです。最終行では、引数`"WARNING"`はデフォルト値`"INFO"`を上書きます。Javaでオーバーロードされたメソッドを実行する場合、同じ効果を得るためオプショナルなパラメーターでメソッドを使えます。しかしながら呼び出し者が引数を省略すれば、全ての後ろに続く引数に名前が必要です。 +パラメータ`level`はデフォルト値を持つので、省略可能です。最終行では、引数`"WARNING"`はデフォルト値`"INFO"`を上書きします。Javaで同じ効果を得るのに、省略可能なパラメータをもつメソッドを複数使ってメソッドのオーバーロードをしたようなものです。しかしながら呼び出す側が引数をひとつ省略すると、以降全ての引数は名前つきにする必要があります。 ```tut class Point(val x: Double = 0, val y: Double = 0) From 7b919560c501bfd65e294baf6f50c60f5e9ac37c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:45:46 +0900 Subject: [PATCH 253/358] Update _ja/tour/default-parameter-values.md Co-Authored-By: take2webservice --- _ja/tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md index d0b8641635..ba87370100 100644 --- a/_ja/tour/default-parameter-values.md +++ b/_ja/tour/default-parameter-values.md @@ -31,7 +31,7 @@ class Point(val x: Double = 0, val y: Double = 0) val point1 = new Point(y = 1) ``` -ここで、`y = 1`と明示しなければなりません。 +ここでは、`y = 1`と書かなければなりません。 Javaのコードから呼び出された時、Sclaにおけるデフォルトパラメータはオプショナルではありません。 From 57e12eddd943c28bae194a8321be6ee3d7788d58 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:46:38 +0900 Subject: [PATCH 254/358] Update _ja/tour/default-parameter-values.md Co-Authored-By: take2webservice --- _ja/tour/default-parameter-values.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/default-parameter-values.md b/_ja/tour/default-parameter-values.md index ba87370100..fe2da78c75 100644 --- a/_ja/tour/default-parameter-values.md +++ b/_ja/tour/default-parameter-values.md @@ -33,7 +33,7 @@ val point1 = new Point(y = 1) ``` ここでは、`y = 1`と書かなければなりません。 -Javaのコードから呼び出された時、Sclaにおけるデフォルトパラメータはオプショナルではありません。 +Scalaで定義したデフォルトパラメータはJavaのコードから呼び出される時はオプショナルではありません。 ```tut // Point.scala From a5db34262c330c8c7ae70ba0d88ad4a248237c2b Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:47:31 +0900 Subject: [PATCH 255/358] Update _ja/tour/named-arguments.md Co-Authored-By: take2webservice --- _ja/tour/named-arguments.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md index 86b2a075a9..e75ca991d8 100644 --- a/_ja/tour/named-arguments.md +++ b/_ja/tour/named-arguments.md @@ -15,7 +15,6 @@ redirect_from: "/tutorials/tour/named-arguments.html" --- メソッドを呼ぶ時、以下のように引数にパラメータ名でラベル付が可能です。 -When calling methods, you can label the arguments with their parameter names like so: ```tut def printName(first: String, last: String): Unit = { From f0c94efe6a33e3c30e563e1cf48dc9d783563212 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:49:13 +0900 Subject: [PATCH 256/358] Update _ja/tour/named-arguments.md Co-Authored-By: take2webservice --- _ja/tour/named-arguments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md index e75ca991d8..7584f0fbe8 100644 --- a/_ja/tour/named-arguments.md +++ b/_ja/tour/named-arguments.md @@ -26,7 +26,7 @@ printName(first = "John", last = "Smith") // Prints "John Smith" printName(last = "Smith", first = "John") // Prints "John Smith" ``` -名前付き引数の順序はどのように並び替えられるかに気をつけましょう。ただし、ある引数は名前をつけられ、他の引数には名前がつけられなかった場合、名前が付いていない引数は最初かつメソッドの署名の中にあるパラメーター並び順でなければなりません。 +名前付き引数の順序はどのように並び替えられるかに気をつけましょう。ただし、名前つき引数と名前つきでない引数がある場合は、名前つきでない引数は引数リストの最初に置かれ、かつメソッドシグネチャのパラメーター順でなければなりません。 ```tut:fail printName(last = "Smith", "john") // error: positional after named argument From b44d1743dbc0892fee6c8591c8f20898d8942787 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:49:24 +0900 Subject: [PATCH 257/358] Update _ja/tour/named-arguments.md Co-Authored-By: take2webservice --- _ja/tour/named-arguments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md index 7584f0fbe8..3b7e133669 100644 --- a/_ja/tour/named-arguments.md +++ b/_ja/tour/named-arguments.md @@ -32,4 +32,4 @@ printName(last = "Smith", first = "John") // Prints "John Smith" printName(last = "Smith", "john") // error: positional after named argument ``` -名前付き引数はJavaメソッドを呼び出す時は動きません。 +名前付き引数はJavaメソッドを呼び出す時には使えません。 From 57cc250185c5c09278459a096628e15fc318b9ed Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:52:44 +0900 Subject: [PATCH 258/358] Update _ja/tour/packages-and-imports.md Co-Authored-By: take2webservice --- _ja/tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index 04416a6964..89ca2a9baf 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -23,7 +23,7 @@ package users class User ``` -パッケージとScalaファイルが含まれるディレクトリは同じ名前をつける習慣があります。しかし、Scalaはファイルのレイアウトを認知できません。`package users`のsbtプロジェクトのディレクトリ構成はこのように見えます。 +パッケージとScalaファイルが含まれるディレクトリは同じ名前をつける習慣があります。しかし、Scalaはファイルのレイアウトには関知しません。`package users`を含むsbtプロジェクトのディレクトリ構成はこのようになるかもしれません。 ``` - ExampleProject From ee97eea1dd8994c71cd21c031d3b7f103a963dea Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:53:40 +0900 Subject: [PATCH 259/358] Update _ja/tour/packages-and-imports.md Co-Authored-By: take2webservice --- _ja/tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index 89ca2a9baf..d86a98a849 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -38,7 +38,7 @@ class User UserPreferences.scala - test ``` -`users`ディレクトリがどのように`scala`ディレクトリの中にあり、Scalaファイルがどのようにパッケージ内にあるのかに注意してください。パッケージ内のいずれのScalaファイルも同じパッケージ宣言がされます。他のパッケージ宣言の方法は波括弧を使います。 +`users`ディレクトリがどのように`scala`ディレクトリの中にあり、複数のScalaファイルがどのようにパッケージ内にあるのかに注意してください。パッケージ内のScalaファイルは同じパッケージ宣言を持ちます。パッケージ宣言の他の方法は波括弧を使い以下のようにします。 ``` package users { From a97b1cc3d5f1e5832e44c3c59dbe280955c81128 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:54:00 +0900 Subject: [PATCH 260/358] Update _ja/tour/packages-and-imports.md Co-Authored-By: take2webservice --- _ja/tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index d86a98a849..84c5b90691 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -50,7 +50,7 @@ package users { } } ``` -見ての通り、この方法はパッケージのネストができ、より大きいスコープとカプセル化のコントロールができます。 +見ての通り、この方法はパッケージのネストができ、スコープとカプセル化をより強くコントロールできます。 パッケージ名は全て小文字で書き、もしコードがwebサイトを持つ組織によって開発される場合、慣習として次のフォーマットであるべきです。`<トップレベルドメイン>.<ドメイン名>.<プロジェクト名>`。例えば、Googleが`SelfDrivingCar`と呼ばれるプロジェクトを持っている場合、パッケージ名はこんな風になるでしょう。 ``` From 8401486cd7206bdd3de15fbf9ff4b5caff646ad3 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:54:44 +0900 Subject: [PATCH 261/358] Update _ja/tour/packages-and-imports.md Co-Authored-By: take2webservice --- _ja/tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index 84c5b90691..88b85a81cf 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -61,7 +61,7 @@ class Lens これは次のディレクトリ構成に対応します。`SelfDrivingCar/src/main/scala/com/google/selfdrivingcar/camera/Lens.scala` ## インポート -`import`句は他パッケージのメンバー(クラス、トレイト、関数など)にアクセスするためのものです。同じパッケージのメンバーにアクセスするには`import`句は必要ありません。import句は選択的です。 +`import`句は他パッケージのメンバー(クラス、トレイト、関数など)にアクセスするためのものです。同じパッケージのメンバーにアクセスするには`import`句は必要ありません。import句は以下のどれでも使えます。 ``` import users._ // usersパッケージから全てをインポートする import users.User // クラスUserをインポートする From 2b3774f2ed9b1e55c1a6634a5f3b26fb39c3eccf Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Mon, 29 Apr 2019 15:55:09 +0900 Subject: [PATCH 262/358] Update _ja/tour/packages-and-imports.md Co-Authored-By: take2webservice --- _ja/tour/packages-and-imports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/packages-and-imports.md b/_ja/tour/packages-and-imports.md index 88b85a81cf..dd2e42f844 100644 --- a/_ja/tour/packages-and-imports.md +++ b/_ja/tour/packages-and-imports.md @@ -68,7 +68,7 @@ import users.User // クラスUserをインポートする import users.{User, UserPreferences} // 選択されたメンバーのみインポートする import users.{UserPreferences => UPrefs} // インポートし利便性のために名前を変更する ``` -ScalaのJavaと異なる利用方法の1つはインポートがどこでも使える点です。 +ScalaのJavaと異なる点の1つはインポートがどこでも使える点です。 ```tut def sqrtplus1(x: Int) = { From e87d43a49c1f27a0dc660e32836c4fba64a7013c Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 00:51:35 +0900 Subject: [PATCH 263/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index db3b6edd80..30ec8396b4 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -21,7 +21,7 @@ Scalaはパッケージ全体を通して共有される便利なコンテナと 習慣として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 -いずれのパッケージもパッケージオブジェクトを1つ持てます。パッケージオブジェクト内の全ての定義はパッケージのメンバーであるか検討されます。 +1つのパッケージはパッケージオブジェクトを1つ持てます。パッケージオブジェクト内の全ての定義はパッケージ自体のメンバーと見なされます。 以下の例を見てみましょう。まず1つのクラス`Fruit`と3つの`Fruit`オブジェクトがパッケージ`gardening.fruits`にあるとします。 From 76f268651d2f69410e65e41a416d2704d7803eec Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 00:54:15 +0900 Subject: [PATCH 264/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index 30ec8396b4..d79b78ea84 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -49,7 +49,7 @@ package object fruits { } ``` -利用方法で見た例の通り、以下のオブジェクト`PrintPlanted`は`planted`と`showFruit`をインポートします。 +利用側がどのようになるかの例としては、以下のオブジェクト`PrintPlanted`は、ワイルドカードインポートでクラス`Fruit`と全く同様に`planted`と`showFruit`をインポートしています。 それはをクラス`Fruit`をインポートするのと全く同じ方法で、パッケージgardening.fruitsでワイルドカードインポートしています。 ``` From 786f86258ea5cb1730b1829dde318700f4f150b7 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 00:54:23 +0900 Subject: [PATCH 265/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index d79b78ea84..01db9b96d1 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -50,7 +50,6 @@ package object fruits { ``` 利用側がどのようになるかの例としては、以下のオブジェクト`PrintPlanted`は、ワイルドカードインポートでクラス`Fruit`と全く同様に`planted`と`showFruit`をインポートしています。 -それはをクラス`Fruit`をインポートするのと全く同じ方法で、パッケージgardening.fruitsでワイルドカードインポートしています。 ``` // ファイル PrintPlanted.scala の中 From 0826e33140d9659a664ce7db5b2a553d86e75dd2 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 00:55:34 +0900 Subject: [PATCH 266/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index 01db9b96d1..cbaaa05572 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -63,7 +63,7 @@ object PrintPlanted { } ``` -パッケージオブジェクトは他のオブジェクトと似ています。それはビルドのために継承を利用できるという意味です。例えば、一対のトレイトの中でパッケージオブジェクトをミックスします。 +パッケージオブジェクトは他のオブジェクのように、継承を利用して構成できます。例えば、一つのパッケージオブジェクトが2つのトレイトをミックスインしている例を示します。 ``` package object fruits extends FruitAliases with FruitHelpers { From 4aaf14656fd9d7df7db27defa2f8a1e8a3836461 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 00:58:55 +0900 Subject: [PATCH 267/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index cbaaa05572..ea6475d4e7 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -16,7 +16,7 @@ previous-page: packages-and-imports Scalaはパッケージ全体を通して共有される便利なコンテナとしてパッケージオブジェクトを提供します。 パッケージオブジェクトは任意の定義を含むことができます。それは変数だけではなくメソッド定義も含みます。 -例えば、それらはパッケージ全体の型エイリアスと暗黙の変換を保有するために頻繁に使われます。 +例えば、それらはパッケージ全体で使われる型エイリアスと暗黙の変換を保有するためによく使われます。 パッケージオブジェクトはScalaクラスとトレイトさえ受け継げます。 習慣として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 From 203f6f6e7ac76ffa55dafdd577c4bcbd3ca8cbac Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:00:01 +0900 Subject: [PATCH 268/358] Update _ja/tour/package-objects.md Co-Authored-By: take2webservice --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index ea6475d4e7..9600c18e94 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -17,7 +17,7 @@ Scalaはパッケージ全体を通して共有される便利なコンテナと パッケージオブジェクトは任意の定義を含むことができます。それは変数だけではなくメソッド定義も含みます。 例えば、それらはパッケージ全体で使われる型エイリアスと暗黙の変換を保有するためによく使われます。 -パッケージオブジェクトはScalaクラスとトレイトさえ受け継げます。 +パッケージオブジェクトはScalaクラスやトレイトを継承することもできます。 習慣として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 From 0eb3e3abd07c00e479e8101aa4b207ac9c96e8ff Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:01:56 +0900 Subject: [PATCH 269/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 591177f0ba..9d06c491b8 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -15,7 +15,7 @@ topics: tuples redirect_from: "/tutorials/tour/tuples.html" --- -Scalaではタプルは決まった数の要素を含む値であり、それぞれの要素は明確な型を持ちます。 +Scalaではタプルは決まった数の要素を含む値であり、各要素はそれぞれの型を持ちます。 タプルは不変です タプルはメソッドから複数の値を返す際に特に役立ちます。 From 640559b9c15705da9d42fe741fe4a14022e36144 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:02:14 +0900 Subject: [PATCH 270/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 9d06c491b8..467cf0da99 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -27,7 +27,7 @@ val ingredient = ("Sugar" , 25) ``` ここでは`String`要素を1つと`Int`要素を1つ含むタプルを作っています。 -`ingredient`の推論型は`(String, Int)`であり、これは`Tuple2[String, Int]`の簡単な表記法です。 +推論される`ingredient`の型は`(String, Int)`であり、これは`Tuple2[String, Int]`の簡単な表記法です。 タプルを表すためには、Scalaは`Tuple2`, `Tuple3`,から `Tuple22`までのクラス群を使います。 それぞれのクラスは要素の数と同じ数の型パラメータを持ちます。 From 0c57048478110e38870c61181af25bbe1606e418 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:02:36 +0900 Subject: [PATCH 271/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 467cf0da99..ac9dd7b432 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -29,7 +29,7 @@ val ingredient = ("Sugar" , 25) 推論される`ingredient`の型は`(String, Int)`であり、これは`Tuple2[String, Int]`の簡単な表記法です。 -タプルを表すためには、Scalaは`Tuple2`, `Tuple3`,から `Tuple22`までのクラス群を使います。 +タプルを表すために、Scalaは`Tuple2`, `Tuple3` … `Tuple22`までのクラス群を使います。 それぞれのクラスは要素の数と同じ数の型パラメータを持ちます。 ## 要素へのアクセス From 45b27786bf4f1df3c76417f6351c15d9b3e36af0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:02:52 +0900 Subject: [PATCH 272/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index ac9dd7b432..699ba46b7b 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -34,7 +34,7 @@ val ingredient = ("Sugar" , 25) ## 要素へのアクセス -タプル要素へのアクセス方法の1つは位置の利用です。 +タプルの要素へのアクセス方法の1つとして、位置があります。 個々の要素は`_1`、`_2`などと名付けられます。 ```tut From 1aaaaef4cdeca62f848dacc2c0f28084f2f0a252 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:03:01 +0900 Subject: [PATCH 273/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 699ba46b7b..83f6539b8d 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -50,7 +50,7 @@ println(name) // Sugar println(quantity) // 25 ``` -ここでは`name`の型推論は`String`で、`quantity`の型推論は`Int`です。 +ここでは`name`に推論される型は`String`で、`quantity`に推論される型は`Int`です。 こちらはタプルのパターンマッチングの他の例です。 From aa3ee0d4452b5d6426db05629bd3c494a23a8ab0 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:03:22 +0900 Subject: [PATCH 274/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 83f6539b8d..654e0b29c5 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -65,7 +65,7 @@ planets.foreach{ } ``` -または、`for`の中では +また、`for`内包表記では以下のようになります。 ```tut val numPairs = List((2, 5), (3, -7), (20, 56)) From 72590202c221e4211102e5b6d859c37b034d96f7 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:03:48 +0900 Subject: [PATCH 275/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 654e0b29c5..05afebe024 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -75,5 +75,5 @@ for ((a, b) <- numPairs) { ``` ## タプルとケースクラス -ユーザーは時々、タプルとケースクラスの選択を難しいと思うかもしれません。ケースクラスには名前付き要素があります。その名前はある種のコードの可読性改善します。 +ユーザーは時々、タプルとケースクラスの選択を難しいと思うかもしれません。ケースクラスには名前付き要素があります。それらの名前によってコードの可読性を改善できる場合があります。 上記の惑星の例ではタプルを使うのではなく、`case class Planet(name: String, distance: Double)`の定義もできます。 From 32261f01f0104c5f011b462cb62d13758c4e1610 Mon Sep 17 00:00:00 2001 From: Masanori Fujita Date: Sun, 5 May 2019 01:04:19 +0900 Subject: [PATCH 276/358] Update _ja/tour/tuples.md Co-Authored-By: take2webservice --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index 05afebe024..ae14dc9923 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -76,4 +76,4 @@ for ((a, b) <- numPairs) { ## タプルとケースクラス ユーザーは時々、タプルとケースクラスの選択を難しいと思うかもしれません。ケースクラスには名前付き要素があります。それらの名前によってコードの可読性を改善できる場合があります。 -上記の惑星の例ではタプルを使うのではなく、`case class Planet(name: String, distance: Double)`の定義もできます。 +上記の惑星の例ではタプルを使うより、`case class Planet(name: String, distance: Double)`を定義したほうがいいかもしれません。 From 369dbde8ad1533e86bf3af2174a6abf11e4ab225 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 00:19:30 +0900 Subject: [PATCH 277/358] =?UTF-8?q?=E8=A8=B3=E3=81=8B=E3=82=99=E4=B8=8D?= =?UTF-8?q?=E8=87=AA=E7=84=B6=E3=81=AA=E3=81=A8=E3=81=93=E3=82=8D=E3=82=92?= =?UTF-8?q?=E8=A8=82=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/tour-of-scala.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 5d7b4f7643..c4ffda9756 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -27,55 +27,57 @@ Scalaは一般的なプログラミング方法を簡潔かつエレガントか それはオブジェクト指向言語と関数型言語の機能をスムーズに統合しています。 ## Scalaはオブジェクト指向 ## -Scalaは[全ての値がオブジェクトである](unified-types.html\) という意味では純数オブジェクト指向言語です。 +Scalaは[全ての値がオブジェクトである](unified-types.html\) という意味では純粋オブジェクト指向言語です。 型とオブジェクトの振る舞いは[クラス](classes.html\) と[トレイト](traits.html\) によって記述されます。 クラスはサブクラス化と多重継承の代わりの柔軟な[ミックスインを基にした合成](mixin-class-composition.html\) 機構により拡張されます。 ## Scalaは関数型 ## Scalaは[すべての関数が値である](unified-types.html\) という意味で関数型言語でもあります。 -Scalaは無名関数を定義するために[軽量な構文](basics.html#functions\) 提供し、 -それは[高階関数](higher-order-functions.html\) をサポートし、関数の[ネスト](nested-functions.html\) を許可し、[カリー化](multiple-parameter-lists.html\) をサポートします. -Scalaの[ケースクラス](case-classes.html\\) とその組み込みは多くのプログラミング言語で使われる[パターンマッチング](pattern-matching.html) モデル代数型をサポートします +Scalaは無名関数を定義するために[軽量な構文](basics.html#functions\)を提供し、 +[高階関数](higher-order-functions.html\) をサポートし、関数の[ネスト](nested-functions.html\) を許可し、[カリー化](multiple-parameter-lists.html\) をサポートします。 +Scalaの[ケースクラス](case-classes.html\\)と[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 [シングルトンオブジェクト](singleton-objects.html\) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 -さらに、Scalaのパターンマッチングの概念は当然のことながら、[エクストラクタオブジェクト](extractor-objects.html\) による一般的な拡張として、[シーケンスパターンを正しく無視して](regular-expression-patterns.html\) 、[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing\\) をすることまで拡張されます。 -この際、For内包表記はクエリの定式化に役立ちます。これらの機能によりWebサービスのようなアプリケーション開発の際、Scalaを理想的なものとなります。 +さらに、Scalaのパターンマッチングの概念は、[エクストラクタオブジェクト](extractor-objects.html\) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html\)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing\\) にまで拡張されています。 +(訳者註:現在のバージョンでは右無視シーケンスパターンの説明は正規表現のページから除かれています。[→古いバージョン](https://www.scala-lang.org/old/node/122)) +この文脈では、For内包表記はクエリの設計に役立ちます。 +これらの機能により、ScalaはWebサービスのようなアプリケーション開発に理想的なものとなっています。 ## Scalaは静的型付け ## -Scalaは抽象化が安全で首尾一貫した方法で使われることをコンパイル時に強制する表現型システムを備えています。 +Scalaは抽象化が安全で首尾一貫した方法で使われることをコンパイル時に強制する、表現力豊かな型システムを備えています。 特にその型システムは以下をサポートします: * [ジェネリッククラス](generic-classes.html\) * [変位指定アノテーション](variances.html\) -* [上限](upper-type-bounds.html\\) と [下限](lower-type-bounds.html\\) 型境界, +* [上限](upper-type-bounds.html\\) と [下限](lower-type-bounds.html\\) 型境界 * [内部クラス](inner-classes.html\) とオブジェクトメンバーとしての[抽象型メンバー](abstract-type-members.html\) * [複合型](compound-types.html\) * [明示的に型指定された自己参照](self-types.html\) * [暗黙パラメーター](implicit-parameters.html\\) と [暗黙の変換](implicit-conversions.html\) * [多態性メソッド](polymorphic-methods.html\) -[型インターフェイス](type-inference.html\\) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 -組み合わせることで、それらの機能はプログラミングの抽象化の安全な再利用とソフトウェアの型安全な拡張のための強力な基礎を提供します。 +[型推論](type-inference.html\\) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 +これらの機能を組み合わせることにより、プログラミングの抽象化を安全に再利用でき、ソフトウェアを型安全に拡張できる強力な基盤となります。 ## Scalaは拡張可能 ## -実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張をよく必要となります。 +実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張がよく必要となります。 Scalaはライブラリの形で新しい言語構成をスムーズに追加すことを簡単にする言語メカニズムのユニークな組み合わせを提供します。 -多くのケースで、これはマクロのようなメタプログラミングの機能を使うことなく実行することができます。例えば、 +多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 -* [暗黙のクラス](http://docs.scala-lang.org/overviews/core/implicit-classes.html\\) は既存の型に拡張メソッドを追加することを許します。 +* [暗黙のクラス](http://docs.scala-lang.org/overviews/core/implicit-classes.html\\) で既存の型に拡張メソッドを追加できます。 -* [文字列補間](/overviews/core/string-interpolation.html\\) はカスタム補間を使ってユーザー拡張可能です。 +* [文字列補間](/ja/overviews/core/string-interpolation.html\\) はカスタム補間を使ってユーザー拡張可能です。 ## Scalaの相互運用 Scalaは一般的なJava実行環境 (JRE\) と相互運用するように設計されています。 特に、主流であるオブジェクト指向のJavaプログラミング言語とのやり取りはできるだけスムーズになっています。 -ScalaではSAMs、[ラムダ](higher-order-functions.html\) 、[アノテーション](annotations.html\) 、[ジェネリクス](generic-classes.html\) のようなより新しいJavaの機能の類似物があります。 +ScalaではSAMs、[ラムダ](higher-order-functions.html\) 、[アノテーション](annotations.html\) 、[ジェネリクス](generic-classes.html\) のようなJavaの新しい機能には直接の類似物があります。 [デフォルト引数値](default-parameter-values.html\) 、[名前付きパラメータ](named-arguments.html\) -といったJavaに類似物がないScalaの機能はできるだけJavaが合理的に理解できるよう、Javaに近しい形にコンパイルされます。 +といった、Javaに類似物がないScalaの機能はできるだけ適切でJavaに近い形にコンパイルされます。 ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み\) を持っており、数千の既存の高品質なライブラリへのアクセスができます。 ## ツアーを楽しんで! From 552b87f732c23ff34c137ad954dc749d15d962ce Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 00:29:08 +0900 Subject: [PATCH 278/358] =?UTF-8?q?=E3=82=88=E3=81=8F=E3=82=88=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index c4ffda9756..56b8450148 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -61,7 +61,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ ## Scalaは拡張可能 ## -実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張がよく必要となります。 +実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張が必要となります。 Scalaはライブラリの形で新しい言語構成をスムーズに追加すことを簡単にする言語メカニズムのユニークな組み合わせを提供します。 多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 From da4d60368ad3619caedc161c36d9b0bc33a4ef10 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 00:56:44 +0900 Subject: [PATCH 279/358] =?UTF-8?q?=E5=86=97=E9=95=B7=E3=81=99=E3=81=8D?= =?UTF-8?q?=E3=82=99=E3=82=8B=E9=83=A8=E5=88=86=E3=81=A8=E3=82=B3=E3=83=B3?= =?UTF-8?q?=E3=83=92=E3=82=99=E3=83=8B=E6=95=AC=E8=AA=9E=E3=81=AE=E6=8C=87?= =?UTF-8?q?=E6=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/basics.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index a2256f4a0c..5a7851b8f2 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -75,7 +75,7 @@ val x: Int = 1 + 1 ### 変数 -再代入ができおることを除けば、変数は値と似ています。 +再代入ができることを除けば、変数は値と似ています。 `var`キーワードを使うことで、変数は定義できます。 ```tut @@ -106,13 +106,13 @@ println({ ## 関数 -関数は引数を受け取る式です。 +関数は引数を受け取る式です。 ここでは与えられた数値に1を足す無名関数(すなわち名前が無い関数)を宣言しています。 ```tut (x: Int) => x + 1 ``` -`=>` の左側は引数のリストになります。右側は引数を含む式になります。 +`=>` の左側は引数のリストです。右側は引数を含む式です。 関数には名前をつけることもできます。 @@ -190,9 +190,9 @@ class Greeter(prefix: String, suffix: String) { println(prefix + name + suffix) } ``` -`greet` メソッドの戻り値の型は`Unit`です。`Unit`は意味のある戻り値が存在しないことを意味します。 -それはJavaやC言語の`void`と似たような使われ方をします。(`void`との違いは全てのScalaの式は値をを持たなければなりませんので、 -戻り値はUnit型のシングルトンな値になります。その値には情報はありません。) +`greet` メソッドの戻り値の型は`Unit`です。`Unit`は戻り値として意味がないことを示します。 +それはJavaやC言語の`void`と似たような使われ方をします。(`void`との違いは、全てのScalaの式は値を持つ必要があるため、 +実はUnit型のシングルトンで`()`と書かれる値があります。その値には情報はありません。) `new` キーワードを使うことで、クラスのインスタンスを生成することができます。 @@ -226,7 +226,7 @@ if (point == anotherPoint) { println(point + " and " + anotherPoint + " are the same.") } else { println(point + " and " + anotherPoint + " are different.") -} // Point(1,2) と Point(1,2) は同じになります。 +} // Point(1,2) と Point(1,2) は同じです。 if (point == yetAnotherPoint) { println(point + " and " + yetAnotherPoint + " are the same.") @@ -235,7 +235,7 @@ if (point == yetAnotherPoint) { } // Point(1,2) と Point(2,2) は異なります。 ``` -ケースクラスについて紹介すべきことはたくさんあり、あなたはケースクラスが大好きになると確信しています。 +ケースクラスについて紹介すべきことはたくさんあり、あなたはケースクラスが大好きになると確信しています! それらについては[後で](case-classes.html)詳しく取り扱います。 ## オブジェクト @@ -254,7 +254,7 @@ object IdFactory { } ``` -名前を参照することでオブジェクトにアクセスすることができます。 +名前を参照してオブジェクトにアクセスすることができます。 ```tut val newId: Int = IdFactory.create() @@ -277,7 +277,7 @@ trait Greeter { } ``` -トレイトはデフォルトの実装を持つことができます。 +トレイトはデフォルトの実装を持つこともできます。 {% scalafiddle %} ```tut @@ -313,7 +313,7 @@ customGreeter.greet("Scala developer") // How are you, Scala developer? ## メインメソッド メインメソッドはプログラムの始点になります。Javaバーチャルマシーンは`main`と名付けられたメインメソッドが必要で、 -それは文字列型を含む配列を一つ引数として受け取ります。 +それは文字列の配列を一つ引数として受け取ります。 オブジェクトを使い、以下のようにメインメソッドを定義することができます。 From 09e4e2ac22f72a13da61cab43926bf88f6c5f747 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 01:12:14 +0900 Subject: [PATCH 280/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/unified-types.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md index e6edc4e377..81dcb5cea8 100644 --- a/_ja/tour/unified-types.md +++ b/_ja/tour/unified-types.md @@ -1,6 +1,6 @@ --- layout: tour -title: Unified型 +title: 統合された型 language: ja discourse: true @@ -65,20 +65,19 @@ true ## 型キャスト 値型は以下の順序でキャストできます。 -Value types can be cast in the following way: Scalaの型階層 例えば、 ```tut val x: Long = 987654321 -val y: Float = x // 9.8765434E8 (この場合精度が落ちることにが注意してください) +val y: Float = x // 9.8765434E8 (この場合精度が落ちることに注意してください) val face: Char = '☺' val number: Int = face // 9786 ``` -型変換は一方向になります。これはコンパイルができないでしょう。 +型変換は一方向です。これはコンパイルができないでしょう。 ``` val x: Long = 987654321 From fdcbbdc05f4571803705596e7323bee66f16a2f6 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 23:26:20 +0900 Subject: [PATCH 281/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/classes.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 705c6e093e..46f47a4ab4 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -53,12 +53,11 @@ println(point1) // prints (2, 3) ``` この`Point`クラスは4つのメンバーを持ちます。 変数`x` と `y` そしてメソッド `move` と `toString`です。 -多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネイチャになります。 -(シグネイチャはメソッドの名、そのメソッドに対する引数の数と型で構成されています。) +多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネチャ`(var x: Int, var y: Int)`です。 `move`メソッドは2つのInteger引数を受け取り、意味を持たないUnitの値である`()` を返します。 -これは乱暴に言えば、Javaのような言語における`void`と一致します。 +これは乱暴に言えば、Javaのような言語における`void`に対応します。 その一方で`toString`は引数を受け取りませんが、`String`の値を返します。 -なぜなら[`AnyRef`](unified-types.html)の`toString`を`toString`がオーバーライドするためで、それは`override`キーワードでタグ付けされます。 +`toString`は[`AnyRef`](unified-types.html)の`toString`をオーバーライドしているので、`override`キーワードのタグが付いています。 ## コンストラクター From 624e3f0b3fde6bf383899e7023092853d862d42e Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 23:33:57 +0900 Subject: [PATCH 282/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/tuples.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tuples.md b/_ja/tour/tuples.md index ae14dc9923..0d67f6c5c1 100644 --- a/_ja/tour/tuples.md +++ b/_ja/tour/tuples.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/tuples.html" --- Scalaではタプルは決まった数の要素を含む値であり、各要素はそれぞれの型を持ちます。 -タプルは不変です +タプルは不変です。 タプルはメソッドから複数の値を返す際に特に役立ちます。 From 0c61586f40de912b5ff01c3875adec989ab89624 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 6 May 2019 23:44:02 +0900 Subject: [PATCH 283/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index 70306c1a04..a1037a373d 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -14,7 +14,7 @@ previous-page: nested-functions redirect_from: "/tutorials/tour/multiple-parameter-lists.html" --- -メソッドは複数パラメータリストを持てます。 +メソッドは複数のパラメータリストを持てます。 # 例 From 72dae7cb483cdf286349234385eab1fd7bbee9f7 Mon Sep 17 00:00:00 2001 From: fujita Date: Wed, 8 May 2019 00:25:59 +0900 Subject: [PATCH 284/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/pattern-matching.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index a1bebfee0d..327952443a 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -49,7 +49,7 @@ def matchTest(x: Int): String = x match { matchTest(3) // many matchTest(1) // one ``` -このマッチ表現はString型を持ちます、なぜなら全てのケースはStringを返します。 +全てのケースでStringを返しているので、このマッチ式はString型を持ちます。 そのため関数`matchTest`はStringを返します。 ## ケースクラスでのマッチング @@ -66,14 +66,14 @@ case class SMS(caller: String, message: String) extends Notification case class VoiceRecording(contactName: String, link: String) extends Notification ``` -`Notification`はケースクラスで実装された`Email`、 `SMS`、 そして`VoiceRecording`3つの具体的なお知らせの種類を持つ抽象スーパークラスです。 +`Notification`は抽象スーパークラスで、ケースクラスの実装`Email`、 `SMS`、 `VoiceRecording`3つの具象クラスがあります。 今、これらのケースクラスでパターンマッチングをすることができます。 ``` def showNotification(notification: Notification): String = { notification match { - case Email(email, title, _) => - s"You got an email from $email with title: $title" + case Email(sender, title, _) => + s"You got an email from $sender with title: $title" case SMS(number, message) => s"You got an SMS from $number! Message: $message" case VoiceRecording(name, link) => @@ -87,18 +87,18 @@ println(showNotification(someSms)) // You got an SMS from 12345! Message: Are y println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123 が出力されます。 ``` -関数`showNotification`は抽象型`Notification`のパラメータとして受け取り、`Notification` の種類とマッチします(すなわち`Email`、`SMS`、または `VoiceRecording`のいずれであるかを解決します)。 -`case Email(email, title, _)` ではフィールド`email`と`title`が戻り値として使われますが、`_`を使うことでフィールド`body`は無視されます。 +関数`showNotification`は抽象型`Notification`をパラメータとして受け取り、`Notification`の型でマッチします(すなわち`Email`、`SMS`、または `VoiceRecording`のいずれであるかを解決します)。 +`case Email(sender, title, _)` ではフィールド`sender`と`title`が戻り値として使われますが、`_`を使うことでフィールド`body`は無視されます。 ## パターンガード パターンガードはケースをより特別にするために使われる簡単な真偽表現です。 -ただ`if `をパターンの後ろに追加するだけです。 +ただ`if `をパターンの後ろに追加するだけです。 ``` def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = { notification match { - case Email(email, _, _) if importantPeopleInfo.contains(email) => + case Email(sender, _, _) if importantPeopleInfo.contains(sender) => "You got an email from special someone!" case SMS(number, _) if importantPeopleInfo.contains(number) => "You got an SMS from special someone!" @@ -120,7 +120,7 @@ println(showImportantNotification(importantEmail, importantPeopleInfo)) println(showImportantNotification(importantSms, importantPeopleInfo)) ``` -`case Email(email, _, _) if importantPeopleInfo.contains(email)`では、パターンは`email`が重要な人のリストに存在して初めてマッチします。 +`case Email(sender, _, _) if importantPeopleInfo.contains(sender)`では、パターンは`sender`が重要な人のリストに存在して初めてマッチします。 ## 型のみでのマッチング @@ -145,7 +145,7 @@ def goIdle(device: Device) = device match { ## シールドクラス トレイトとクラスに`sealed`をつけると、全てのサブタイプは同一ファイル内で宣言されなければならないという意味になります。 -これは全てのサブタイプは既知であることを保証します。 +これは全てのサブタイプが既知であることを保証します。 ```tut sealed abstract class Furniture From 76fb205d1694505beba51f683083c6bf35b428e1 Mon Sep 17 00:00:00 2001 From: fujita Date: Wed, 8 May 2019 00:39:20 +0900 Subject: [PATCH 285/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/extractor-objects.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/_ja/tour/extractor-objects.md b/_ja/tour/extractor-objects.md index c9ed18fbe3..2b12283764 100644 --- a/_ja/tour/extractor-objects.md +++ b/_ja/tour/extractor-objects.md @@ -1,6 +1,6 @@ --- layout: tour -title: エクストラクタオブジェクト +title: 抽出子オブジェクト language: ja discourse: true @@ -14,7 +14,7 @@ previous-page: regular-expression-patterns redirect_from: "/tutorials/tour/extractor-objects.html" --- -エクストラクタオブジェクトは`unapply`メソッドを持つオブジェクトです。 +抽出子オブジェクトは`unapply`メソッドを持つオブジェクトです。 `apply`メソッドが引数を取り、オブジェクトを作るコンストラクタであるように、`unapply`は1つのオブジェクトを受け取り、引数を返そうとします。 これはパターンマッチングと部分関数で最も頻繁に使われます。 @@ -42,7 +42,7 @@ customer1ID match { `CustomerID("Sukyoung")`は、`CustomerID.apply("Sukyoung")`を短く書く構文です。 `case CustomerID(name) => println(name)`では、unapplyメソッドを呼んでいます。 -値を定義する文で、パターン中に新しい変数を使うことができるので、エクストラクタは変数を初期化するのに使えます。この場合unapplyメソッドが初期値を与えます。 +値を定義する文で、パターン中に新しい変数を使うことができるので、抽出子は変数を初期化するのに使えます。この場合unapplyメソッドが初期値を与えます。 ```tut val customer2ID = CustomerID("Nico") @@ -61,14 +61,12 @@ val CustomerID(name3) = "-asdfasdfasdf" ``` `unapply`の戻り値型は以下のように選ばれなければなりません。 -The return type of an `unapply` should be chosen as follows: * ただのテストであれば、`Boolean`を返します。例えば`case even()`。 * T型のサブバリュー1つを返すのであれば、`Option[T]`を返します。 * いくつかのサブバリュー`T1,...,Tn`を返したいのであれば、オプショナルタプル`Option[(T1,...,Tn)]`でグループ化します。 時々、抽出する値の数が確定せず、入力に応じて任意の数の値を返したいことがあります。 -このユースケースとして、`Option[Seq[T]]`を返す`unapplySeq`メソッドを持つエキストラクターを定義することができます。 +この場合は、`Option[Seq[T]]`を返す`unapplySeq`メソッドを持つ抽出子を定義することができます。 これらのパターンと同様の例として以下のものがあります。 -`case List(x, y, z) =>`を使って`List`を分解する。 -`case r(name, remainingFields @ _*) =>`.のように正規表現`Regex`を使って`String`を分解する。 +`case List(x, y, z) =>`を使って`List`を分解する例や`case r(name, remainingFields @ _*) =>`.のように正規表現`Regex`を使って`String`を分解する例です。 From 127a0f698701251059a6b6aabbd3d7af5aa56bce Mon Sep 17 00:00:00 2001 From: fujita Date: Thu, 9 May 2019 22:48:58 +0900 Subject: [PATCH 286/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/for-comprehensions.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 4490efa406..a661322b2a 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -17,9 +17,9 @@ redirect_from: "/tutorials/tour/for-comprehensions.html" Scalaは*シーケンス内包表記*を表現するための軽量な記法を提供します。 内含表記は`for (enumerators) yield e`という形をとります。`enumerators`はセミコロンで区切られたEnumeratorのリストを指します。 1つの*enumerator*は新しい変数を導き出すジェネレータかフィルタのどちらかです。 -内包表記は本体`e`をenumeratorsにより生成された各バインディングとして評価され、これら値の列を返します。 +内包表記はenumeratorsが生成する束縛一つ一つについて本体`e`を評価し、その値のシーケンスを返します。 -こちらは例になります。 +こちらは例です。 ```tut case class User(name: String, age: Int) @@ -54,14 +54,15 @@ foo(10, 10) foreach { ``` この例では`n == 10`で`v == 10`です。最初のイテレーションでは`i == 0`かつ`j == 0`で、`i + j != v`となるので、何も生成されません。 -`i`が`1`に増加する前に、`j`はあと9回にインクリメントされます。`if`ガードがなければ、単純に以下の内容が表示されます。 +`i`が`1`にインクリメントされるまでに、`j`はあと9回インクリメントされます。`if`ガードがなければ、単純に以下の内容が表示されます。 ``` (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 0) ... ``` -内包表記はリストに制限されまん。 +内包表記はリストだけのものではありません。 演算子`withFilter`、`map`、そして (適切な型を持った)`flatMap`をサポートした全てのデータ型はシーケンス内包表記で使うことができます。 + 内包表記の中では`yield`を省略することができます。その場合、内包表記は`Unit`を返します。 これは副作用をもたらす必要があるときに役立ちます。 こちらは先に出たプログラムと同等のものですが、`yield`を使っていません。 From 6543402afd09ecfe6be99e989da0895428f29775 Mon Sep 17 00:00:00 2001 From: fujita Date: Thu, 9 May 2019 23:00:46 +0900 Subject: [PATCH 287/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/generic-classes.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index a7c355a86a..3f21f52dfd 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/generic-classes.html" ## ジェネリッククラスの定義 ジェネリッククラスは角カッコ`[]`の中にパラメータとして型を1つ受け取ります。 -型引数の識別子として文字`A`を使う習慣がありますが、パラメータ名は任意のものを使うことができます。 +型パラメータの識別子として文字`A`を使う習慣がありますが、任意のパラメータ名を使うことができます。 ```tut class Stack[A] { private var elements: List[A] = Nil @@ -34,7 +34,7 @@ class Stack[A] { この`Stack`クラスの実装はパラメータとして任意の型`A`を受け取ります。 これはメンバーのリスト`var elements: List[A] = Nil`が型`A`の要素のみを格納できることを意味します。 手続き`def push`は型`A`のオブジェクトのみを受け取ります -(注: `elements = x :: elements`は、`x`を現在の`elements`の先頭に追加した新しいリストを`elements`に割り当て直します。)。 +(注: `elements = x :: elements`は、`x`を現在の`elements`の先頭に追加した新しいリストを`elements`に割り当て直します)。 ## 使い方 @@ -47,8 +47,7 @@ println(stack.pop) // prints 2 println(stack.pop) // prints 1 ``` インスタンス`stack`はIntのみを受け取ることができます。 -しかしながら、型がサブタイプを持つ場合、それらは以下のように通すことができます。 -The instance `stack` can only take Ints. However, if the type argument had subtypes, those could be passed in: +しかしながら、型がサブタイプを持つ場合、それらは以下のように渡すことができます。 ``` class Fruit class Apple extends Fruit @@ -62,4 +61,5 @@ stack.push(apple) stack.push(banana) ``` クラス`Apple`と`Banana`は共に`Fruit`を継承しています。そのため`Fruit`のスタックには`apple`と`banana`のインスタンスを追加できます。 -_注意: ジェネリック型のサブタイプは不変です。`Stack[Char]`型の文字スタックがあるとき、それを`Stack[Int]`型の整数スタックとしては使うことはできません。文字スタックに整数を入れることはできるので、このことは変に思えるしれません。結論としては、`B = A`の場合に限り、`Stack[A]`は`Stack[B]`の唯一のサブタイプとなります。これでは制限が強いので、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ + +_注意: ジェネリック型のサブタイプは*不変(invariant)*です。つまり`Stack[Char]`型の文字スタックがあるとき、それを`Stack[Int]`型の整数スタックとして使うことはできません。文字スタックに整数を入れることはできるので、このことは変に思えるかもしれません。結論としては、`B = A`の場合に限り、`Stack[A]`は`Stack[B]`の唯一のサブタイプとなります。これでは制限が強いので、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ From ea2aab71a69d3ffa9209fa45d40dd6ab78ecaf7b Mon Sep 17 00:00:00 2001 From: fujita Date: Sun, 19 May 2019 00:09:08 +0900 Subject: [PATCH 288/358] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=AF=E8=A8=98?= =?UTF-8?q?=E6=B3=95=E3=81=AE=E8=AA=A4=E3=82=8A=E8=A8=82=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/tour-of-scala.md | 54 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 56b8450148..2dd50d0ca9 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -19,26 +19,26 @@ redirect_from: "/tutorials/tour/tour-of-scala.html" これらはScala初心者を対象としています。 これはほんの短いツアーで、完全な言語のチュートリアルではありません。 -もしそれを望むなら、[こちらの本](/books.html\) を手に入れるか、 -[その他の解決手段](/learn.html\) での相談を検討してください。 +もしそれを望むなら、[こちらの本](/books.html) を手に入れるか、 +[その他の解決手段](/learn.html) での相談を検討してください。 ## Scalaとは? Scalaは一般的なプログラミング方法を簡潔かつエレガントかつ型安全な方法で表現するために設計されたモダンなマルチパラダイム言語です。 それはオブジェクト指向言語と関数型言語の機能をスムーズに統合しています。 ## Scalaはオブジェクト指向 ## -Scalaは[全ての値がオブジェクトである](unified-types.html\) という意味では純粋オブジェクト指向言語です。 -型とオブジェクトの振る舞いは[クラス](classes.html\) と[トレイト](traits.html\) によって記述されます。 -クラスはサブクラス化と多重継承の代わりの柔軟な[ミックスインを基にした合成](mixin-class-composition.html\) 機構により拡張されます。 +Scalaは[全ての値がオブジェクトである](unified-types.html) という意味では純粋オブジェクト指向言語です。 +型とオブジェクトの振る舞いは[クラス](classes.html) と[トレイト](traits.html) によって記述されます。 +クラスはサブクラス化と多重継承の代わりの柔軟な[ミックスインを基にした合成](mixin-class-composition.html) 機構により拡張されます。 ## Scalaは関数型 ## -Scalaは[すべての関数が値である](unified-types.html\) という意味で関数型言語でもあります。 -Scalaは無名関数を定義するために[軽量な構文](basics.html#functions\)を提供し、 -[高階関数](higher-order-functions.html\) をサポートし、関数の[ネスト](nested-functions.html\) を許可し、[カリー化](multiple-parameter-lists.html\) をサポートします。 -Scalaの[ケースクラス](case-classes.html\\)と[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 -[シングルトンオブジェクト](singleton-objects.html\) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 +Scalaは[すべての関数が値である](unified-types.html) という意味で関数型言語でもあります。 +Scalaは無名関数を定義するために[軽量な構文](basics.html#functions)を提供し、 +[高階関数](higher-order-functions.html) をサポートし、関数の[ネスト](nested-functions.html) を許可し、[カリー化](multiple-parameter-lists.html) をサポートします。 +Scalaの[ケースクラス](case-classes.html)と[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 +[シングルトンオブジェクト](singleton-objects.html) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 -さらに、Scalaのパターンマッチングの概念は、[エクストラクタオブジェクト](extractor-objects.html\) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html\)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing\\) にまで拡張されています。 +さらに、Scalaのパターンマッチングの概念は、[エクストラクタオブジェクト](extractor-objects.html) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing) にまで拡張されています。 (訳者註:現在のバージョンでは右無視シーケンスパターンの説明は正規表現のページから除かれています。[→古いバージョン](https://www.scala-lang.org/old/node/122)) この文脈では、For内包表記はクエリの設計に役立ちます。 これらの機能により、ScalaはWebサービスのようなアプリケーション開発に理想的なものとなっています。 @@ -47,16 +47,16 @@ Scalaの[ケースクラス](case-classes.html\\)と[パターンマッチング Scalaは抽象化が安全で首尾一貫した方法で使われることをコンパイル時に強制する、表現力豊かな型システムを備えています。 特にその型システムは以下をサポートします: -* [ジェネリッククラス](generic-classes.html\) -* [変位指定アノテーション](variances.html\) -* [上限](upper-type-bounds.html\\) と [下限](lower-type-bounds.html\\) 型境界 -* [内部クラス](inner-classes.html\) とオブジェクトメンバーとしての[抽象型メンバー](abstract-type-members.html\) -* [複合型](compound-types.html\) -* [明示的に型指定された自己参照](self-types.html\) -* [暗黙パラメーター](implicit-parameters.html\\) と [暗黙の変換](implicit-conversions.html\) -* [多態性メソッド](polymorphic-methods.html\) +* [ジェネリッククラス](generic-classes.html) +* [変位指定アノテーション](variances.html) +* [上限](upper-type-bounds.html) と [下限](lower-type-bounds.html) 型境界 +* [内部クラス](inner-classes.html) とオブジェクトメンバーとしての[抽象型メンバー](abstract-type-members.html) +* [複合型](compound-types.html) +* [明示的に型指定された自己参照](self-types.html) +* [暗黙パラメーター](implicit-parameters.html) と [暗黙の変換](implicit-conversions.html) +* [多態性メソッド](polymorphic-methods.html) -[型推論](type-inference.html\\) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 +[型推論](type-inference.html) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 これらの機能を組み合わせることにより、プログラミングの抽象化を安全に再利用でき、ソフトウェアを型安全に拡張できる強力な基盤となります。 ## Scalaは拡張可能 ## @@ -66,21 +66,21 @@ Scalaはライブラリの形で新しい言語構成をスムーズに追加す 多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 -* [暗黙のクラス](http://docs.scala-lang.org/overviews/core/implicit-classes.html\\) で既存の型に拡張メソッドを追加できます。 +* [暗黙のクラス](http://docs.scala-lang.org/overviews/core/implicit-classes.html) で既存の型に拡張メソッドを追加できます。 -* [文字列補間](/ja/overviews/core/string-interpolation.html\\) はカスタム補間を使ってユーザー拡張可能です。 +* [文字列補間](/ja/overviews/core/string-interpolation.html) はカスタム補間を使ってユーザー拡張可能です。 ## Scalaの相互運用 -Scalaは一般的なJava実行環境 (JRE\) と相互運用するように設計されています。 +Scalaは一般的なJava実行環境 (JRE) と相互運用するように設計されています。 特に、主流であるオブジェクト指向のJavaプログラミング言語とのやり取りはできるだけスムーズになっています。 -ScalaではSAMs、[ラムダ](higher-order-functions.html\) 、[アノテーション](annotations.html\) 、[ジェネリクス](generic-classes.html\) のようなJavaの新しい機能には直接の類似物があります。 +ScalaではSAMs、[ラムダ](higher-order-functions.html) 、[アノテーション](annotations.html) 、[ジェネリクス](generic-classes.html) のようなJavaの新しい機能には直接の類似物があります。 -[デフォルト引数値](default-parameter-values.html\) 、[名前付きパラメータ](named-arguments.html\) +[デフォルト引数値](default-parameter-values.html) 、[名前付きパラメータ](named-arguments.html) といった、Javaに類似物がないScalaの機能はできるだけ適切でJavaに近い形にコンパイルされます。 -ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み\) を持っており、数千の既存の高品質なライブラリへのアクセスができます。 +ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み) を持っており、数千の既存の高品質なライブラリへのアクセスができます。 ## ツアーを楽しんで! -もっと読むには、目次メニューの[次のページ](basics.html\) に進んでください。 +もっと読むには、目次メニューの[次のページ](basics.html) に進んでください。 From 20fd0df4c304707fc91b4fe5fa5fdc025f53176c Mon Sep 17 00:00:00 2001 From: fujita Date: Sun, 19 May 2019 00:16:45 +0900 Subject: [PATCH 289/358] =?UTF-8?q?=E4=B8=8D=E5=A4=89=E2=86=92=E9=9D=9E?= =?UTF-8?q?=E5=A4=89=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/variances.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index 4f6570d3b5..d1d8b63b1a 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -16,7 +16,7 @@ redirect_from: "/tutorials/tour/variances.html" 変位指定は複合型の間の継承関係とそれらの型パラメータ間の継承関係の相関です。 Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータの変位指定アノテーションをサポートしています。 -変位指定アノテーションにより共変、反変にでき、アノテーション無しなら不変になります。 +変位指定アノテーションにより共変、反変にでき、アノテーション無しなら非変になります。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 その一方で変位指定が無いと、クラスを抽象化して再利用しにくくなります。 @@ -24,7 +24,7 @@ Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータ ```tut class Foo[+A] // 共変クラス class Bar[-A] // 反変クラス -class Baz[A] // 不変クラス +class Baz[A] // 非変クラス ``` ### 共変 @@ -130,11 +130,11 @@ The cat's name is: Boots The animal's name is: Boots ``` -### 不変 +### 非変 -Scalaのジェネリッククラスは標準では不変です。 +Scalaのジェネリッククラスは標準では非変です。 これは共変でも反変でもないことを意味します。 -以下の例の状況では、`Container`クラスは不変です。`Container[Cat]`は`Container[Animal]`_ではなく_、逆もまた真ではありません。 +以下の例の状況では、`Container`クラスは非変です。`Container[Cat]`は`Container[Animal]`_ではなく_、逆もまた真ではありません。 ```tut class Container[A](value: A) { From 470d3dc628f706874d0665bf06160e3d5129a5e4 Mon Sep 17 00:00:00 2001 From: fujita Date: Sun, 19 May 2019 00:56:14 +0900 Subject: [PATCH 290/358] =?UTF-8?q?=E8=A8=B3=E3=81=97=E6=BC=8F=E3=82=8C?= =?UTF-8?q?=E3=81=A8=E3=81=8B=E3=80=81=E6=84=8F=E5=91=B3=E3=81=AE=E9=80=9A?= =?UTF-8?q?=E3=81=97=E3=82=99=E3=81=AB=E3=81=8F=E3=81=84=E3=81=A8=E3=81=93?= =?UTF-8?q?=E3=82=8D=E3=81=A8=E3=81=8B=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/variances.md | 44 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/_ja/tour/variances.md b/_ja/tour/variances.md index d1d8b63b1a..35f0d01de8 100644 --- a/_ja/tour/variances.md +++ b/_ja/tour/variances.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/variances.html" Scalaは[ジェネリッククラス](generic-classes.html)の型パラメータの変位指定アノテーションをサポートしています。 変位指定アノテーションにより共変、反変にでき、アノテーション無しなら非変になります。 型システム上で変位指定を利用すると複合型間の直感的な繋がりを作ることができます。 -その一方で変位指定が無いと、クラスを抽象化して再利用しにくくなります。 +もし変位指定が無ければ、クラスを抽象化して再利用しにくくなるでしょう。 ```tut @@ -30,8 +30,7 @@ class Baz[A] // 非変クラス ### 共変 ジェネリッククラスの型パラメータ`A`はアノテーション`+A`を使うと共変になります。 -ある`class List[+A]`がある時`A`を共変にすることは、2つの型`A`と`B`に対して`A`が`B`のサブタイプであることをほのめかします。 -その時、`List[A]`は`List[B]`のサブタイプとなります。 +`class List[+A]`では、`A`が共変になっているので、`A`が`B`のサブタイプであるような`A`と`B`に対して`List[A]`が`List[B]`のサブタイプであることを示します。 これによりジェネリックを利用したとても便利で直感的なサブタイプの関係を作ることができます。 このシンプルなクラス構成を考えてみましょう。 @@ -44,9 +43,9 @@ case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal ``` `Cat`と`Dog`は`Animal`のサブタイプです。 -型パラメータ`A`が共変である場合、Scalaの標準ライブラリはジェネリックイミュータブル`sealed abstract class List[+A]`クラスを持ちます。 +Scala標準ライブラリにはイミュータブルなジェネリッククラス`sealed abstract class List[+A]`があり、型パラメータ`A`が共変です。 これは`List[Cat]`は`List[Animal]`であり、`List[Dog]`も`List[Animal]`であることを意味します。 -直感的に猫のリストと犬のリストはそれれぞれ動物のリストであり、それらを`List[Animal]`で代替できると理解できます。 +猫のリストも犬のリストも動物のリストであり、どちらも`List[Animal]`の代わりにできる、というのは直感的に理解できます。 以下の例では、メソッド`printAnimalNames`は引数に動物のリストを受け取り、新しい行にそれらの名前をプリントします。 もし`List[A]`が共変でなければ、最後の2つのメソッド呼び出しはコンパイルされず、`printAnimalNames`メソッドの使い勝手はひどく制限されます。 @@ -74,11 +73,10 @@ object CovarianceTest extends App { ### 反変 -型パラメータ`A`のジェネリッククラスはアノテーション`-A`を利用することで、反変にすることができます。 -これはクラスとその型パラメータの間でサブタイプ関係を作ります。 -それは似ていますが、共変で得られるものとは反対のものになります。 +ジェネリッククラスの型パラメータ`A`はアノテーション`-A`を利用して反変にできます。 +これはクラスとその型パラメータの間で、共変と似ていますが反対の意味のサブタイプ関係を作ります。 -`class Writer[-A]`がある時、`A`を反変とすると、2つの型`A`と`B`に対し、`A`が`B`のサブタイプの場合、`Writer[B]`は`Writer[A]`のサブタイプであることを匂わせます。 +`class Writer[-A]`では`A`が反変になっているので、`A`が`B`のサブタイプであるような`A`と`B`に対し、`Writer[B]`が`Writer[A]`のサブタイプであることを示します。 先に定義された`Cat`、`Dog`、`Animal`クラスを以下の例で検討してみます。 @@ -105,7 +103,7 @@ class CatPrinter extends Printer[Cat] { そして`Printer[Animal]`はコンソールに任意の`Animal`をプリントする方法を知っています。 それは`Printer[Animal]`も任意の`Cat`をプリントする方法を知っていることを意味します。 逆の関係性は適用されません、それは`Printer[Cat]`がコンソールに任意の`Animal`をプリントする方法を知らないからです。 -そのため`Printer[Animal]`を`Printer[Cat]`代わりに使い、`Printer[A]`を反変にすることができます。 +したがって、私達は必要であれば`Printer[Animal]`を`Printer[Cat]`代わりに使うことができます。これは`Printer[A]`が反変であるからこそ可能なのです。 ```tut object ContravarianceTest extends App { @@ -134,7 +132,7 @@ The animal's name is: Boots Scalaのジェネリッククラスは標準では非変です。 これは共変でも反変でもないことを意味します。 -以下の例の状況では、`Container`クラスは非変です。`Container[Cat]`は`Container[Animal]`_ではなく_、逆もまた真ではありません。 +以下の例の状況では、`Container`クラスは非変です。`Container[Cat]`は`Container[Animal]`_ではなく_、逆もまた同様です。 ```tut class Container[A](value: A) { @@ -145,8 +143,8 @@ class Container[A](value: A) { } } ``` -`Container[Cat]`が `Container[Animal]`でもあることはは当然かもしれませんが、ミュータブルジェネリッククラスが共変であることを許すことは安全ではありません。 -このようなことが起こりえます。 +`Container[Cat]`が `Container[Animal]`でもあることは自然なように思えるかもしれませんが、ミュータブルジェネリッククラスを共変にするのは安全ではありません。 +この例では、`Container`が非変であることは非常に重要です。`Container`が仮に共変だったとするとこのようなことが起こり得ます。 ``` val catContainer: Container[Cat] = new Container(Cat("Felix")) @@ -159,7 +157,7 @@ val cat: Cat = catContainer.getValue // おっと、犬に猫に割り当てて ### 他の例 -変位指定を理解することを助けるもう一つの例はScalaの標準ライブラリの`trait Function1[-T, +R]`です。 +変位指定の理解を助けるもう一つの例はScala標準ライブラリの`trait Function1[-T, +R]`です。 `Function1`は1つのパラメータを持つ関数を表します。1つ目の型パラメータ`T`はパラメータの型を表します。 そして2つ目の型パラメータ`R`は戻り値の型を表します。 `Function1`はその引数の型に対して反変であり、戻り値の型に対して共変です。 @@ -172,17 +170,15 @@ abstract class SmallAnimal extends Animal case class Mouse(name: String) extends SmallAnimal ``` -動物の種類を受け取り、それらが食べる食料の種類を返す関数について考えましょう。 +動物の種類を受け取り、それらが食べる食料の種類を返す関数がいくつかあるとしましょう。 +もし(猫は小動物を食べるので)`Cat => SmallAnimal`が欲しい場合に代わりに`Animal => Mouse`を与えられたとしても、私たちのプログラムはまだ動きます。 +直感的に、`Cat`は`Animal`なので、`Animal => Mouse` は`Cat`を引数として受け取ります。 +そして`SmallAnimal`である`Mouse`を返します。 -仮に(猫は小動物を食べるので)`Cat => SmallAnimal`が欲しいとします。 -しかし`Animal => Mouse`を代わりに与えられたとしても、私たちのプログラムはまだ動きます。 -直感的に、`Animal => Mouse` はまだ`Cat`を引数として受け取ります。 -なぜなら`Cat`は`Animal`であり、`SmallAnimal`である`Mouse`を返すからです。 - -安全かつ陰で前者に代えて後者を用いることができるため、`Animal => Mouse`は`Cat => SmallAnimal`のサブタイプと言うことができます。 +安全に、そのままで前者に代えて後者を用いることができるため、`Animal => Mouse`は`Cat => SmallAnimal`のサブタイプと言うことができます。 ### 他の言語との比較 -変位指定はScalaに似たいくつかの言語によって他の方法でサポートされます。 -例えば、Scalaの変位指定アノテーションはC#のそれと非常に似ています。C#ではクラスの抽象性を定義する時にアノテーションが追加されます。(宣言時の変位指定) -しかしながら、Javaでは、クラスの抽象性を使う時に変位指定アノテーションが利用側のコードから与えられます。(使用時の変位指定) +Scalaに似たいくつかの言語で、変位指定はいろんな方法でサポートされています。 +例えば、Scalaの変位指定アノテーションはC#のそれと非常に似ています。C#ではクラスの抽象性を定義する時にアノテーションが追加されます(宣言時の変位指定)。 +しかしながら、Javaでは、クラスの抽象性を使う時に変位指定アノテーションが利用側のコードから与えられます(使用時の変位指定)。 From b3f69e7014b6568c797a0cbbb9b32017662c4810 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 20 May 2019 18:26:35 +0900 Subject: [PATCH 291/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/upper-type-bounds.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_ja/tour/upper-type-bounds.md b/_ja/tour/upper-type-bounds.md index 8ff0a29e8a..427dbe429f 100644 --- a/_ja/tour/upper-type-bounds.md +++ b/_ja/tour/upper-type-bounds.md @@ -14,9 +14,9 @@ previous-page: variances redirect_from: "/tutorials/tour/upper-type-bounds.html" --- -Scalaでは [型パラメータ](generic-classes.html)と[抽象型メンバー](abstract-type-members.html) は型境界により制約されることがあります。 -型境界は型変数に入れられる値を制限して、時にはそれらの型についてより多くの情報を与えます。 -_上限型境界_ `T <: A` は型変数`T`が型`A`のサブタイプであることを宣言します。 +Scalaでは [型パラメータ](generic-classes.html)と[抽象型メンバー](abstract-type-members.html) は型境界による制約をかけることができます。 +型境界は型変数に入れられる具象型を制限して、時にはそれらの型のメンバーについてより多くの情報を与えます。 +_上限型境界_ `T <: A` は型変数`T`が型`A`のサブタイプであるという宣言です。 こちらはクラス`PetContainer`の型パラメータの上限型境界を実演する例です。 @@ -52,7 +52,7 @@ val catContainer = new PetContainer[Cat](new Cat) val lionContainer = new PetContainer[Lion](new Lion) ``` `class PetContainer`は型パラメータ`P`を受け取ります。それは`Pet`のサブタイプである必要があります。 - `Dog`と`Cat`は`Pet`のサブタイプです。そのため新たな`PetContainer[Dog]`と`PetContainer[Cat]`を作ることができます。 + `Dog`と`Cat`は`Pet`のサブタイプなので、新たな`PetContainer[Dog]`と`PetContainer[Cat]`を作ることができます。 しかしながら、もし`PetContainer[Lion]`を作ろうとすると、以下のエラーが返ってきます。 `type arguments [Lion] do not conform to class PetContainer's type parameter bounds [P <: Pet]` From ed567e30c2eb221c6edfe48f9092c8621062dbb1 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 20 May 2019 18:54:23 +0900 Subject: [PATCH 292/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/lower-type-bounds.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ja/tour/lower-type-bounds.md b/_ja/tour/lower-type-bounds.md index 8fee86a2c8..d461e31e5d 100644 --- a/_ja/tour/lower-type-bounds.md +++ b/_ja/tour/lower-type-bounds.md @@ -15,7 +15,7 @@ prerequisite-knowledge: upper-type-bounds, generics, variance redirect_from: "/tutorials/tour/lower-type-bounds.html" --- - [上限型境界](upper-type-bounds.html) が型を別の型のサブタイプに制限する一方で、*下限型境界*は型が別の型のスーパータイプになるよう宣言します。表現`B >: A`はパラメータ`B`または抽象型`B`が型`A`のスーパータイプであることを表します。ほとんどのケースで`A`はそのクラスの型パラメータであり、`B`はメソッドの型パラメータになります。 + [上限型境界](upper-type-bounds.html) は型を別の型のサブタイプに制限しますが、*下限型境界*は型が別の型のスーパータイプであることを宣言します。表現`B >: A`はパラメータ`B`または抽象型`B`が型`A`のスーパータイプであることを表します。ほとんどのケースで`A`はそのクラスの型パラメータであり、`B`はメソッドの型パラメータになります。 以下はこれが役立つ場合の例です。 @@ -40,7 +40,7 @@ case class Nil[+B]() extends Node[B] { `class Node`とそのサブタイプは、`+B`とあるので、共変です。 しかしながら、このプログラムはコンパイル _されません_。`prepend`のパラメータ`elem`が、宣言時に*共* 変と宣言した型`B`になっているからです。 -なぜ通らないかというと、関数はそれらの型パラメータの中では*反*変であり、それらの結果型の中では*共*変だからです。 +なぜ通らないかというと、関数はそれらの型パラメータに対して*反*変であり、それらの結果型に対して*共*変だからです。 これを解決するためには、`prepend`のパラメータ`elem`の型の変位指定を逆転させる必要があります。 これを実現するには、下限型境界として`B`を持つ新しい型パラメータ`U`を導入します。 From 9bfa23f38c8951e1c2dfea45462a1096af397ec3 Mon Sep 17 00:00:00 2001 From: fujita Date: Mon, 20 May 2019 23:47:14 +0900 Subject: [PATCH 293/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/inner-classes.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_ja/tour/inner-classes.md b/_ja/tour/inner-classes.md index 32e253b783..4bc373d8c8 100644 --- a/_ja/tour/inner-classes.md +++ b/_ja/tour/inner-classes.md @@ -15,8 +15,8 @@ redirect_from: "/tutorials/tour/inner-classes.html" --- Scalaではクラスが他のクラスをメンバーとして保持することが可能です。 -Javaのような、内部クラスが外側のクラスのメンバーとなる言語とは対照的に、Scalaでは、そのような内部クラスは外側のオブジェクトに束縛されます。 -どのノードがなんのグラフに属しているのかを私達が混同しないように、コンパイラがコンパイル時に防いでほしいのです。 +Javaのような、内部クラスが外側のクラスのメンバーとなる言語とは対照的に、Scalaでは、内部クラスは外側のオブジェクトに束縛されます。 +どのノードがどのグラフに属しているのかを私達が混同しないように、コンパイラがコンパイル時に防いでほしいのです。 パス依存型はその解決策の1つです。 その違いを示すために、グラフデータ型の実装をさっと書きます。 @@ -40,7 +40,7 @@ class Graph { } ``` このプログラムはグラフをノードのリスト(`List[Node]`)で表現しています。いずれのノードも接続している他のノードへのリスト(`connectedNodes`)を保持します。`class Node`は `class Graph`の中にネストしているので、 _パス依存型_ です。 -そのため`connectedNodes`の中にある全てのノードは同じ`Graph`インスタンスから`newNode`を使用し作る必要があります。 +そのため`connectedNodes`の中にある全てのノードは同じ`Graph`インスタンスから`newNode`を使用して作る必要があります。 ```tut val graph1: Graph = new Graph @@ -51,7 +51,7 @@ node1.connectTo(node2) node3.connectTo(node1) ``` わかりやすくするため`node1`、`node2`、`node3`の型を`graph1.Node`と明示的に宣言しましたが、なくてもコンパイラは推論できます。 -これは`new Node`を呼んでいる`graph1.newNode`を呼び出す時、そのメソッドが`Node`のインスタンス`graph1`を使用しているからです。 +これは`new Node`を呼んでいる`graph1.newNode`を呼び出す時、メソッドが`Node`のインスタンス`graph1`を使用しているからです。 2つのグラフがあるとき、Scalaの型システムは1つのグラフの中で定義されたノードと別のグラフで定義されたノードを混ぜることを許しません。 それは別のグラフのノードは別の型を持つからです。 @@ -66,7 +66,7 @@ val graph2: Graph = new Graph val node3: graph2.Node = graph2.newNode node1.connectTo(node3) // illegal! ``` -型`graph1.Node`は`graph2.Node`とは異なります。Javaでは先のプログラム例の最後の行は正しいでしょう。 +型`graph1.Node`は`graph2.Node`とは異なります。Javaであれば先のプログラム例の最後の行は正しいでしょう。 2つのグラフのノードに対して、Javaは同じ型`Graph.Node`を指定します。つまりクラス`Graph`は`Node`の接頭辞です。 Scalaではそのような型も同様に表現することができ、`Graph#Node`と書きます。 もし他のグラフのノードに接続できるようにしたければ、以下の方法で最初のグラフ実装の定義を変える必要があります。 From 26602a09ba3c062ab9958e2f4e4234ce0ff87940 Mon Sep 17 00:00:00 2001 From: fujita Date: Sat, 25 May 2019 00:48:48 +0900 Subject: [PATCH 294/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/abstract-type-members.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index 2265487d97..e63d239baf 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -37,9 +37,9 @@ abstract class SeqBuffer extends Buffer { ``` `T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。 -この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプである必要があると明示しているからです。 +この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプであると記述しているからです。 -抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使います。 +抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使われます。 これを説明するために、今から整数のリストを参照するシーケンスバッファーを扱うプログラムを見てみます。 ```tut From b5aa7ee4effe2c3b4a85616c206246aafa2b5672 Mon Sep 17 00:00:00 2001 From: fujita Date: Sat, 25 May 2019 01:01:01 +0900 Subject: [PATCH 295/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/compound-types.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_ja/tour/compound-types.md b/_ja/tour/compound-types.md index 677871cd5d..6eeadc481d 100644 --- a/_ja/tour/compound-types.md +++ b/_ja/tour/compound-types.md @@ -13,8 +13,8 @@ previous-page: abstract-type-members redirect_from: "/tutorials/tour/compound-types.html" --- -オブジェクトの型が複数の他の型のサブタイプであることを表現する必要がある時があります。 -Scalaでは*複合型*の助けにより表現できます。複合型とはオブジェクトの型同士を重ねることです。 +ときどき、あるオブジェクトの型が、複数の他の型のサブタイプであると表現する必要が生じます。 +Scalaでは、これは*複合型*を用いて表現できます。複合型とはオブジェクトの型同士を重ねることです。 2つのトレイト`Cloneable`と`Resetable`があるとしましょう。 @@ -39,7 +39,7 @@ def cloneAndReset(obj: ?): Cloneable = { } ``` -パラメータ`obj`の型は何かという疑問が上がります。もし`Cloneable`であれば、オブジェクトを`clone`することができますが、`reset`することはできません。もし`Resetable`であれば、`reset`することができますが、`clone`の操作はできません。そのような状態で型キャストを回避するために`obj`の型を`Cloneable`と`Resetable`の両方であると指定することができます。Scalaではこの複合型は`Cloneable with Resetable`のように書くことができます。 +パラメータ`obj`の型は何かという疑問が生じます。もし`Cloneable`であれば、オブジェクトを`clone`することができますが、`reset`することはできません。もし`Resetable`であれば、`reset`することができますが、`clone`の操作はできません。そのような状態で型キャストを回避するために`obj`の型を`Cloneable`と`Resetable`の両方であると指定することができます。Scalaではこの複合型は`Cloneable with Resetable`のように書くことができます。 こちらが書き変えた関数です。 From 2fe7b5fe171949d259fbb003b2ec2aef8a2e8590 Mon Sep 17 00:00:00 2001 From: fujita Date: Sat, 25 May 2019 08:38:06 +0900 Subject: [PATCH 296/358] =?UTF-8?q?=E8=87=AA=E5=B7=B1=E5=9E=8B=E2=86=92?= =?UTF-8?q?=E8=87=AA=E5=88=86=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/self-types.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index dddf7c2750..bd23110843 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -1,6 +1,6 @@ --- layout: tour -title: 自己型 +title: 自分型 language: ja discourse: true @@ -15,13 +15,13 @@ prerequisite-knowledge: nested-classes, mixin-class-composition redirect_from: "/tutorials/tour/self-types.html" --- -自己型はたとえ直接継承していなくても、トレイトが他のトレイトにミックスインされていることを宣言する方法です。 -それはimportなしに、依存先のメンバーを利用可能とします。 +自分型は、直接継承していなくてもトレイトが他のトレイトにミックスインされていることを宣言する方法です。 +これにより依存先のメンバーをimportなしで利用できます。 -自己型は`this`の型、または`this`の別名となる別の識別子を絞り込む方法です。 +自分型は`this`、または`this`の別名となる別の識別子の型を絞り込む方法です。 その構文は普通の関数構文のように見えますが、全く異なる意味があります。 -トレイトで自己型を使うには、識別子、ミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 +トレイトで自分型を使うには、識別子、ミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 ```tut trait User { def username: String From 258400860bcd9d4ee64f56bb50af8433e4d5f4a8 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 29 May 2019 14:23:02 +0900 Subject: [PATCH 297/358] adapt changes in 9afd159f5556ad9a772e8a264e000d30d910c168 --- _ja/tour/pattern-matching.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 327952443a..999d1e7f8e 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -32,11 +32,11 @@ x match { case 0 => "zero" case 1 => "one" case 2 => "two" - case _ => "many" + case _ => "other" } ``` 上記の`val x`は0から10の間のランダムな整数です。`x`は`match`演算子の左オペランドで、右側は4つのケースを持つ式です。 -最後のケース`_`は 2より大きい全ての数字のための"全てを捕捉する"ケースです。 +最後のケース`_`は その他の取りうる整数値のための"全てを捕捉する"ケースです。 ケースは*オルタナティブ*とも呼ばれます。 マッチ式は値を持ちます。 @@ -44,9 +44,9 @@ x match { def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" - case _ => "many" + case _ => "other" } -matchTest(3) // many +matchTest(3) // other matchTest(1) // one ``` 全てのケースでStringを返しているので、このマッチ式はString型を持ちます。 From 44fa1837afe6b20ab6d133f815753612cd5fb2f4 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Wed, 29 May 2019 15:01:22 +0900 Subject: [PATCH 298/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/tour-of-scala.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 2dd50d0ca9..4b5c4a7ce9 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -33,12 +33,12 @@ Scalaは[全ての値がオブジェクトである](unified-types.html) とい ## Scalaは関数型 ## Scalaは[すべての関数が値である](unified-types.html) という意味で関数型言語でもあります。 -Scalaは無名関数を定義するために[軽量な構文](basics.html#functions)を提供し、 +Scalaは無名関数を定義するために[軽量な構文](basics.html#関数)を提供し、 [高階関数](higher-order-functions.html) をサポートし、関数の[ネスト](nested-functions.html) を許可し、[カリー化](multiple-parameter-lists.html) をサポートします。 Scalaの[ケースクラス](case-classes.html)と[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 [シングルトンオブジェクト](singleton-objects.html) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 -さらに、Scalaのパターンマッチングの概念は、[エクストラクタオブジェクト](extractor-objects.html) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing) にまで拡張されています。 +さらに、Scalaのパターンマッチングの概念は、[抽出子オブジェクト](extractor-objects.html) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing) にまで拡張されています。 (訳者註:現在のバージョンでは右無視シーケンスパターンの説明は正規表現のページから除かれています。[→古いバージョン](https://www.scala-lang.org/old/node/122)) この文脈では、For内包表記はクエリの設計に役立ちます。 これらの機能により、ScalaはWebサービスのようなアプリケーション開発に理想的なものとなっています。 @@ -62,7 +62,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ ## Scalaは拡張可能 ## 実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張が必要となります。 -Scalaはライブラリの形で新しい言語構成をスムーズに追加すことを簡単にする言語メカニズムのユニークな組み合わせを提供します。 +Scalaは言語メカニズムのユニークな組み合わせを提供します。それはライブラリの形での円滑な新しい言語構成の追加を簡単にします。 多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 @@ -76,9 +76,9 @@ Scalaは一般的なJava実行環境 (JRE) と相互運用するように設計 特に、主流であるオブジェクト指向のJavaプログラミング言語とのやり取りはできるだけスムーズになっています。 ScalaではSAMs、[ラムダ](higher-order-functions.html) 、[アノテーション](annotations.html) 、[ジェネリクス](generic-classes.html) のようなJavaの新しい機能には直接の類似物があります。 -[デフォルト引数値](default-parameter-values.html) 、[名前付きパラメータ](named-arguments.html) +[デフォルト引数](default-parameter-values.html) 、[名前付きパラメータ](named-arguments.html) といった、Javaに類似物がないScalaの機能はできるだけ適切でJavaに近い形にコンパイルされます。 -ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み) を持っており、数千の既存の高品質なライブラリへのアクセスができます。 +ScalaはJavaのような同じコンパイルモデル(分割コンパイル、動的クラス読み込み) を持っており、数千の既存の高品質なライブラリにアクセスができます。 ## ツアーを楽しんで! From 3cd5f3840983917f367d13cc65c1fec09325bbad Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 31 May 2019 16:10:27 +0900 Subject: [PATCH 299/358] =?UTF-8?q?=E3=81=A6=E3=81=AB=E3=82=92=E3=81=AF?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index e6862e489f..1cbc5c0b90 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -118,5 +118,5 @@ REPL内でコンパニオンを定義する場合は、それらを同じ行で Javaにおける`static`メンバーはScalaではコンパニオンオブジェクトの一般メンバーとして作られています。 Javaのコードからコンパニオンオブジェクトを使う場合、メンバーはコンパニオンクラス内で`static`識別子を用いて定義されます。 -これを*static forwarding*と呼ばれます。 +これは*static forwarding*と呼ばれます。 これはコンパニオンクラスを自分で定義していなかったとしても起きます。 From 83d2635d796c3c03d5b75e0a50caae46b18d2dda Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Fri, 31 May 2019 17:54:36 +0900 Subject: [PATCH 300/358] =?UTF-8?q?=E4=B8=8D=E5=A4=89=3D>=E9=9D=9E?= =?UTF-8?q?=E5=A4=89,=20=E3=82=B8=E3=82=A7=E3=83=8D=E3=83=AA=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=BF=E3=82=A4=E3=83=97=3D>=E3=82=B8=E3=82=A7?= =?UTF-8?q?=E3=83=8D=E3=83=AA=E3=83=83=E3=82=AF=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/generic-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/generic-classes.md b/_ja/tour/generic-classes.md index 3f21f52dfd..2535e31695 100644 --- a/_ja/tour/generic-classes.md +++ b/_ja/tour/generic-classes.md @@ -62,4 +62,4 @@ stack.push(banana) ``` クラス`Apple`と`Banana`は共に`Fruit`を継承しています。そのため`Fruit`のスタックには`apple`と`banana`のインスタンスを追加できます。 -_注意: ジェネリック型のサブタイプは*不変(invariant)*です。つまり`Stack[Char]`型の文字スタックがあるとき、それを`Stack[Int]`型の整数スタックとして使うことはできません。文字スタックに整数を入れることはできるので、このことは変に思えるかもしれません。結論としては、`B = A`の場合に限り、`Stack[A]`は`Stack[B]`の唯一のサブタイプとなります。これでは制限が強いので、ジェネリックタイプのサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ +_注意: ジェネリック型のサブタイプは*非変(invariant)*です。つまり`Stack[Char]`型の文字スタックがあるとき、それを`Stack[Int]`型の整数スタックとして使うことはできません。文字スタックに整数を入れることはできるので、このことは変に思えるかもしれません。結論としては、`B = A`の場合に限り、`Stack[A]`は`Stack[B]`の唯一のサブタイプとなります。これでは制限が強いので、ジェネリック型のサブタイプの振る舞いをコントロールするために、Scalaは[型引数アノテーションの仕組み](variances.html)を提供します。_ From 0495a484ed701b1578a67f67fbad13998a0c490b Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 19:38:58 +0900 Subject: [PATCH 301/358] =?UTF-8?q?=E5=88=A5=E3=81=8C=E3=81=84=E3=81=A3?= =?UTF-8?q?=E3=81=B1=E3=81=84=E3=81=82=E3=81=A3=E3=81=A6=E6=B0=97=E6=8C=81?= =?UTF-8?q?=E3=81=A1=E6=82=AA=E3=81=84=E3=81=AE=E3=81=A7=E3=80=81=E4=BB=96?= =?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/self-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/self-types.md b/_ja/tour/self-types.md index bd23110843..836e4459ec 100644 --- a/_ja/tour/self-types.md +++ b/_ja/tour/self-types.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/self-types.html" 自分型は、直接継承していなくてもトレイトが他のトレイトにミックスインされていることを宣言する方法です。 これにより依存先のメンバーをimportなしで利用できます。 -自分型は`this`、または`this`の別名となる別の識別子の型を絞り込む方法です。 +自分型は`this`、または`this`の別名となる他の識別子の型を絞り込む方法です。 その構文は普通の関数構文のように見えますが、全く異なる意味があります。 トレイトで自分型を使うには、識別子、ミックスインする他のトレイトの型、`=>`を書きます(例えば `someIdentifier: SomeOtherTrait =>`)。 From 9b6981a6d4ff7b3933e041c18b264e018cbf5751 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:12:17 +0900 Subject: [PATCH 302/358] =?UTF-8?q?=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/implicit-parameters.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index 1df511b4c9..ec321f4545 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -14,8 +14,8 @@ previous-page: self-types redirect_from: "/tutorials/tour/implicit-parameters.html" --- -メソッドは _暗黙の_ パラメータのリストを持つことができ、パラメータリストの先頭には_implicit_キーワードで印をつけます。 -もしそのパラメータリストの中のパラメータがいつものように渡らなければ、Scalaは正しい型の暗黙値を受け取ることができるかを確認し、可能であれば自動的に渡します。 +メソッドは _暗黙の_ パラメータのリストを持つことができ、パラメータリストの先頭には _implicit_ キーワードで印をつけます。 +もしそのパラメータリストの中のパラメータがいつものように渡らなければ、Scalaは正しい型の暗黙値を受け取ることができるかを確認し、可能であればそれを自動的に渡します。 Scalaがこれらのパラメータを探す場合は2つのカテゴリに分かれます。 @@ -56,7 +56,7 @@ object ImplicitTest { ``` `モノイド`はここでは`add`と呼ばれる処理を定義します。この処理は`A`のペアを結合して、別の`A`を返します。また、(特定の)`A`を作ることができる`unit`と呼ばれる処理も定義します。 -どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトは暗黙に使われうることを指し示します。 +どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトが暗黙に使われうることを指し示します。 メソッド`sum`は`List[A]`を受け取り、`A`を返します。このメソッドは初期値`A`を`unit`から受け取り、リスト中の`A`を順番に`add`メソッドで結合します。ここでパラメータ`m`をimplicitにしているのは、そのメソッドを呼び出すとき、Scalaが暗黙のパラメータ`m`として暗黙の`Monoid[A]`を見つけることができるなら、私達は`xs`パラメータを提供するだけで良いということです。 From 4e15e26b2f7331f551b30a9748b7d94b37ae315f Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:21:45 +0900 Subject: [PATCH 303/358] =?UTF-8?q?language=E5=B1=9E=E6=80=A7=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=80=81=E4=B8=8D=E8=A6=81=E3=81=AA=E3=80=8C=E3=81=A7?= =?UTF-8?q?=E3=81=99=E3=80=8D=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/polymorphic-methods.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index cdd0706dec..0033921908 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -1,6 +1,7 @@ --- layout: tour title: ポリモーフフィックメソッド +language: ja discourse: true @@ -35,4 +36,4 @@ println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) 最初の呼び出し例では、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`Int`でなければならず、戻される型は`List[Int]`となります。 -2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことを示しています。コンパイラは文脈または値引数の型に基づき、型パラメータを推論できることが多いです。この例では`"La"`が`String`なので、コンパイラは`A`が`String`にちがいないことがわかります。 +2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことを示しています。コンパイラは文脈または値引数の型に基づき、型パラメータを推論できることが多い。この例では`"La"`が`String`なので、コンパイラは`A`が`String`にちがいないことがわかります。 From 5ce49470de08493437606e35acf5bf7a4d3f136e Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:29:50 +0900 Subject: [PATCH 304/358] =?UTF-8?q?=E3=83=AF=E3=83=BC=E3=83=89=E6=BC=8F?= =?UTF-8?q?=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/type-inference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index ea919e3207..bf0eb4352b 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -47,7 +47,7 @@ val q = id(1) // type: Int ## パラメータ -コンパイラはメソッドのパラメータを決して推論しません。しかし、関数が引数として渡されている場合は、無名関数のパラメータ型を推論できます。 +コンパイラはメソッドのパラメータ型を決して推論しません。しかし、関数が引数として渡されている場合は、無名関数のパラメータ型を推論できます。 ```tut Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) From 6abc981f5766aa08899b0375eeaab60f34fde69b Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:36:48 +0900 Subject: [PATCH 305/358] =?UTF-8?q?=E3=82=AA=E3=83=9A=E3=83=AC=E3=83=BC?= =?UTF-8?q?=E3=82=BF=3D>=E6=BC=94=E7=AE=97=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/operators.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index ac09316f8c..60c5ad85aa 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -14,13 +14,13 @@ prerequisite-knowledge: case-classes redirect_from: "/tutorials/tour/operators.html" --- -Scalaではオペレーターはメソッドです。1つのパラメータを持つメソッドであれば*中置演算子*として使えます。例えば、`+`はドット記法で呼び出せます。 +Scalaでは演算子はメソッドです。パラメータを1つだけ持つメソッドであれば*中置演算子*として使えます。例えば、`+`はドット記法で呼び出せます。 ``` 10.+(1) ``` -しかしながら、中置オペレータの方が読みやすいです。 +しかしながら、中置演算子の方が読みやすいです。 ``` 10 + 1 From 0f9a11cd72f683755010e3f7d2f82e94af067244 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:45:42 +0900 Subject: [PATCH 306/358] add language:ja --- _ja/tour/by-name-parameters.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_ja/tour/by-name-parameters.md b/_ja/tour/by-name-parameters.md index 4eb343bcbc..86325795ba 100644 --- a/_ja/tour/by-name-parameters.md +++ b/_ja/tour/by-name-parameters.md @@ -1,6 +1,7 @@ --- layout: tour title: 名前渡しパラメータ +language: ja discourse: true From 1e304468df7c623d7dbf4d7ba0c55e23850dff21 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:55:19 +0900 Subject: [PATCH 307/358] =?UTF-8?q?=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/annotations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index b62132440d..be576f7893 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -29,7 +29,7 @@ object DeprecationDemo extends App { ## エンコーディングの正確性を保証するアノテーション -条件が一致しない場合にコンパイルを失敗させるアノテーションもあります。例えば、アノテーション`@tailrec`はメソッドは[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰ではメモリ使用量が一定になります。こちらは階乗を計算するメソッドの中での使われ方です。 +条件が一致しない場合にコンパイルを失敗させるアノテーションもあります。例えば、アノテーション`@tailrec`はメソッドが[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰ではメモリ使用量が一定になります。こちらは階乗を計算するメソッドの中での使われ方です。 ```tut import scala.annotation.tailrec @@ -42,7 +42,7 @@ def factorial(x: Int): Int = { factorialHelper(x, 1) } ``` -`factorialHelper`メソッドは`@tailrec`を持ちます。`@tailrec`はメソッドが実際に末尾再帰であることを保証します。もし`factorialHelper`の実装を以下のように変更すれば、失敗し +`factorialHelper`メソッドは`@tailrec`を持ちます。`@tailrec`はメソッドが実際に末尾再帰であると保証します。もし`factorialHelper`の実装を以下のように変更すれば、失敗し ``` import scala.annotation.tailrec From b58bf06e7e11105a92ff1203ebc3313e536014e1 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sun, 2 Jun 2019 20:58:07 +0900 Subject: [PATCH 308/358] add language: ja --- _ja/tour/named-arguments.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_ja/tour/named-arguments.md b/_ja/tour/named-arguments.md index 3b7e133669..b7f0ca82a2 100644 --- a/_ja/tour/named-arguments.md +++ b/_ja/tour/named-arguments.md @@ -1,6 +1,7 @@ --- layout: tour title: 名前付き引数 +language: ja discourse: true From fb364ff6e2a115277ce8807966db59dc7474597c Mon Sep 17 00:00:00 2001 From: fujita Date: Wed, 19 Jun 2019 23:24:02 +0900 Subject: [PATCH 309/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/implicit-parameters.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/_ja/tour/implicit-parameters.md b/_ja/tour/implicit-parameters.md index ec321f4545..38067f9e27 100644 --- a/_ja/tour/implicit-parameters.md +++ b/_ja/tour/implicit-parameters.md @@ -17,15 +17,15 @@ redirect_from: "/tutorials/tour/implicit-parameters.html" メソッドは _暗黙の_ パラメータのリストを持つことができ、パラメータリストの先頭には _implicit_ キーワードで印をつけます。 もしそのパラメータリストの中のパラメータがいつものように渡らなければ、Scalaは正しい型の暗黙値を受け取ることができるかを確認し、可能であればそれを自動的に渡します。 -Scalaがこれらのパラメータを探す場合は2つのカテゴリに分かれます。 +Scalaがこれらのパラメータを探す場所は2つのカテゴリに分かれます。 -* Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 +* Scalaはまず最初に暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータを探します。 * 次に、候補となる型に関連づけられた全てのコンパニオンオブジェクトの中でimplicitと宣言されているメンバーを探します。 Scalaがimplicitをどこから見つけるかについてのより詳しいガイドは[FAQ](//docs.scala-lang.org/tutorials/FAQ/finding-implicits.html)で見ることができます。 以下の例では、モノイドの`add`と`unit`の演算を使い、要素のリストの合計を計算するメソッド`sum`を定義しています。 -implicitの値がトップレベルでないことに注意してください。 +implicitの値をトップレベルには置けないことに注意してください。 ```tut abstract class Monoid[A] { @@ -54,15 +54,15 @@ object ImplicitTest { } } ``` -`モノイド`はここでは`add`と呼ばれる処理を定義します。この処理は`A`のペアを結合して、別の`A`を返します。また、(特定の)`A`を作ることができる`unit`と呼ばれる処理も定義します。 +`Monoid`はここでは`add`と呼ばれる処理を定義します。この処理は`A`のペアを結合して、別の`A`を返します。また、(特定の)`A`を作ることができる`unit`と呼ばれる処理も定義します。 -どのように暗黙のパラメータが動くかを見るには、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトが暗黙に使われうることを指し示します。 +暗黙のパラメータがどのように働くかを見るため、まずは文字列と整数のためにそれぞれモノイド`stringMonoid`と`intMonoid`を定義します。`implicit`キーワードは対応するオブジェクトが暗黙に使われうることを指し示します。 メソッド`sum`は`List[A]`を受け取り、`A`を返します。このメソッドは初期値`A`を`unit`から受け取り、リスト中の`A`を順番に`add`メソッドで結合します。ここでパラメータ`m`をimplicitにしているのは、そのメソッドを呼び出すとき、Scalaが暗黙のパラメータ`m`として暗黙の`Monoid[A]`を見つけることができるなら、私達は`xs`パラメータを提供するだけで良いということです。 -`main`メソッドでは`sum`を2回呼んでいて、`xs`パラメータだけを与えています。するとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`xs`として`List[Int]`を渡します。それは `A`が`Int`であることを意味します。暗黙のパラメータリスト`m`が省略されているので、Scalaは暗黙の`Monoid[Int]`を探します。最初の探索ルールはこうでした。 +`main`メソッドでは`sum`を2回呼んでいて、`xs`パラメータだけを渡しています。するとScalaは先に言及したスコープの中でimplicitを探します。最初の`sum`の呼び出しは`xs`として`List[Int]`を渡します。それは `A`が`Int`であることを意味します。暗黙のパラメータリスト`m`が省略されているので、Scalaは暗黙の`Monoid[Int]`を探します。最初の探索ルールはこうでした。 -> Scalaは暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータをまず最初に探します。 +> Scalaはまず最初に暗黙のパラメータブロックを持つメソッドが呼び出されている箇所で、直接(プレフィックスなしに)アクセスできる暗黙の定義と暗黙のパラメータを探します。 `intMonoid`は`main`の中で直接アクセスできる暗黙の定義です。型も一致しているので、`sum`メソッドに自動的に渡されます。 From aaf656f60d39f42e25edd07b329d913395da6858 Mon Sep 17 00:00:00 2001 From: fujita Date: Thu, 20 Jun 2019 00:18:41 +0900 Subject: [PATCH 310/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/type-inference.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/_ja/tour/type-inference.md b/_ja/tour/type-inference.md index bf0eb4352b..5a0dc8c4a7 100644 --- a/_ja/tour/type-inference.md +++ b/_ja/tour/type-inference.md @@ -32,16 +32,16 @@ def squareOf(x: Int) = x * x def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1) ``` -次のような場合は型パラメータを指定することは強制されません。[ポリモーフフィックメソッド](polymorphic-methods.html)が呼ばれる時や[ジェネリッククラス](generic-classes.html) がインスタンス化される時です。Scalaコンパイラは文脈あるいはメソッドやコンストラクタの実際の引数から、指定されていない型パラメータを推論します。 +[ポリモーフフィックメソッド](polymorphic-methods.html)が呼ばれる時や[ジェネリッククラス](generic-classes.html) がインスタンス化される場合も型パラメータの指定は強制ではありません。Scalaコンパイラは文脈あるいはメソッドやコンストラクタの実際の引数から、指定されていない型パラメータを推論します。 こちらは2つの例です。 ```tut case class MyPair[A, B](x: A, y: B); -val p = MyPair(1, "scala") // type: MyPair[Int, String] +val p = MyPair(1, "scala") // 型: MyPair[Int, String] def id[T](x: T) = x -val q = id(1) // type: Int +val q = id(1) // 型: Int ``` コンパイラは型`A`と`B`が何であるかを見つけ出すために`MyPair`の引数の型を使用します。`x`の型も同様です。 From 1cc21511905fe74c2c5aef6002943cfdc800c417 Mon Sep 17 00:00:00 2001 From: fujita Date: Thu, 20 Jun 2019 22:19:42 +0900 Subject: [PATCH 311/358] =?UTF-8?q?=E5=86=8D=E7=A2=BA=E8=AA=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/operators.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_ja/tour/operators.md b/_ja/tour/operators.md index 60c5ad85aa..b61b229558 100644 --- a/_ja/tour/operators.md +++ b/_ja/tour/operators.md @@ -28,7 +28,7 @@ Scalaでは演算子はメソッドです。パラメータを1つだけ持つ ## 演算子の定義方法と使い方 -有効な識別子であれば演算子として使用できます。これは `add`のような名前と`+`のような記号も含みます。 +有効な識別子であれば演算子として使用できます。これは `add`のような名前も`+`のような記号も含みます。 ```tut case class Vec(val x: Double, val y: Double) { def +(that: Vec) = new Vec(this.x + that.x, this.y + that.y) @@ -41,7 +41,7 @@ val vector3 = vector1 + vector2 vector3.x // 3.0 vector3.y // 3.0 ``` -クラスVecはメソッド`+`を持ち、 `vector1`と`vector2`を足しわせるのに使います。丸括弧を使えば、読みやすい構文の複雑な式を作れます。 +クラスVecはメソッド`+`を持ち、 `vector1`と`vector2`を足しわせるのに使います。丸括弧を用いて、複雑な式を読みやすい構文で作ることができます。 こちらはクラス`MyBool`の定義です。クラス`MyBool`はメソッド`and`と`or`を含みます。 ```tut From 0942e7733a707298e816edad5f9da76b07d70d78 Mon Sep 17 00:00:00 2001 From: take2 Date: Sat, 22 Jun 2019 00:01:09 +0900 Subject: [PATCH 312/358] Update _ja/tour/annotations.md typo Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/annotations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/annotations.md b/_ja/tour/annotations.md index be576f7893..88114954f1 100644 --- a/_ja/tour/annotations.md +++ b/_ja/tour/annotations.md @@ -25,7 +25,7 @@ object DeprecationDemo extends App { ``` これはコンパイルされますが、コンパイラは警告"there was one deprecation warning"を出力します。 -アノテーション句はそれに続くに最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。これらの句の順番は重要ではありません。 +アノテーション句はそれに続く最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。これらの句の順番は重要ではありません。 ## エンコーディングの正確性を保証するアノテーション From 05303f61a395c2df409ed2979a9461204902565d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sat, 22 Jun 2019 00:11:12 +0900 Subject: [PATCH 313/358] =?UTF-8?q?=E5=8E=9F=E6=96=87=E3=81=AB=E5=90=88?= =?UTF-8?q?=E3=82=8F=E3=81=9B=E4=B8=8D=E8=A6=81=E3=81=AA=E6=94=B9=E8=A1=8C?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/abstract-type-members.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/_ja/tour/abstract-type-members.md b/_ja/tour/abstract-type-members.md index e63d239baf..f5821a7b74 100644 --- a/_ja/tour/abstract-type-members.md +++ b/_ja/tour/abstract-type-members.md @@ -35,9 +35,7 @@ abstract class SeqBuffer extends Buffer { def length = element.length } ``` -`T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。 - -この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプであると記述しているからです。 +`T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプであると記述しているからです。 抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使われます。 これを説明するために、今から整数のリストを参照するシーケンスバッファーを扱うプログラムを見てみます。 From 637b23e5e8d22dbfdddf4e66b3f645719ccec2b0 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sat, 22 Jun 2019 01:01:36 +0900 Subject: [PATCH 314/358] remove untranslated file --- _ja/tour/automatic-closures.md | 61 ---------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 _ja/tour/automatic-closures.md diff --git a/_ja/tour/automatic-closures.md b/_ja/tour/automatic-closures.md deleted file mode 100644 index 37b82524ef..0000000000 --- a/_ja/tour/automatic-closures.md +++ /dev/null @@ -1,61 +0,0 @@ ---- -layout: tour -title: Automatic Type-Dependent Closure Construction - -discourse: true - -partof: scala-tour ---- - -Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evalutation). - -The following code demonstrates this mechanism: - - object TargetTest1 extends Application { - def whileLoop(cond: => Boolean)(body: => Unit): Unit = - if (cond) { - body - whileLoop(cond)(body) - } - var i = 10 - whileLoop (i > 0) { - println(i) - i -= 1 - } - } - -The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme. - -We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax). - -Here is the implementation of a loop-unless statement: - - object TargetTest2 extends Application { - def loop(body: => Unit): LoopUnlessCond = - new LoopUnlessCond(body) - protected class LoopUnlessCond(body: => Unit) { - def unless(cond: => Boolean) { - body - if (!cond) unless(cond) - } - } - var i = 10 - loop { - println("i = " + i) - i -= 1 - } unless (i == 0) - } -The `loop` function just accepts a body of a loop and returns an instance of class `LoopUnlessCond` (which encapsulates this body object). Note that the body didn't get evaluated yet. Class `LoopUnlessCond` has a method `unless` which we can use as a *infix operator*. This way, we achieve a quite natural syntax for our new loop: `loop { < stats > } unless ( < cond > )`. - -Here's the output when `TargetTest2` gets executed: - - i = 10 - i = 9 - i = 8 - i = 7 - i = 6 - i = 5 - i = 4 - i = 3 - i = 2 - i = 1 From a36eaf4e33324faaf65d9a2ee0c2e9d517209ad5 Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Sat, 22 Jun 2019 14:37:04 +0900 Subject: [PATCH 315/358] =?UTF-8?q?enumerators=E3=81=AF=E8=A4=87=E6=95=B0?= =?UTF-8?q?=E3=81=AE=E5=80=A4=E3=82=92=E7=94=9F=E6=88=90=E3=81=97=E3=81=86?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index a661322b2a..6896c73229 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -17,7 +17,7 @@ redirect_from: "/tutorials/tour/for-comprehensions.html" Scalaは*シーケンス内包表記*を表現するための軽量な記法を提供します。 内含表記は`for (enumerators) yield e`という形をとります。`enumerators`はセミコロンで区切られたEnumeratorのリストを指します。 1つの*enumerator*は新しい変数を導き出すジェネレータかフィルタのどちらかです。 -内包表記はenumeratorsが生成する束縛一つ一つについて本体`e`を評価し、その値のシーケンスを返します。 +内包表記はenumeratorsが生成する束縛一つ一つについて本体`e`を評価し、これらの値のシーケンスを返します。 こちらは例です。 From ae8150888bd4be5190dec1216ec8dbc80235809e Mon Sep 17 00:00:00 2001 From: take2 Date: Sat, 22 Jun 2019 14:43:27 +0900 Subject: [PATCH 316/358] Update _ja/tour/for-comprehensions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit operatorは「演算子」、operationは「操作」 (with the proper types)の係りを修正 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/for-comprehensions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/for-comprehensions.md b/_ja/tour/for-comprehensions.md index 6896c73229..89bce4d42a 100644 --- a/_ja/tour/for-comprehensions.md +++ b/_ja/tour/for-comprehensions.md @@ -61,7 +61,7 @@ foo(10, 10) foreach { (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) (0, 7) (0, 8) (0, 9) (1, 0) ... ``` 内包表記はリストだけのものではありません。 -演算子`withFilter`、`map`、そして (適切な型を持った)`flatMap`をサポートした全てのデータ型はシーケンス内包表記で使うことができます。 +操作 `withFilter`、`map`、`flatMap`を(適切な型で)サポートする全てのデータ型は、シーケンス内包表記で使うことができます。 内包表記の中では`yield`を省略することができます。その場合、内包表記は`Unit`を返します。 これは副作用をもたらす必要があるときに役立ちます。 From 9ae2596ef4b56b42cd98d1b3d35b73e99ec7558f Mon Sep 17 00:00:00 2001 From: take2 Date: Sat, 22 Jun 2019 14:47:21 +0900 Subject: [PATCH 317/358] Update _ja/tour/higher-order-functions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 「just」の係りが不適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index 337bb48679..f6a0145f55 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -42,7 +42,7 @@ val newSalaries = salaries.map(x => x * 2) // List(40000, 140000, 80000) val salaries = Seq(20000, 70000, 40000) val newSalaries = salaries.map(_ * 2) ``` -Scalaコンパイラはパラメーターの型を(Intが1つだけと)既に知っているため、関数の右側だけを提供する必要があります。 +Scalaコンパイラはパラメーターの型を(Intが1つだけと)既に知っているため、関数の右側を提供するだけでよいです。 唯一の注意点はパラメータ名の代わりに`_`を使う必要があるということです(先の例では`x`でした)。 ## メソッドを関数に強制変換 From 53484c7710ffa8c82c6cbf568f7449eb37a98a86 Mon Sep 17 00:00:00 2001 From: take2 Date: Sat, 22 Jun 2019 14:48:05 +0900 Subject: [PATCH 318/358] Update _ja/tour/higher-order-functions.md typo Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index f6a0145f55..da6025380f 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -56,7 +56,7 @@ case class WeeklyWeatherForecast(temperatures: Seq[Double]) { } ``` ここで、メソッド`convertCtoF`が`forecastInFahrenheit`に渡されています。 -これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであること保証された名前になります)に変換することで実現します。 +これはコンパイラが`convertCtoF`を関数`x => convertCtoF(x)`(注意点:`x`はスコープ内でユニークであることが保証された名前になります)に変換することで実現します。 ## 関数を受け取る関数 高階関数を使う理由の1つは余分なコードを削減することです。 From 1f060b96a1e5fb2d916e6dbfad0677f6103e633a Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:08:13 +0900 Subject: [PATCH 319/358] Update _ja/tour/implicit-conversions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主部の明確化 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 4a328829c6..040fb30fd7 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -41,7 +41,7 @@ implicit def list2ordered[A](x: List[A]) def compare(that: List[A]): Int = 1 } ``` -暗黙にインポートされているオブジェクト`scala.Predef`は頻繁に使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)といくつかの暗黙の型変換を宣言しています。 +暗黙にインポートされているオブジェクト`scala.Predef`は、頻繁に使われる型(例えば`scala.collection.immutable.Map`は`Map`と別名づけられます)とメソッド(例えば`assert`)といくつかの暗黙の型変換を宣言しています。 例えば、`java.lang.Integer`を受け取るようなJavaのメソッドを呼び出す時、自由に`scala.Int`を代わりに渡すことができます。それはPredefオブジェクトが以下の暗黙の変換をを含んでいるからです。 From 3a838fd2d2cb52697aa19ff1835461f09b89d0fa Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:10:19 +0900 Subject: [PATCH 320/358] Update _ja/tour/implicit-conversions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 「無差別に使う」はあまり使わない表現なので修正 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 040fb30fd7..71e37befcc 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -52,7 +52,7 @@ implicit def int2Integer(x: Int) = java.lang.Integer.valueOf(x) ``` -暗黙の変換は無差別に使うと落とし穴になり得るため、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 +暗黙の変換は見境なく使われると落とし穴になり得るため、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 警告をオフにするには、次のいずれかの操作を行います。 From f601b5fc9e133038b2ffc0bd4a6b212858f7cf4a Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:12:32 +0900 Subject: [PATCH 321/358] Update _ja/tour/implicit-conversions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 対象が人の場合「操作を行う」は不適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/implicit-conversions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/implicit-conversions.md b/_ja/tour/implicit-conversions.md index 71e37befcc..386ed3c500 100644 --- a/_ja/tour/implicit-conversions.md +++ b/_ja/tour/implicit-conversions.md @@ -54,7 +54,7 @@ implicit def int2Integer(x: Int) = 暗黙の変換は見境なく使われると落とし穴になり得るため、暗黙の変換の定義をコンパイルしている時にコンパイラは警告を出します。 -警告をオフにするには、次のいずれかの操作を行います。 +警告をオフにするには、次のいずれかの措置を講じてください。 * 暗黙の変換定義のスコープに`scala.language.implicitConversions`をインポートする。 * コンパイラを`-language:implicitConversions`をつけて起動する From 779d899c9e92be9a90f1179dd6d2c7acac098ad5 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:19:02 +0900 Subject: [PATCH 322/358] Update _ja/tour/mixin-class-composition.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit respectivelyの係り方はsupeclassに対する、extends,mixi-inに対するwith Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/mixin-class-composition.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/mixin-class-composition.md b/_ja/tour/mixin-class-composition.md index 8ec544930e..9ec9fd65f4 100644 --- a/_ja/tour/mixin-class-composition.md +++ b/_ja/tour/mixin-class-composition.md @@ -33,7 +33,7 @@ println(d.message) // I'm an instance of class B println(d.loudMessage) // I'M AN INSTANCE OF CLASS B ``` クラス`D`は スーパークラスを`B` とし、 ミックスイン`C`を持ちます。 -クラスは1つだけしかスーパークラスを持つことができませんが、ミックスインは(`extends`とそれぞれに応じた`with`キーワードを利用し)複数持つことができます。 +クラスは1つだけしかスーパークラスを持つことができませんが、ミックスインは複数持つことができます(キーワードはそれぞれ`extends`と`with`を使います)。 ミックスインとスーパークラスは同じスーパータイプを持つことができます。 それでは抽象クラスから始まる興味深い例を見てみましょう。 From 2f5b43d6e2571d720ab687e76a5e18239ce560cb Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:19:32 +0900 Subject: [PATCH 323/358] Update _ja/tour/multiple-parameter-lists.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原文側の変更に対応 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index a1037a373d..d2cec67fe4 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -18,7 +18,7 @@ redirect_from: "/tutorials/tour/multiple-parameter-lists.html" # 例 -こちらはScalaのコレクションの[Traversable](/overviews/collections/trait-traversable.html) トレイトで定義されている実例です。 +こちらはScalaのコレクションAPIの `TraversableOnce`トレイトで定義されている実例です。 ``` def foldLeft[B](z: B)(op: (B, A) => B): B From 83d164c12a62569da89aa21a0898345e273e13b6 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:24:29 +0900 Subject: [PATCH 324/358] Update _ja/tour/multiple-parameter-lists.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 句読点を入れて、各要素を明確にする Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/multiple-parameter-lists.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/multiple-parameter-lists.md b/_ja/tour/multiple-parameter-lists.md index d2cec67fe4..a35bccf615 100644 --- a/_ja/tour/multiple-parameter-lists.md +++ b/_ja/tour/multiple-parameter-lists.md @@ -23,7 +23,7 @@ redirect_from: "/tutorials/tour/multiple-parameter-lists.html" ``` def foldLeft[B](z: B)(op: (B, A) => B): B ``` -`foldLeft`は初期値`z`とこのコレクションの全ての要素に対し左から右に2つのパラメータを取る関数`op`を適用していきます。 +`foldLeft`は、2つのパラメータを取る関数`op`を、初期値`z`とこのコレクションの全要素に対して左から右に適用していきます。 以下はその使い方の例です。 初期値0から始まり、`foldLeft`はここではリスト内の各要素とその一つ前の累積値に関数`(m, n) => m + n`を適用します。 From aa23d5d1dd87dc69c1f47f5a15e763db4e70343a Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:25:32 +0900 Subject: [PATCH 325/358] Update _ja/tour/nested-functions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ネストの訳語を追加 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/nested-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/nested-functions.md b/_ja/tour/nested-functions.md index c8c4c73b18..05a57a2fae 100644 --- a/_ja/tour/nested-functions.md +++ b/_ja/tour/nested-functions.md @@ -14,7 +14,7 @@ previous-page: higher-order-functions redirect_from: "/tutorials/tour/nested-functions.html" --- -Scalaではメソッドの定義をネストすることができます。 +Scalaではメソッドの定義をネストする(_訳注:入れ子にする_)ことができます。 以下のコードは与えられた数値の階乗を計算するための`factorial`メソッドを提供します。 {% scalafiddle %} From 04a2a94664375bd1d28b373b409561ad3d8fc7a7 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:29:18 +0900 Subject: [PATCH 326/358] Update _ja/tour/package-objects.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit not justの係る対象が不適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index 9600c18e94..3d4c8e6fdd 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -15,7 +15,7 @@ previous-page: packages-and-imports Scalaはパッケージ全体を通して共有される便利なコンテナとしてパッケージオブジェクトを提供します。 -パッケージオブジェクトは任意の定義を含むことができます。それは変数だけではなくメソッド定義も含みます。 +パッケージオブジェクトは、変数やメソッド定義だけでなく、任意の定義を含むことができます。 例えば、それらはパッケージ全体で使われる型エイリアスと暗黙の変換を保有するためによく使われます。 パッケージオブジェクトはScalaクラスやトレイトを継承することもできます。 From c6f7ce24e2386f0fe1a1e1e0bf5d534ee163a880 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:31:08 +0900 Subject: [PATCH 327/358] Update _ja/tour/package-objects.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 対象が集団なので慣習の方が適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index 3d4c8e6fdd..bbc317294e 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -19,7 +19,7 @@ Scalaはパッケージ全体を通して共有される便利なコンテナと 例えば、それらはパッケージ全体で使われる型エイリアスと暗黙の変換を保有するためによく使われます。 パッケージオブジェクトはScalaクラスやトレイトを継承することもできます。 -習慣として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 +慣習として、パッケージオブジェクトのソースコードは通常`package.scala`という名のソースファイルに設置されます。 1つのパッケージはパッケージオブジェクトを1つ持てます。パッケージオブジェクト内の全ての定義はパッケージ自体のメンバーと見なされます。 From 9fa868bdbd6b2907e2360d128f9c3baacdf595fb Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:31:38 +0900 Subject: [PATCH 328/358] Update _ja/tour/package-objects.md typo Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/package-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/package-objects.md b/_ja/tour/package-objects.md index bbc317294e..fd7eaced57 100644 --- a/_ja/tour/package-objects.md +++ b/_ja/tour/package-objects.md @@ -63,7 +63,7 @@ object PrintPlanted { } ``` -パッケージオブジェクトは他のオブジェクのように、継承を利用して構成できます。例えば、一つのパッケージオブジェクトが2つのトレイトをミックスインしている例を示します。 +パッケージオブジェクトは他のオブジェクトのように、継承を利用して構成できます。例えば、一つのパッケージオブジェクトが2つのトレイトをミックスインしている例を示します。 ``` package object fruits extends FruitAliases with FruitHelpers { From c13f87c14b347fb9d94bd7ee4445e3a0202b9f24 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:35:12 +0900 Subject: [PATCH 329/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check~の文章が冗長なので調整 mechanism機能より仕組みの方が適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 999d1e7f8e..364016a0ad 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -16,7 +16,7 @@ prerequisite-knowledge: case-classes, string-interpolation, subtyping redirect_from: "/tutorials/tour/pattern-matching.html" --- -パターンマッチングはパターンに対応させて値をチェックするための機能です。 +パターンマッチングは値をパターンに照合するための仕組みです。 マッチに成功すれば、一つの値をその構成要素のパーツに分解することもできます。 Javaの`switch`文の強化バージョンで、if/else文の連続の代わりとして同様に使うことができます。 From c063377ec95b9d745876678bc1fb6c51a53b8b47 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:36:12 +0900 Subject: [PATCH 330/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit at leastは数量を表すので、少なくともの方が適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 364016a0ad..2e13f3f9aa 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -22,7 +22,7 @@ Javaの`switch`文の強化バージョンで、if/else文の連続の代わり ## 構文 -マッチ式は値、キーワード`match`と最低でも1つの`case`句を持ちます。 +マッチ式は値、キーワード`match`と少なくとも1つの`case`句を持ちます。 ```tut import scala.util.Random From 806b83917f7555fef3d78f082138ab82ddf6a8f3 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:39:28 +0900 Subject: [PATCH 331/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit justは~だけと訳すのみでOK Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 2e13f3f9aa..2b0fb4e232 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -92,7 +92,7 @@ println(showNotification(someVoiceRecording)) // you received a Voice Recording ## パターンガード パターンガードはケースをより特別にするために使われる簡単な真偽表現です。 -ただ`if `をパターンの後ろに追加するだけです。 +`if `をパターンの後ろに追加するだけです。 ``` From cec1bf1aee5ed81c414bb3bbba2d1d2811a76831 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:40:57 +0900 Subject: [PATCH 332/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit specificは「特別に」より「具体的に」が適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 2b0fb4e232..96b51c0b6f 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -91,7 +91,7 @@ println(showNotification(someVoiceRecording)) // you received a Voice Recording `case Email(sender, title, _)` ではフィールド`sender`と`title`が戻り値として使われますが、`_`を使うことでフィールド`body`は無視されます。 ## パターンガード -パターンガードはケースをより特別にするために使われる簡単な真偽表現です。 +パターンガードはケースをより具体的にするために使われる簡単な真偽表現です。 `if `をパターンの後ろに追加するだけです。 ``` From c537688a67bf390ba8cd8020524e7141ad72e175 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:49:50 +0900 Subject: [PATCH 333/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a method on the patternはパターン内の処理よりも、パターンで利用できるメソッドの意味合いが強い Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 96b51c0b6f..50fe69f345 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -140,7 +140,7 @@ def goIdle(device: Device) = device match { } ``` `def goIdle`は`Device`のタイプによって異なる振る舞いをします。 -これはケースがパターン内でメソッドを呼び出す必要がある時に役立ちます。 +これはケースがそのパターンのメソッドを呼び出す必要がある時に役立ちます。 ケースの識別子には型の最初の一文字(この場合に`p`と`c`)を利用する慣習があります。 ## シールドクラス From 1e875540907f0753b8e7ff9924e3e4dc683a96b6 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:51:14 +0900 Subject: [PATCH 334/358] Update _ja/tour/pattern-matching.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit typeは型に統一 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/pattern-matching.md b/_ja/tour/pattern-matching.md index 50fe69f345..d44d13e41b 100644 --- a/_ja/tour/pattern-matching.md +++ b/_ja/tour/pattern-matching.md @@ -139,7 +139,7 @@ def goIdle(device: Device) = device match { case c: Computer => c.screenSaverOn } ``` -`def goIdle`は`Device`のタイプによって異なる振る舞いをします。 +`def goIdle`は`Device`の型によって異なる振る舞いをします。 これはケースがそのパターンのメソッドを呼び出す必要がある時に役立ちます。 ケースの識別子には型の最初の一文字(この場合に`p`と`c`)を利用する慣習があります。 From 34925ab9af0cb8dacf524fc93825182318dc14a0 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:51:31 +0900 Subject: [PATCH 335/358] Update _ja/tour/polymorphic-methods.md typo Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 0033921908..9217d43dea 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -1,6 +1,6 @@ --- layout: tour -title: ポリモーフフィックメソッド +title: ポリモーフィックメソッド language: ja discourse: true From f89ee26ba456e141e44434bd3712c293ed1cb64c Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:52:09 +0900 Subject: [PATCH 336/358] Update _ja/tour/polymorphic-methods.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit です・ます調に統一 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/polymorphic-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/polymorphic-methods.md b/_ja/tour/polymorphic-methods.md index 9217d43dea..0770657246 100644 --- a/_ja/tour/polymorphic-methods.md +++ b/_ja/tour/polymorphic-methods.md @@ -36,4 +36,4 @@ println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) 最初の呼び出し例では、`[Int]`と書いて明示的に型引数を渡しています。そのため最初の引数は`Int`でなければならず、戻される型は`List[Int]`となります。 -2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことを示しています。コンパイラは文脈または値引数の型に基づき、型パラメータを推論できることが多い。この例では`"La"`が`String`なので、コンパイラは`A`が`String`にちがいないことがわかります。 +2つ目の呼び出し例では必ずしも明示的に型パラメータを渡す必要がないことを示しています。コンパイラは文脈または値引数の型に基づき、型パラメータを推論できることが多いです。この例では`"La"`が`String`なので、コンパイラは`A`が`String`に違いないことが分かります。 From e23dad7bf14e054e3087ca1ebb192a8ddcbc12f5 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:53:13 +0900 Subject: [PATCH 337/358] Update _ja/tour/regular-expression-patterns.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 出力例の一部が欠落 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/regular-expression-patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/regular-expression-patterns.md b/_ja/tour/regular-expression-patterns.md index 9596579143..21068d1de1 100644 --- a/_ja/tour/regular-expression-patterns.md +++ b/_ja/tour/regular-expression-patterns.md @@ -53,7 +53,7 @@ for (patternMatch <- keyValPattern.findAllMatchIn(input)) それぞれのマッチはサブマッチのグループを持ちます。こちらが出力結果です。 ``` key: background-color value: #A03300 -key: background-image value: url(img +key: background-image value: url(img/header100.png) key: background-position value: top center key: background-repeat value: repeat-x key: background-size value: 2160px 108px From ad6353a6b73eb0efed8707305b91666c54301469 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 20:55:02 +0900 Subject: [PATCH 338/358] Update _ja/tour/singleton-objects.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原文がmustなので、必然性をより強くしました Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index 1cbc5c0b90..ce49785344 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -110,7 +110,7 @@ scalaCenterEmail match { `object Email`はファクトリー`fromString`を持ち、Stringから`Email`インスタンスを作ります。 パースエラーの場合も考えて、返り値の型を`Option[Email]`とします。 -注意:クラスまたはオブジェクトがコンパニオンを持つ場合、クラス、オブジェクトの両方は同じファイルの中に定義されている必要があります。 +注意:クラスまたはオブジェクトがコンパニオンを持つ場合、クラス、オブジェクトの両方は同じファイルの中に定義されていなければなりません。 REPL内でコンパニオンを定義する場合は、それらを同じ行で定義するか、`:paste`モードに入ります。 ## Javaプログラマのための注意事項 ## From c62cb06a6c8cb9ad3eede4dc340e6cb58ed3a254 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:01:13 +0900 Subject: [PATCH 339/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 置き換わる対象が不明瞭なので修正 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 4b5c4a7ce9..3565552e70 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -29,7 +29,7 @@ Scalaは一般的なプログラミング方法を簡潔かつエレガントか ## Scalaはオブジェクト指向 ## Scalaは[全ての値がオブジェクトである](unified-types.html) という意味では純粋オブジェクト指向言語です。 型とオブジェクトの振る舞いは[クラス](classes.html) と[トレイト](traits.html) によって記述されます。 -クラスはサブクラス化と多重継承の代わりの柔軟な[ミックスインを基にした合成](mixin-class-composition.html) 機構により拡張されます。 +クラスはサブクラス化と、多重継承を巧みに置き換える柔軟な[ミックスインを基にした合成](mixin-class-composition.html) 機構により拡張されます。 ## Scalaは関数型 ## Scalaは[すべての関数が値である](unified-types.html) という意味で関数型言語でもあります。 From 4daa0b566e3d57cc2e20085dc73fb5b89f6991bc Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:03:26 +0900 Subject: [PATCH 340/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 区切りを入れて読みやすさを改善 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 3565552e70..22020668f9 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -56,7 +56,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ * [暗黙パラメーター](implicit-parameters.html) と [暗黙の変換](implicit-conversions.html) * [多態性メソッド](polymorphic-methods.html) -[型推論](type-inference.html) はユーザーはコードに冗長な型情報の注釈をつける必要はないことを意味します。 +[型推論](type-inference.html) は、ユーザーがコードに冗長な型情報の注釈をつける必要はないことを意味します。 これらの機能を組み合わせることにより、プログラミングの抽象化を安全に再利用でき、ソフトウェアを型安全に拡張できる強力な基盤となります。 ## Scalaは拡張可能 ## From d111112f7400c9372ad4a5af2c95a24e7d7f7332 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:06:21 +0900 Subject: [PATCH 341/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ここでは in practice は「実際に」より「実際問題として」の方が適切 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 22020668f9..2eafbae9b4 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -61,7 +61,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ ## Scalaは拡張可能 ## -実際に、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張が必要となります。 +実際問題として、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張が必要となります。 Scalaは言語メカニズムのユニークな組み合わせを提供します。それはライブラリの形での円滑な新しい言語構成の追加を簡単にします。 多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 From 69f6ea04c1422249e9c0e2b2c508e1ec0f68ab4e Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:10:47 +0900 Subject: [PATCH 342/358] Update _ja/tour/traits.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 言語とバージョン番号の間に空白を補う Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/traits.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/traits.md b/_ja/tour/traits.md index 190e81db66..1ea0b11809 100644 --- a/_ja/tour/traits.md +++ b/_ja/tour/traits.md @@ -16,7 +16,7 @@ prerequisite-knowledge: expressions, classes, generics, objects, companion-objec redirect_from: "/tutorials/tour/traits.html" --- -トレイトはクラス間でインターフェースとフィールドを共有するために使います。それらはJava8のインターフェースと似ています。 +トレイトはクラス間でインターフェースとフィールドを共有するために使います。それらはJava 8のインターフェースと似ています。 クラスとオブジェクトはトレイトを継承することができますが、トレイトはインスタンス化ができません、したがってパラメータを持ちません。 ## トレイトを定義する From 45b232fe282234fd8cbab4a56a3cbe82b3c270fc Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:12:33 +0900 Subject: [PATCH 343/358] Update _ja/tour/unified-types.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不要な改行を削除 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/unified-types.md | 1 - 1 file changed, 1 deletion(-) diff --git a/_ja/tour/unified-types.md b/_ja/tour/unified-types.md index 81dcb5cea8..98482b1c95 100644 --- a/_ja/tour/unified-types.md +++ b/_ja/tour/unified-types.md @@ -28,7 +28,6 @@ Anyは `equals`、` hashCode`、そして `toString`のようないくつかの `AnyVal` は値型に相当します。 事前に定義された9つの値型が存在し、それら`Double`, `Float`, `Long`, `Int`, `Short`, `Byte`, `Char`, `Unit`,`Boolean`は null非許容です。 - `Unit`は意味のある情報をもたない値型です。`Unit`型のインスタンスはただ1つだけあり、`()`というリテラルで宣言することができます。 全ての関数は必ず何かを返さなければなりません。そのため`Unit`は戻り値の型として時々役立ちます。 From 5480bf70eb46434065da30460fc5e83c7566a64d Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:16:29 +0900 Subject: [PATCH 344/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主題である言語拡張にフォーカスされるよう語順を変更 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 2eafbae9b4..b23aa5a773 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -62,7 +62,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ ## Scalaは拡張可能 ## 実際問題として、ドメイン固有のアプリケーションの開発ではよくドメイン固有の言語拡張が必要となります。 -Scalaは言語メカニズムのユニークな組み合わせを提供します。それはライブラリの形での円滑な新しい言語構成の追加を簡単にします。 +Scalaは言語メカニズムのユニークな組み合わせを提供します。それにより、新しい言語構成要素をライブラリという形で円滑に追加することを簡単にします。 多くのケースで、これはマクロのようなメタプログラミングの機能を使わずに実現できます。例えば、 From b9de5c131245260864b0454018e3515a691cbb42 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:19:56 +0900 Subject: [PATCH 345/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit polymorphicはポリモーフィックに統一 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index b23aa5a773..748c40f0d4 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -54,7 +54,7 @@ Scalaは抽象化が安全で首尾一貫した方法で使われることをコ * [複合型](compound-types.html) * [明示的に型指定された自己参照](self-types.html) * [暗黙パラメーター](implicit-parameters.html) と [暗黙の変換](implicit-conversions.html) -* [多態性メソッド](polymorphic-methods.html) +* [ポリモーフィックメソッド](polymorphic-methods.html) [型推論](type-inference.html) は、ユーザーがコードに冗長な型情報の注釈をつける必要はないことを意味します。 これらの機能を組み合わせることにより、プログラミングの抽象化を安全に再利用でき、ソフトウェアを型安全に拡張できる強力な基盤となります。 From a9f8bafd987e6184c427824e4eb16b18b0464c3f Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:21:11 +0900 Subject: [PATCH 346/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit built-inの対象が明確になるよう「には」と意訳 Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 748c40f0d4..6fad8d68ea 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -35,7 +35,7 @@ Scalaは[全ての値がオブジェクトである](unified-types.html) とい Scalaは[すべての関数が値である](unified-types.html) という意味で関数型言語でもあります。 Scalaは無名関数を定義するために[軽量な構文](basics.html#関数)を提供し、 [高階関数](higher-order-functions.html) をサポートし、関数の[ネスト](nested-functions.html) を許可し、[カリー化](multiple-parameter-lists.html) をサポートします。 -Scalaの[ケースクラス](case-classes.html)と[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 +Scalaの[ケースクラス](case-classes.html)には[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 [シングルトンオブジェクト](singleton-objects.html) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 さらに、Scalaのパターンマッチングの概念は、[抽出子オブジェクト](extractor-objects.html) による一般的な拡張として、[右無視シーケンスパターン](regular-expression-patterns.html)の働きにより、自然に[XMLデータの処理](https://github.com/scala/scala-xml/wiki/XML-Processing) にまで拡張されています。 From 53b104d3a17a7452daad9497dca731d9228b1d73 Mon Sep 17 00:00:00 2001 From: take2 Date: Mon, 24 Jun 2019 21:26:04 +0900 Subject: [PATCH 347/358] Update _ja/tour/singleton-objects.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit is nested inは構造を明確にするため、素直に「ネストされている」と訳す バッククオートで囲まれたobjectはコード片を示しているので、英字にする Co-Authored-By: TATSUNO Yasuhiro --- _ja/tour/singleton-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/singleton-objects.md b/_ja/tour/singleton-objects.md index ce49785344..e920e884c9 100644 --- a/_ja/tour/singleton-objects.md +++ b/_ja/tour/singleton-objects.md @@ -54,7 +54,7 @@ import文`import logging.Logger.info`により、`info`メソッドが見える import文には取り込むシンボルへの"変動しないパス"が必要であり、オブジェクトは変動しないパスとなります。 -注意:`オブジェクト`がトップレベルではないが、他のクラスやオブジェクトを必要とする時、オブジェクトは他のメンバーのように"経路依存性"があります。 +注意:`object`がトップレベルではなく他のクラスやオブジェクトにネストされている時、そのオブジェクトは他のメンバーのように"経路依存性"があります。 これは2種類の飲み物`class 牛乳`と`class オレンジジュース`が与えられた場合、クラスメンバーの`object 栄養素`はそれが属するインスタンス、すなわち牛乳またはオレンジジュースのいずれかに依存することを意味します。 `milk.NutritionInfo`は`oj.NutritionInfo`とは全く異なります。 From 9fe0f2acd2d5c379c7225babb4db68419eeda95d Mon Sep 17 00:00:00 2001 From: Taketo Iseki Date: Mon, 24 Jun 2019 21:40:47 +0900 Subject: [PATCH 348/358] add untranslated file to compile --- _ja/tour/automatic-closures.md | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 _ja/tour/automatic-closures.md diff --git a/_ja/tour/automatic-closures.md b/_ja/tour/automatic-closures.md new file mode 100644 index 0000000000..29ae0a8c00 --- /dev/null +++ b/_ja/tour/automatic-closures.md @@ -0,0 +1,62 @@ +--- +layout: tour +title: Automatic Type-Dependent Closure Construction +language: ja + +discourse: true + +partof: scala-tour +--- + +Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evaluation). + +The following code demonstrates this mechanism: + + object TargetTest1 extends Application { + def whileLoop(cond: => Boolean)(body: => Unit): Unit = + if (cond) { + body + whileLoop(cond)(body) + } + var i = 10 + whileLoop (i > 0) { + println(i) + i -= 1 + } + } + +The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme. + +We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax). + +Here is the implementation of a loop-unless statement: + + object TargetTest2 extends Application { + def loop(body: => Unit): LoopUnlessCond = + new LoopUnlessCond(body) + protected class LoopUnlessCond(body: => Unit) { + def unless(cond: => Boolean) { + body + if (!cond) unless(cond) + } + } + var i = 10 + loop { + println("i = " + i) + i -= 1 + } unless (i == 0) + } +The `loop` function just accepts a body of a loop and returns an instance of class `LoopUnlessCond` (which encapsulates this body object). Note that the body didn't get evaluated yet. Class `LoopUnlessCond` has a method `unless` which we can use as a *infix operator*. This way, we achieve a quite natural syntax for our new loop: `loop { < stats > } unless ( < cond > )`. + +Here's the output when `TargetTest2` gets executed: + + i = 10 + i = 9 + i = 8 + i = 7 + i = 6 + i = 5 + i = 4 + i = 3 + i = 2 + i = 1 From c2010ffec49397905fd7d67ec0d2853418942b71 Mon Sep 17 00:00:00 2001 From: take2 Date: Tue, 25 Jun 2019 23:57:10 +0900 Subject: [PATCH 349/358] Update _ja/tour/tour-of-scala.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit allowは「しても良く」くらい Co-Authored-By: eugene yokota --- _ja/tour/tour-of-scala.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/tour-of-scala.md b/_ja/tour/tour-of-scala.md index 6fad8d68ea..775c66557f 100644 --- a/_ja/tour/tour-of-scala.md +++ b/_ja/tour/tour-of-scala.md @@ -34,7 +34,7 @@ Scalaは[全ての値がオブジェクトである](unified-types.html) とい ## Scalaは関数型 ## Scalaは[すべての関数が値である](unified-types.html) という意味で関数型言語でもあります。 Scalaは無名関数を定義するために[軽量な構文](basics.html#関数)を提供し、 -[高階関数](higher-order-functions.html) をサポートし、関数の[ネスト](nested-functions.html) を許可し、[カリー化](multiple-parameter-lists.html) をサポートします。 +[高階関数](higher-order-functions.html) をサポートし、関数は[ネスト](nested-functions.html)しても良く、[カリー化](multiple-parameter-lists.html) をサポートします。 Scalaの[ケースクラス](case-classes.html)には[パターンマッチング](pattern-matching.html)が組み込まれていることにより、多くの関数型プログラミング言語で使われる代数型を作ることができます。 [シングルトンオブジェクト](singleton-objects.html) はクラスのメンバーではない関数をグループ化する便利な方法を提供します。 From 5d2dfc63aa05bb37cd83a31225a8f94294ae8b2e Mon Sep 17 00:00:00 2001 From: take2 Date: Tue, 25 Jun 2019 23:59:39 +0900 Subject: [PATCH 350/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 5a7851b8f2..d49c986462 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -106,7 +106,7 @@ println({ ## 関数 -関数は引数を受け取る式です。 +関数はパラメーターを受け取る式です。 ここでは与えられた数値に1を足す無名関数(すなわち名前が無い関数)を宣言しています。 ```tut From e2fccf198ff987c26a9ae090830c754121c1a032 Mon Sep 17 00:00:00 2001 From: take2 Date: Tue, 25 Jun 2019 23:59:52 +0900 Subject: [PATCH 351/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index d49c986462..504382a8e6 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -112,7 +112,7 @@ println({ ```tut (x: Int) => x + 1 ``` -`=>` の左側は引数のリストです。右側は引数を含む式です。 +`=>` の左側はパラメーターのリストです。右側はパラメーターを含む式です。 関数には名前をつけることもできます。 From dfeb7ce09d0cd9701d3548c3bd0fa10681de7b8b Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:00:03 +0900 Subject: [PATCH 352/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 504382a8e6..caccd92ea3 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -123,7 +123,7 @@ println(addOne(1)) // 2 ``` {% endscalafiddle %} -関数は複数の引数をとることもできます。 +関数は複数のパラメーターをとることもできます。 {% scalafiddle %} ```tut From d13c4d10c4d6395121ffa9a84243c857e164d9a1 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:00:13 +0900 Subject: [PATCH 353/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index caccd92ea3..6831b461d4 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -132,7 +132,7 @@ println(add(1, 2)) // 3 ``` {% endscalafiddle %} -また引数を取らないこともありえます。 +またパラメーターを取らないこともありえます。 ```tut val getTheAnswer = () => 42 From 10bbc1a8338727fe5929eb51e668a4e5dbccd762 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:00:30 +0900 Subject: [PATCH 354/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 6831b461d4..7da8d5056a 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -143,7 +143,7 @@ println(getTheAnswer()) // 42 メソッドは関数と見た目、振る舞いがとても似ていますが、それらには違いがいくつかあります。 -メソッドは `def` キーワードで定義されます。 `def` の後ろには名前、引数リスト、戻り値の型、処理の内容が続きます。 +メソッドは `def` キーワードで定義されます。 `def` の後ろには名前、パラメーターリスト、戻り値の型、処理の内容が続きます。 {% scalafiddle %} ```tut From e6e9a7292095062110f61d25f508a9a8da8a1332 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:00:47 +0900 Subject: [PATCH 355/358] Update _ja/tour/basics.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parameterは素直にパラメーター Co-Authored-By: eugene yokota --- _ja/tour/basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/basics.md b/_ja/tour/basics.md index 7da8d5056a..b14330f2b3 100644 --- a/_ja/tour/basics.md +++ b/_ja/tour/basics.md @@ -182,7 +182,7 @@ def getSquareString(input: Double): String = { ## クラス -`class` キーワードとその後ろに名前、コンストラクタ引数を続けることで、クラスを定義することができます。 +`class` キーワードとその後ろに名前、コンストラクタパラメーターを続けることで、クラスを定義することができます。 ```tut class Greeter(prefix: String, suffix: String) { From 584c2e631d7c1dcfb1ba4061a92c7545ecb977c5 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:01:49 +0900 Subject: [PATCH 356/358] Update _ja/tour/classes.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integer => 整数、回りくどいので Unitの値 => Unit値 Co-Authored-By: eugene yokota --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 46f47a4ab4..522045b073 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -54,7 +54,7 @@ println(point1) // prints (2, 3) この`Point`クラスは4つのメンバーを持ちます。 変数`x` と `y` そしてメソッド `move` と `toString`です。 多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネチャ`(var x: Int, var y: Int)`です。 -`move`メソッドは2つのInteger引数を受け取り、意味を持たないUnitの値である`()` を返します。 +`move` メソッドは2つの整数の引数を受け取り、情報を持たない Unit 値 `()` を返します。 これは乱暴に言えば、Javaのような言語における`void`に対応します。 その一方で`toString`は引数を受け取りませんが、`String`の値を返します。 `toString`は[`AnyRef`](unified-types.html)の`toString`をオーバーライドしているので、`override`キーワードのタグが付いています。 From df3155413f4a8d73aed4542d3f1a9f55af952b7a Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:02:28 +0900 Subject: [PATCH 357/358] Update _ja/tour/classes.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 「乱暴に言えば」はあまり使わないので、「大雑把に言えば」の方が適切 Co-Authored-By: eugene yokota --- _ja/tour/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/classes.md b/_ja/tour/classes.md index 522045b073..0253f210f5 100644 --- a/_ja/tour/classes.md +++ b/_ja/tour/classes.md @@ -55,7 +55,7 @@ println(point1) // prints (2, 3) 変数`x` と `y` そしてメソッド `move` と `toString`です。 多くの他の言語とは異なり、プライマリコンストラクタはクラスのシグネチャ`(var x: Int, var y: Int)`です。 `move` メソッドは2つの整数の引数を受け取り、情報を持たない Unit 値 `()` を返します。 -これは乱暴に言えば、Javaのような言語における`void`に対応します。 +これは大雑把に言えば、Javaのような言語における`void`に対応します。 その一方で`toString`は引数を受け取りませんが、`String`の値を返します。 `toString`は[`AnyRef`](unified-types.html)の`toString`をオーバーライドしているので、`override`キーワードのタグが付いています。 From 11b26fb5efc0eeb5e26b3f37debf6f691ca3fde5 Mon Sep 17 00:00:00 2001 From: take2 Date: Wed, 26 Jun 2019 00:03:26 +0900 Subject: [PATCH 358/358] Update _ja/tour/higher-order-functions.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit そのまま「ファーストクラス」より「第一級値 (first-classs value)」と括弧付きで補足がある方が分かりやすい Co-Authored-By: eugene yokota --- _ja/tour/higher-order-functions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_ja/tour/higher-order-functions.md b/_ja/tour/higher-order-functions.md index da6025380f..57714202e8 100644 --- a/_ja/tour/higher-order-functions.md +++ b/_ja/tour/higher-order-functions.md @@ -15,7 +15,7 @@ redirect_from: "/tutorials/tour/higher-order-functions.html" --- 高階関数は他の関数をパラメーターとして受け取る、もしくは結果として関数を返します。 -このようなことができるのは、Scalaでは関数がファーストクラスだからです。 +このようなことができるのは、Scalaでは関数が第一級値 (first-classs value) だからです。 用語が少し紛らわしいかもしれませんが、 ここでは"高階関数"というフレーズを関数をパラメーターとして受け取る、または関数を返すメソッドと関数の両方に対して使います。