11% メソッド構文
22<!-- % Method Syntax -->
33
4- <!-- Functions are great, but if you want to call a bunch of them on some data, it
5- can be awkward. Consider this code: -->
6- 関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。
4+ <!-- Functions are great, but if you want to call a bunch of them on some data, it -->
5+ <!-- can be awkward. Consider this code: -->
6+ 関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。
7+ 以下のコードについて考えてみます。
78
89``` rust,ignore
910baz(bar(foo));
1011```
1112
12- <!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
13- order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
14- Wouldn’t it be nice if we could do this instead? -->
15- 私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか?
13+ <!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the -->
14+ <!-- order that the functions would get called in, that’s inside-out: ‘foo bar baz’. -->
15+ <!-- Wouldn’t it be nice if we could do this instead? -->
16+ 私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。
17+ もし代わりにこうできたらいいとは思いませんか?
1618
1719``` rust,ignore
1820foo.bar().baz();
1921```
2022
21- <!-- Luckily, as you may have guessed with the leading question, you can! Rust provides
22- the ability to use this ‘method call syntax’ via the `impl` keyword. -->
23- 最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!Rustは ` impl ` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
23+ <!-- Luckily, as you may have guessed with the leading question, you can! Rust provides -->
24+ <!-- the ability to use this ‘method call syntax’ via the `impl` keyword. -->
25+ 最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!
26+ Rustは ` impl ` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
2427
2528<!-- # Method calls -->
2629# メソッド呼び出し
@@ -50,23 +53,27 @@ fn main() {
5053<!-- This will print `12.566371`. -->
5154これは ` 12.566371 ` と出力します。
5255
53- <!-- We’ve made a `struct` that represents a circle. We then write an `impl` block,
54- and inside it, define a method, `area`. -->
56+ <!-- We’ve made a `struct` that represents a circle. We then write an `impl` block, -->
57+ <!-- and inside it, define a method, `area`. -->
5558私たちは円を表す ` struct ` を作りました。その際 ` impl ` ブロックを書き、その中に ` area ` というメソッドを定義しています。
5659
57- <!-- Methods take a special first parameter, of which there are three variants:
58- `self`, `&self`, and `&mut self`. You can think of this first parameter as
59- being the `foo` in `foo.bar()`. The three variants correspond to the three
60- kinds of things `foo` could be: `self` if it’s just a value on the stack,
61- `&self` if it’s a reference, and `&mut self` if it’s a mutable reference.
62- Because we took the `&self` parameter to `area`, we can use it just like any
63- other parameter. Because we know it’s a `Circle`, we can access the `radius`
64- just like we would with any other `struct`. -->
65- メソッドに渡す特別な第1引数として、 ` self ` 、 ` &self ` 、 ` &mut self ` という3つの変形があります。第一引数は ` foo.bar() ` に於ける ` foo ` だと考えて下さい。3つの変形は ` foo ` が成り得る3種類の状態に対応しており、それぞれ ` self ` がスタック上の値である場合、 ` &self ` が参照である場合、 ` &mut self ` がミュータブルな参照である場合となっています。 ` area ` では ` &self ` を受け取っているため、他の引数と同じように扱えます。引数が ` Circle ` であるのは分かっていますから、他の ` struct ` でするように ` radius ` へアクセスできます。
66-
67- <!-- We should default to using `&self`, as you should prefer borrowing over taking
68- ownership, as well as taking immutable references over mutable ones. Here’s an
69- example of all three variants: -->
60+ <!-- Methods take a special first parameter, of which there are three variants: -->
61+ <!-- `self`, `&self`, and `&mut self`. You can think of this first parameter as -->
62+ <!-- being the `foo` in `foo.bar()`. The three variants correspond to the three -->
63+ <!-- kinds of things `foo` could be: `self` if it’s a value on the stack, -->
64+ <!-- `&self` if it’s a reference, and `&mut self` if it’s a mutable reference. -->
65+ <!-- Because we took the `&self` parameter to `area`, we can use it like any -->
66+ <!-- other parameter. Because we know it’s a `Circle`, we can access the `radius` -->
67+ <!-- like we would with any other `struct`. -->
68+ メソッドに渡す特別な第1引数として、 ` self ` 、 ` &self ` 、 ` &mut self ` という3つの変形があります。
69+ 第一引数は ` foo.bar() ` に於ける ` foo ` だと考えて下さい。
70+ 3つの変形は ` foo ` が成り得る3種類の状態に対応しており、それぞれ ` self ` がスタック上の値である場合、 ` &self ` が参照である場合、 ` &mut self ` がミュータブルな参照である場合となっています。
71+ ` area ` では ` &self ` を受け取っているため、他の引数と同じように扱えます。
72+ 引数が ` Circle ` であるのは分かっていますから、他の ` struct ` でするように ` radius ` へアクセスできます。
73+
74+ <!-- We should default to using `&self`, as you should prefer borrowing over taking -->
75+ <!-- ownership, as well as taking immutable references over mutable ones. Here’s an -->
76+ <!-- example of all three variants: -->
7077所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 ` &self ` を常用すべきです。以下が3種類全ての例です。
7178
7279``` rust
@@ -91,9 +98,10 @@ impl Circle {
9198}
9299```
93100
94- <!-- You can use as many `impl` blocks as you’d like. The previous example could
95- have also been written like this: -->
96- 好きな数だけ ` impl ` ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。
101+ <!-- You can use as many `impl` blocks as you’d like. The previous example could -->
102+ <!-- have also been written like this: -->
103+ 好きな数だけ ` impl ` ブロックを使用することができます。
104+ 前述の例は以下のように書くこともできるでしょう。
97105
98106``` rust
99107struct Circle {
@@ -124,10 +132,13 @@ impl Circle {
124132<!-- # Chaining method calls -->
125133# メソッドチェーン
126134
127- <!-- So, now we know how to call a method, such as `foo.bar()`. But what about our
128- original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s
129- look at an example: -->
130- ここまでで、` foo.bar() ` というようなメソッドの呼び出し方が分かりましたね。ですが元の例の ` foo.bar().baz() ` はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。
135+ <!-- So, now we know how to call a method, such as `foo.bar()`. But what about our -->
136+ <!-- original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s -->
137+ <!-- look at an example: -->
138+ ここまでで、` foo.bar() ` というようなメソッドの呼び出し方が分かりましたね。
139+ ですが元の例の ` foo.bar().baz() ` はどうなっているのでしょう?
140+ これは「メソッドチェーン」と呼ばれています。
141+ 以下の例を見て下さい。
131142
132143``` rust
133144struct Circle {
@@ -165,16 +176,18 @@ fn grow(&self, increment: f64) -> Circle {
165176# Circle } }
166177```
167178
168- <!-- We just say we’re returning a `Circle`. With this method, we can grow a new
169- `Circle` to any arbitrary size. -->
170- 単に ` Circle ` を返しているだけです。このメソッドにより、私たちは新しい ` Circle ` を任意の大きさに拡大することができます。
179+ <!-- We say we’re returning a `Circle`. With this method, we can grow a new -->
180+ <!-- `Circle` to any arbitrary size. -->
181+ 単に ` Circle ` を返しているだけです。
182+ このメソッドにより、私たちは新しい ` Circle ` を任意の大きさに拡大することができます。
171183
172184<!-- # Associated functions -->
173185# 関連関数
174186
175- <!-- You can also define associated functions that do not take a `self` parameter.
176- Here’s a pattern that’s very common in Rust code: -->
177- あなたは ` self ` を引数に取らない関連関数を定義することもできます。以下のパターンはRustのコードにおいて非常にありふれた物です。
187+ <!-- You can also define associated functions that do not take a `self` parameter. -->
188+ <!-- Here’s a pattern that’s very common in Rust code: -->
189+ あなたは ` self ` を引数に取らない関連関数を定義することもできます。
190+ 以下のパターンはRustのコードにおいて非常にありふれた物です。
178191
179192``` rust
180193struct Circle {
@@ -198,21 +211,25 @@ fn main() {
198211}
199212```
200213
201- <!-- This ‘associated function’ builds a new `Circle` for us. Note that associated
202- functions are called with the `Struct::function()` syntax, rather than the
203- `ref.method()` syntax. Some other languages call associated functions ‘static
204- methods’. -->
205- この「関連関数」(associated function)は新たに ` Circle ` を構築します。この関数は ` ref.method() ` ではなく、 ` Struct::function() ` という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
214+ <!-- This ‘associated function’ builds a new `Circle` for us. Note that associated -->
215+ <!-- functions are called with the `Struct::function()` syntax, rather than the -->
216+ <!-- `ref.method()` syntax. Some other languages call associated functions ‘static -->
217+ <!-- methods’. -->
218+ この「関連関数」(associated function)は新たに ` Circle ` を構築します。
219+ この関数は ` ref.method() ` ではなく、 ` Struct::function() ` という構文で呼び出されることに注意して下さい。
220+ 幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
206221
207222<!-- # Builder Pattern -->
208223# Builderパターン
209224
210- <!-- Let’s say that we want our users to be able to create `Circle`s, but we will
211- allow them to only set the properties they care about. Otherwise, the `x`
212- and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t
213- have method overloading, named arguments, or variable arguments. We employ
214- the builder pattern instead. It looks like this: -->
215- ユーザが ` Circle ` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ ` x ` と ` y ` が ` 0.0 ` 、 ` radius ` が ` 1.0 ` であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
225+ <!-- Let’s say that we want our users to be able to create `Circle`s, but we will -->
226+ <!-- allow them to only set the properties they care about. Otherwise, the `x` -->
227+ <!-- and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t -->
228+ <!-- have method overloading, named arguments, or variable arguments. We employ -->
229+ <!-- the builder pattern instead. It looks like this: -->
230+ ユーザが ` Circle ` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。
231+ もし指定が無ければ ` x ` と ` y ` が ` 0.0 ` 、 ` radius ` が ` 1.0 ` であるものとします。
232+ Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
216233
217234``` rust
218235struct Circle {
@@ -271,10 +288,15 @@ fn main() {
271288}
272289```
273290
274- <!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
275- builder methods on it. We’ve also defined our `area()` method on `Circle`. We
276- also made one more method on `CircleBuilder`: `finalize()`. This method creates
277- our final `Circle` from the builder. Now, we’ve used the type system to enforce
278- our concerns: we can use the methods on `CircleBuilder` to constrain making
279- `Circle`s in any way we choose. -->
280- ここではもう1つの ` struct ` である ` CircleBuilder ` を作成しています。その中にBuilderメソッドを定義しました。また ` Circle ` に ` area() ` メソッドを定義しました。 そして ` CircleBuilder ` にもう1つ ` finalize() ` というメソッドを作りました。このメソッドはBuilderから最終的な ` Circle ` を作成します。さて、先程の要求を実施するために型システムを使いました。 ` CircleBuilder ` のメソッドを好きなように組み合わせ、作る ` Circle ` への制約を与えることができます。
291+ <!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our -->
292+ <!-- builder methods on it. We’ve also defined our `area()` method on `Circle`. We -->
293+ <!-- also made one more method on `CircleBuilder`: `finalize()`. This method creates -->
294+ <!-- our final `Circle` from the builder. Now, we’ve used the type system to enforce -->
295+ <!-- our concerns: we can use the methods on `CircleBuilder` to constrain making -->
296+ <!-- `Circle`s in any way we choose. -->
297+ ここではもう1つの ` struct ` である ` CircleBuilder ` を作成しています。
298+ その中にBuilderメソッドを定義しました。また ` Circle ` に ` area() ` メソッドを定義しました。
299+ そして ` CircleBuilder ` にもう1つ ` finalize() ` というメソッドを作りました。
300+ このメソッドはBuilderから最終的な ` Circle ` を作成します。
301+ さて、先程の要求を実施するために型システムを使いました。
302+ ` CircleBuilder ` のメソッドを好きなように組み合わせ、作る ` Circle ` への制約を与えることができます。
0 commit comments