Skip to content

Commit 73206a8

Browse files
committed
Merge pull request #22 from Nnwww/4_15_Method_Syntax
4.15. Method Syntax
2 parents 7cc70fc + 1006b73 commit 73206a8

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

1.6/ja/book/method-syntax.md

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
% Method Syntax
1+
% メソッドシンタックス
2+
<!-- % Method Syntax -->
23

3-
Functions are great, but if you want to call a bunch of them on some data, it
4-
can be awkward. Consider this code:
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+
関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。
57

68
```rust,ignore
79
baz(bar(foo));
810
```
911

10-
We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
12+
<!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
1113
order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
12-
Wouldn’t it be nice if we could do this instead?
14+
Wouldn’t it be nice if we could do this instead? -->
15+
私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか?
1316

1417
```rust,ignore
1518
foo.bar().baz();
1619
```
1720

18-
Luckily, as you may have guessed with the leading question, you can! Rust provides
19-
the ability to use this ‘method call syntax’ via the `impl` keyword.
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` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
2024

21-
# Method calls
25+
<!-- # Method calls -->
26+
# メソッド呼び出し
2227

23-
Here’s how it works:
28+
<!-- Here’s how it works: -->
29+
どんな風に動作するかが以下になります。
2430

2531
```rust
2632
struct Circle {
@@ -41,23 +47,27 @@ fn main() {
4147
}
4248
```
4349

44-
This will print `12.566371`.
50+
<!-- This will print `12.566371`. -->
51+
これは `12.566371` と出力します。
4552

46-
We’ve made a `struct` that represents a circle. We then write an `impl` block,
47-
and inside it, define a method, `area`.
53+
<!-- We’ve made a `struct` that represents a circle. We then write an `impl` block,
54+
and inside it, define a method, `area`. -->
55+
私たちは円を表す `struct` を作りました。その際 `impl` ブロックを書き、その中に `area` というメソッドを定義しています。
4856

49-
Methods take a special first parameter, of which there are three variants:
57+
<!-- Methods take a special first parameter, of which there are three variants:
5058
`self`, `&self`, and `&mut self`. You can think of this first parameter as
5159
being the `foo` in `foo.bar()`. The three variants correspond to the three
5260
kinds of things `foo` could be: `self` if it’s just a value on the stack,
5361
`&self` if it’s a reference, and `&mut self` if it’s a mutable reference.
5462
Because we took the `&self` parameter to `area`, we can use it just like any
5563
other parameter. Because we know it’s a `Circle`, we can access the `radius`
56-
just like we would with any other `struct`.
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` へアクセスできます。
5766

58-
We should default to using `&self`, as you should prefer borrowing over taking
67+
<!-- We should default to using `&self`, as you should prefer borrowing over taking
5968
ownership, as well as taking immutable references over mutable ones. Here’s an
60-
example of all three variants:
69+
example of all three variants: -->
70+
所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 `&self` を常用すべきです。以下が3種類全ての例です。
6171

6272
```rust
6373
struct Circle {
@@ -81,8 +91,9 @@ impl Circle {
8191
}
8292
```
8393

84-
You can use as many `impl` blocks as you’d like. The previous example could
85-
have also been written like this:
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` ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。
8697

8798
```rust
8899
struct Circle {
@@ -110,11 +121,13 @@ impl Circle {
110121
}
111122
```
112123

113-
# Chaining method calls
124+
<!-- # Chaining method calls -->
125+
# メソッドチェーン
114126

115-
So, now we know how to call a method, such as `foo.bar()`. But what about our
127+
<!-- So, now we know how to call a method, such as `foo.bar()`. But what about our
116128
original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s
117-
look at an example:
129+
look at an example: -->
130+
ここまでで、`foo.bar()` というようなメソッドの呼び出し方が分かりましたね。ですが元の例の `foo.bar().baz()` はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。
118131

119132
```rust
120133
struct Circle {
@@ -142,7 +155,8 @@ fn main() {
142155
}
143156
```
144157

145-
Check the return type:
158+
<!-- Check the return type: -->
159+
以下の返す型を確認して下さい。
146160

147161
```rust
148162
# struct Circle;
@@ -151,13 +165,16 @@ fn grow(&self, increment: f64) -> Circle {
151165
# Circle } }
152166
```
153167

154-
We just say we’re returning a `Circle`. With this method, we can grow a new
155-
`Circle` to any arbitrary size.
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` を任意の大きさに拡大することができます。
156171

157-
# Associated functions
172+
<!-- # Associated functions -->
173+
# 関連関数
158174

159-
You can also define associated functions that do not take a `self` parameter.
160-
Here’s a pattern that’s very common in Rust code:
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のコードにおいて非常にありふれた物です。
161178

162179
```rust
163180
struct Circle {
@@ -181,18 +198,21 @@ fn main() {
181198
}
182199
```
183200

184-
This ‘associated function’ builds a new `Circle` for us. Note that associated
201+
<!-- This ‘associated function’ builds a new `Circle` for us. Note that associated
185202
functions are called with the `Struct::function()` syntax, rather than the
186203
`ref.method()` syntax. Some other languages call associated functions ‘static
187-
methods’.
204+
methods’. -->
205+
この「関連関数」(associated function)は新たに `Circle` を構築します。この関数は `ref.method()` ではなく、 `Struct::function()` という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
188206

189-
# Builder Pattern
207+
<!-- # Builder Pattern -->
208+
# Builderパターン
190209

191-
Let’s say that we want our users to be able to create `Circle`s, but we will
210+
<!-- Let’s say that we want our users to be able to create `Circle`s, but we will
192211
allow them to only set the properties they care about. Otherwise, the `x`
193212
and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t
194213
have method overloading, named arguments, or variable arguments. We employ
195-
the builder pattern instead. It looks like this:
214+
the builder pattern instead. It looks like this: -->
215+
ユーザが `Circle` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ `x``y``0.0``radius``1.0` であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
196216

197217
```rust
198218
struct Circle {
@@ -251,9 +271,10 @@ fn main() {
251271
}
252272
```
253273

254-
What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
274+
<!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
255275
builder methods on it. We’ve also defined our `area()` method on `Circle`. We
256276
also made one more method on `CircleBuilder`: `finalize()`. This method creates
257277
our final `Circle` from the builder. Now, we’ve used the type system to enforce
258278
our concerns: we can use the methods on `CircleBuilder` to constrain making
259-
`Circle`s in any way we choose.
279+
`Circle`s in any way we choose. -->
280+
ここではもう1つの `struct` である `CircleBuilder` を作成しています。その中にBuilderメソッドを定義しました。また `Circle``area()` メソッドを定義しました。 そして `CircleBuilder` にもう1つ `finalize()` というメソッドを作りました。このメソッドはBuilderから最終的な `Circle` を作成します。さて、先程の要求を実施するために型システムを使いました。 `CircleBuilder` のメソッドを好きなように組み合わせ、作る `Circle` への制約を与えることができます。

0 commit comments

Comments
 (0)