Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions 1.9/ja/book/method-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- 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
Expand All @@ -14,15 +14,15 @@ baz(bar(foo));
<!-- order that the functions would get called in, that’s inside-out: ‘foo bar baz’. -->
<!-- Wouldn’t it be nice if we could do this instead? -->
私たちはこれを左から右へ、「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 -->
Expand Down Expand Up @@ -61,21 +61,22 @@ fn main() {
<!-- Methods take a special first parameter, of which there are three variants: -->
<!-- `self`, `&self`, and `&mut self`. You can think of this first parameter as -->
<!-- being the `foo` in `foo.bar()`. The three variants correspond to the three -->
<!-- kinds of things `foo` could be: `self` if it’s just a value on the stack, -->
<!-- kinds of things `foo` could be: `self` if it’s a value on the stack, -->
<!-- `&self` if it’s a reference, and `&mut self` if it’s a mutable reference. -->
<!-- Because we took the `&self` parameter to `area`, we can use it just like any -->
<!-- Because we took the `&self` parameter to `area`, we can use it like any -->
<!-- other parameter. Because we know it’s a `Circle`, we can access the `radius` -->
<!-- just like we would with any other `struct`. -->
メソッドに渡す特別な第1引数として、 `self` 、 `&self` 、 `&mut self` という3つの変形があります
第一引数は `foo.bar()` に於ける `foo` だと考えて下さい。
3つの変形は `foo` が成り得る3種類の状態に対応しており、それぞれ `self` がスタック上の値である場合、 `&self` が参照である場合、 `&mut self` がミュータブルな参照である場合となっています。
<!-- like we would with any other `struct`. -->
メソッドに渡す特別な第1引数には、 `self` 、 `&self` 、 `&mut self` という3つのバリエーションがあります
第一引数は `foo.bar()` における `foo` だと考えて下さい。
これら3つのバリエーションは `foo` がなりうる3種類の状態に対応しており、それぞれ `self` がスタック上の値である場合、 `&self` が参照である場合、 `&mut self` がミュータブルな参照である場合となっています。
`area` では `&self` を受け取っているため、他の引数と同じように扱えます。
引数が `Circle` であるのは分かっていますから、他の `struct` でするように `radius` へアクセスできます。
それが `Circle` であるのは分かっていますから、他の `struct` でするように `radius` へアクセスできます。

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

```rust
struct Circle {
Expand All @@ -101,8 +102,8 @@ impl Circle {

<!--You can use as many `impl` blocks as you’d like. The previous example could -->
<!-- have also been written like this: -->
好きな数だけ `impl` ブロックを使用することができます
前述の例は以下のように書くこともできるでしょう
好きな数だけ `impl` ブロックを使用できます
前述の例は以下のようにも書けるでしょう

```rust
struct Circle {
Expand Down Expand Up @@ -168,7 +169,7 @@ fn main() {
```

<!-- Check the return type: -->
以下の返す型を確認して下さい
戻り値の型を確認しましょう

```rust
# struct Circle;
Expand All @@ -177,18 +178,18 @@ fn grow(&self, increment: f64) -> Circle {
# Circle } }
```

<!-- We just say we’re returning a `Circle`. With this method, we can grow a new -->
<!-- We say we’re returning a `Circle`. With this method, we can grow a new -->
<!-- `Circle` to any arbitrary size. -->
単に `Circle` を返しているだけです
このメソッドにより、私たちは新しい `Circle` を任意の大きさに拡大することができます
`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のコードにおいて非常にありふれた物です
関連関数という `self` を引数に取らない関数も定義できます
以下のパターンはRustのコードにおいて、とてもよく使われています

```rust
struct Circle {
Expand Down Expand Up @@ -218,7 +219,7 @@ fn main() {
<!-- methods’. -->
この「関連関数」(associated function)は新たに `Circle` を構築します。
この関数は `ref.method()` ではなく、 `Struct::function()` という構文で呼び出されることに注意して下さい。
幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
いくつかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。

<!-- # Builder Pattern -->
# Builderパターン
Expand All @@ -228,9 +229,11 @@ fn main() {
<!-- and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t -->
<!-- have method overloading, named arguments, or variable arguments. We employ -->
<!-- the builder pattern instead. It looks like this: -->
ユーザが `Circle` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。
もし指定が無ければ `x` と `y` が `0.0` 、 `radius` が `1.0` であるものとします。
Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
ユーザが `Circle` を作成する際、関心のあるプロパティだけを指定できるようにしたいとしましょう。
指定がなければ `x` と `y` は `0.0` 、 `radius` は `1.0` にします。
Rustにはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がありません。
代わりにBuilderパターンを採用しましょう。
以下のようになります。

```rust
struct Circle {
Expand Down Expand Up @@ -295,10 +298,10 @@ fn main() {
<!-- our final `Circle` from the builder. Now, we’ve used the type system to enforce -->
<!-- our concerns: we can use the methods on `CircleBuilder` to constrain making -->
<!-- `Circle`s in any way we choose. -->
ここではもう1つの `struct` である `CircleBuilder` を作成しています。
その中にBuilderメソッドを定義しました
ここでは `CircleBuilder` という、もう1つの `struct` を作成しています。
それに対してBuilderメソッドを定義しました
また `Circle` に `area()` メソッドを定義しました。
そして `CircleBuilder` にもう1つ `finalize()` というメソッドを作りました。
このメソッドはBuilderから最終的な `Circle` を作成します。
さて、先程の要求を実施するために型システムを使いました
`CircleBuilder` のメソッドを好きなように組み合わせ、作る `Circle` への制約を与えることができます
さて、私たちは先ほどの要求を守らせるために型システムを使いました
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要求を守らせるために

先に出てきた「要求」は制限したいではなくて自由を得たいという内容だったので
「要求を実現するために」
はいかがでしょうか。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KeenS コメントありがとうございます。自由を得たいということですが、その要求とはどこを指してますか?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここではcircleを作成するというのが要求ですから「守らせる」の方が正しいと思います。
ただ日本語的には「満たす」とした方がより自然ではないでしょうか?
いかがでしょうか?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

しばらく家を空けていて返信遅れました。

ユーザが Circle を作成する際、関心のあるプロパティだけを指定できるようにしたいとしましょう。
指定がなければ xy0.0radius1.0 にします。

の意図でした。「関心のあるものだけ指定したい」という要求(要件)です。

ただ日本語的には「満たす」とした方がより自然ではないでしょうか?

その方が良いかもしれません。

`CircleBuilder` に用意するメソッドによって、作成する `Circle` に、望み通りの制約を与えられます
27 changes: 0 additions & 27 deletions diff-1.6.0..1.9.0/src/doc/book/method-syntax.md

This file was deleted.