Skip to content
Merged
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
54 changes: 29 additions & 25 deletions 1.9/ja/book/associated-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
<!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
<!-- this: -->
関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
言い換えると、複数の型をグループ化するものです。
関連型は、Rust型システムの強力な部分です。
関連型は、「型族」という概念と関連があり、言い換えると、複数の型をグループ化するものです。
この説明はすこし抽象的なので、実際の例を見ていきましょう。
例えば、 `Graph` トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
そのため、以下のように `Graph<N, E>` と書きたくなるでしょう:
Expand All @@ -25,20 +25,20 @@ trait Graph<N, E> {
<!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
<!-- the `N`ode and `E`dge types too: -->
たしかに上のようなコードは動作しますが、この `Graph` の定義は少し扱いづらいです。
たとえば、任意の `Graph` を引数に取る関数は、 _同時に_ 頂点 `N` と辺 `E` についてもジェネリックとなることになります:
たとえば、任意の `Graph` を引数に取る関数は、 _さらに_ 頂点 `N` と辺 `E` の型についてもジェネリックになる必要があります:

```rust,ignore
fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
```

<!-- Our distance calculation works regardless of our `Edge` type, so the `E` stuff in -->
<!-- this signature is just a distraction. -->
この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる `E` に関連する部分は邪魔でしかありません
<!-- this signature is a distraction. -->
この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる `E` に関連する部分は邪魔になります

<!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
<!-- to form each kind of `Graph`. We can do that with associated types: -->
本当に表現したいことは、それぞれのグラフ( `Graph` )は辺( `E` )や頂点( `N` )で構成されているということです
それは、以下のように関連型を用いて表現することができます:
本当に表現したいのは、それぞれの `Graph` は、辺 `E` と頂点 `N` で構成されていることです
それは、以下のように関連型を用いて表現できます:

```rust
trait Graph {
Expand All @@ -52,15 +52,14 @@ trait Graph {
```

<!-- Now, our clients can be abstract over a given `Graph`: -->
このようにすると、`Graph` を使った関数は以下のように書くことができます:
こうすると、使う側では、個々の `Graph` をより抽象的なものとして扱えます:

```rust,ignore
fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
```

<!-- No need to deal with the `E`dge type here! -->
もう `E` について扱う必要はありません!

ここでは、頂点 `E` 型を扱わずに済んでいます!

<!-- Let’s go over all this in more detail. -->
もっと詳しく見ていきましょう。
Expand All @@ -83,13 +82,13 @@ trait Graph {

<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
<!-- of the trait, with the functions. -->
非常にシンプルですね。関連型には `type` キーワードを使い、そしてトレイトの本体や関数で利用します
非常にシンプルですね。関連型には `type` キーワードを使い、そしてトレイトの本体にある関数で利用します

<!-- These `type` declarations can have all the same thing as functions do. For example, -->
<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
<!-- we could do this: -->
これらの `type` 宣言は、関数で利用できるものと同じものが全て利用できます。
たとえば、 `N` 型が `Display` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます:
たとえば、頂点を表示するため `N` 型には `Display` を実装してほしいなら、以下のように指定できます:

```rust
use std::fmt;
Expand Down Expand Up @@ -141,15 +140,18 @@ impl Graph for MyGraph {
<!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
<!-- gives you an idea of how to implement this kind of thing. We first need three -->
<!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
<!-- more sense to use a different type, that would work as well, we’re just going to -->
<!-- more sense to use a different type, that would work as well, we’re going to -->
<!-- use `struct`s for all three here. -->
この奇妙な実装は、つねに `true` と空の `Vec<Edge>` を返しますが、どのように定義したら良いかのアイデアをくれます。
まず、はじめに3つの `struct` が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの `struct` を用います。

この、いささか単純過ぎる実装では、常に `true` と空の `Vec<Edge>` を返します。
しかし、関連型をどう定義したらよいのかを教えてくれます。
まず、はじめに3つの `struct` が必要です。
グラフのためにひとつ、頂点のためにひとつ、辺のためにひとつです。
もし異なる型を利用するのが適切ならば、そうしても構いません。
今回はこの3つの `struct` を用います。

<!-- Next is the `impl` line, which is just like implementing any other trait. -->
次は `impl` の行です、これは他のトレイトを実装するときと同様です。
<!-- Next is the `impl` line, which is an implementing like any other trait. -->
次は `impl` の行です。
これは他のトレイトを実装するときと同様です。

<!-- From here, we use `=` to define our associated types. The name the trait uses -->
<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
Expand All @@ -163,9 +165,9 @@ impl Graph for MyGraph {
## 関連型を伴うトレイト

<!-- There’s one more bit of syntax we should talk about: trait objects. If you -->
<!-- try to create a trait object from an associated type, like this: -->
すこし触れておきたい構文: トレイトオブジェクト が有ります
もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合:
<!-- try to create a trait object from a trait with an associated type, like this: -->
すこし触れておきたい構文のひとつに、トレイトオブジェクトがあります
もし、トレイトオブジェクトを以下のように関連型を持つトレイトから作成しようとした場合:

```rust,ignore
# trait Graph {
Expand Down Expand Up @@ -207,8 +209,9 @@ let obj = Box::new(graph) as Box<Graph>;

<!-- We can’t create a trait object like this, because we don’t know the associated -->
<!-- types. Instead, we can write this: -->
上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
代わりに以下のように書くことができます:
上のようにしてトレイトオブジェクトを作ることはできません。
なぜなら関連型について知らないからです。
代わりに以下のように書けます:

```rust
# trait Graph {
Expand Down Expand Up @@ -237,5 +240,6 @@ let obj = Box::new(graph) as Box<Graph<N=Node, E=Edge>>;
<!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
<!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
<!-- couldn’t be sure which `impl` to match this trait object to. -->
`N=Node` 構文を用いて型パラメータ `N` にたいして具体的な型 `Node` を指定することができます、`E=Edge` についても同様です。
`N=Node` 構文を用いて型パラメータ `N` に対して具体的な型 `Node` を指定できます。
`E=Edge` についても同様です。
もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの `impl` がマッチするのか定まりません。
34 changes: 0 additions & 34 deletions diff-1.6.0..1.9.0/src/doc/book/associated-types.md

This file was deleted.