diff --git a/1.6/ja/book/method-syntax.md b/1.6/ja/book/method-syntax.md index 41c134b2..1888558a 100644 --- a/1.6/ja/book/method-syntax.md +++ b/1.6/ja/book/method-syntax.md @@ -1,26 +1,32 @@ -% Method Syntax +% メソッドシンタックス + -Functions are great, but if you want to call a bunch of them on some data, it -can be awkward. Consider this code: + +関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。 ```rust,ignore baz(bar(foo)); ``` -We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the + +私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか? ```rust,ignore foo.bar().baz(); ``` -Luckily, as you may have guessed with the leading question, you can! Rust provides -the ability to use this ‘method call syntax’ via the `impl` keyword. + +最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!Rustは `impl` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。 -# Method calls + +# メソッド呼び出し -Here’s how it works: + +どんな風に動作するかが以下になります。 ```rust struct Circle { @@ -41,23 +47,27 @@ fn main() { } ``` -This will print `12.566371`. + +これは `12.566371` と出力します。 -We’ve made a `struct` that represents a circle. We then write an `impl` block, -and inside it, define a method, `area`. + +私たちは円を表す `struct` を作りました。その際 `impl` ブロックを書き、その中に `area` というメソッドを定義しています。 -Methods take a special first parameter, of which there are three variants: + +メソッドに渡す特別な第1引数として、 `self` 、 `&self` 、 `&mut self` という3つの変形があります。第一引数は `foo.bar()` に於ける `foo` だと考えて下さい。3つの変形は `foo` が成り得る3種類の状態に対応しており、それぞれ `self` がスタック上の値である場合、 `&self` が参照である場合、 `&mut self` がミュータブルな参照である場合となっています。 `area` では `&self` を受け取っているため、他の引数と同じように扱えます。引数が `Circle` であるのは分かっていますから、他の `struct` でするように `radius` へアクセスできます。 -We should default to using `&self`, as you should prefer borrowing over taking + +所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 `&self` を常用すべきです。以下が3種類全ての例です。 ```rust struct Circle { @@ -81,8 +91,9 @@ impl Circle { } ``` -You can use as many `impl` blocks as you’d like. The previous example could -have also been written like this: + +好きな数だけ `impl` ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。 ```rust struct Circle { @@ -110,11 +121,13 @@ impl Circle { } ``` -# Chaining method calls + +# メソッドチェーン -So, now we know how to call a method, such as `foo.bar()`. But what about our + +ここまでで、`foo.bar()` というようなメソッドの呼び出し方が分かりましたね。ですが元の例の `foo.bar().baz()` はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。 ```rust struct Circle { @@ -142,7 +155,8 @@ fn main() { } ``` -Check the return type: + +以下の返す型を確認して下さい。 ```rust # struct Circle; @@ -151,13 +165,16 @@ fn grow(&self, increment: f64) -> Circle { # Circle } } ``` -We just say we’re returning a `Circle`. With this method, we can grow a new -`Circle` to any arbitrary size. + +単に `Circle` を返しているだけです。このメソッドにより、私たちは新しい `Circle` を任意の大きさに拡大することができます。 -# Associated functions + +# 関連関数 -You can also define associated functions that do not take a `self` parameter. -Here’s a pattern that’s very common in Rust code: + +あなたは `self` を引数に取らない関連関数を定義することもできます。以下のパターンはRustのコードにおいて非常にありふれた物です。 ```rust struct Circle { @@ -181,18 +198,21 @@ fn main() { } ``` -This ‘associated function’ builds a new `Circle` for us. Note that associated + +この「関連関数」(associated function)は新たに `Circle` を構築します。この関数は `ref.method()` ではなく、 `Struct::function()` という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。 -# Builder Pattern + +# Builderパターン -Let’s say that we want our users to be able to create `Circle`s, but we will + +ユーザが `Circle` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ `x` と `y` が `0.0` 、 `radius` が `1.0` であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。 ```rust struct Circle { @@ -251,9 +271,10 @@ fn main() { } ``` -What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our + +ここではもう1つの `struct` である `CircleBuilder` を作成しています。その中にBuilderメソッドを定義しました。また `Circle` に `area()` メソッドを定義しました。 そして `CircleBuilder` にもう1つ `finalize()` というメソッドを作りました。このメソッドはBuilderから最終的な `Circle` を作成します。さて、先程の要求を実施するために型システムを使いました。 `CircleBuilder` のメソッドを好きなように組み合わせ、作る `Circle` への制約を与えることができます。