-
Notifications
You must be signed in to change notification settings - Fork 1k
Translation tour ja #1387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Translation tour ja #1387
Changes from all commits
Commits
Show all changes
358 commits
Select commit
Hold shift + click to select a range
9b97eb7
Update _ja/tour/regular-expression-patterns.md
tockri 4553f67
Update _ja/tour/regular-expression-patterns.md
tockri 217695f
Update _ja/tour/extractor-objects.md
tockri b647743
Update _ja/tour/extractor-objects.md
tockri de5d2f9
Update _ja/tour/extractor-objects.md
tockri 5b8a4ad
Update _ja/tour/for-comprehensions.md
tockri cd14c4d
Update _ja/tour/for-comprehensions.md
tockri f8f1364
Update _ja/tour/for-comprehensions.md
tockri 571f282
Update _ja/tour/for-comprehensions.md
tockri ba03ddd
Update _ja/tour/for-comprehensions.md
tockri dd36277
Update _ja/tour/for-comprehensions.md
tockri 47db9ad
Update _ja/tour/generic-classes.md
tockri 852c040
Update _ja/tour/generic-classes.md
tockri e869c94
Update _ja/tour/generic-classes.md
tockri 2a659ea
Update _ja/tour/generic-classes.md
tockri 590b9b9
Japanese translation of TOUR OF SCALA variances
take2webservice 52a8cae
Japanese translation of TOUR OF SCALA upper-type-bounds
take2webservice cb055c0
Japanese translation of TOUR OF SCALA lower-type-bounds
take2webservice 37999b2
Update _ja/tour/generic-classes.md
tockri 03f59ed
Update _ja/tour/multiple-parameter-lists.md
take2webservice 3271984
Japanese translation of TOUR OF SCALA inner-classes
take2webservice 91ec5c7
Japanese translation of TOUR OF SCALA abstract-type-members
take2webservice 2e5bcc7
Japanese translation of TOUR OF SCALA compound-types
take2webservice 29b241d
Japanese translation of TOUR OF SCALA self-types
take2webservice 771ef82
Japanese translation of TOUR OF SCALA implicit-parameters
take2webservice 87acfc9
Update _ja/tour/variances.md
tockri df8fa67
Update _ja/tour/variances.md
tockri 58f10f6
Update _ja/tour/variances.md
tockri 629f34d
Update _ja/tour/variances.md
tockri 7e930ab
Update _ja/tour/variances.md
tockri 8e0db25
Update _ja/tour/variances.md
tockri 2f87c85
Update _ja/tour/variances.md
tockri 47b5645
Update _ja/tour/variances.md
tockri bef6518
Update _ja/tour/variances.md
tockri fd61bbf
Update _ja/tour/variances.md
tockri d82514f
Japanese translation of TOUR OF SCALA implicit-conversions
take2webservice f34c92f
Japanese translation of TOUR OF SCALA polymorphic-methods
take2webservice 659a5b0
Japanese translation of TOUR OF SCALA type-inference
take2webservice 9f9b78d
Japanese translation of TOUR OF SCALA operators
take2webservice 5fe632f
Japanese translation of TOUR OF SCALA by-name-parameters
take2webservice b8a6c8d
Japanese translation of TOUR OF SCALA annotations
take2webservice d5f8c11
Japanese translation of TOUR OF SCALA default-parameter-values
take2webservice a139296
Japanese translation of TOUR OF SCALA named-arguments
take2webservice 6ba235d
Japanese translation of TOUR OF SCALA packages-and-imports
take2webservice c3a4c3c
Japanese translation of TOUR OF SCALA package-objects
take2webservice c0ce5ae
adapt changes on aa641d51873480d9560e310afdcfb133fe1d4558
take2webservice 996b6f1
remove Object extending App from example on japanese file
take2webservice 41178f7
fix grammatical mistake: 'they are instances of' to 'each is an insta…
take2webservice 2778b59
adapt latest changes on tuples document of tour of scala.
take2webservice a031f68
Update _ja/tour/upper-type-bounds.md
tockri a7f79cf
Update _ja/tour/upper-type-bounds.md
tockri a1e055f
Update _ja/tour/lower-type-bounds.md
tockri c6b838b
Update _ja/tour/lower-type-bounds.md
tockri 9b0730e
Update _ja/tour/lower-type-bounds.md
tockri 01ac2ca
Update _ja/tour/lower-type-bounds.md
tockri 3dab31d
Update _ja/tour/lower-type-bounds.md
tockri cc3ec57
Update _ja/tour/lower-type-bounds.md
tockri f397bda
Update _ja/tour/lower-type-bounds.md
tockri 7118dd4
Update _ja/tour/lower-type-bounds.md
tockri 9830878
Update _ja/tour/abstract-type-members.md
tockri 5dcd084
Update _ja/tour/abstract-type-members.md
tockri 76a9e8e
Update _ja/tour/abstract-type-members.md
tockri 3853e93
Update _ja/tour/abstract-type-members.md
tockri 7389bea
Update _ja/tour/abstract-type-members.md
tockri 9b47e8a
Update _ja/tour/abstract-type-members.md
tockri 29cff3c
Update _ja/tour/abstract-type-members.md
tockri fca0364
Update _ja/tour/inner-classes.md
tockri d291349
Update _ja/tour/inner-classes.md
tockri 2c9f72a
Update _ja/tour/inner-classes.md
tockri 482d90e
Update _ja/tour/inner-classes.md
tockri dde02fc
Update _ja/tour/inner-classes.md
tockri 23b2344
Update _ja/tour/inner-classes.md
tockri f8de5bb
Update _ja/tour/inner-classes.md
tockri 141c7c6
Update _ja/tour/compound-types.md
tockri 8c07f53
Update _ja/tour/compound-types.md
tockri 4c0dc3b
Update _ja/tour/compound-types.md
tockri 7d6a7ad
Update _ja/tour/compound-types.md
tockri 3d29ea4
Update _ja/tour/compound-types.md
tockri 6481baf
Update _ja/tour/compound-types.md
tockri 4d97063
Update _ja/tour/compound-types.md
tockri 77a722c
Update _ja/tour/self-types.md
tockri e1cab5e
Update _ja/tour/self-types.md
tockri 9da0361
Update _ja/tour/self-types.md
tockri 42c1e0d
Update _ja/tour/self-types.md
tockri 4cdeb57
Update _ja/tour/self-types.md
tockri 1c2374e
Update _ja/tour/implicit-parameters.md
tockri f818da0
Update _ja/tour/implicit-parameters.md
tockri 84ab042
Update _ja/tour/implicit-parameters.md
tockri 57266f2
Update _ja/tour/implicit-parameters.md
tockri 24e4a6c
Update _ja/tour/implicit-parameters.md
tockri fc8167f
Update _ja/tour/implicit-parameters.md
tockri 8b8354f
Update _ja/tour/implicit-parameters.md
tockri 9532f24
Update _ja/tour/implicit-parameters.md
tockri bf07773
Update _ja/tour/implicit-parameters.md
tockri 0af24c7
Update _ja/tour/implicit-parameters.md
tockri 8b1152e
Update _ja/tour/implicit-conversions.md
tockri ae17f5e
Update _ja/tour/implicit-conversions.md
tockri 731a43e
Update _ja/tour/implicit-conversions.md
tockri 7aececd
Update _ja/tour/implicit-conversions.md
tockri 15a7249
Update _ja/tour/implicit-conversions.md
tockri 773badc
Update _ja/tour/implicit-conversions.md
tockri 44b1139
Update _ja/tour/implicit-conversions.md
tockri 6784f92
Update _ja/tour/implicit-conversions.md
tockri af26494
Update _ja/tour/implicit-conversions.md
tockri 0059b55
Update _ja/tour/implicit-conversions.md
tockri 5ffd5e2
Update _ja/tour/polymorphic-methods.md
take2webservice 21b6e15
Update _ja/tour/polymorphic-methods.md
tockri 85627c1
Update _ja/tour/polymorphic-methods.md
tockri 7a2320c
Update _ja/tour/polymorphic-methods.md
tockri 3877971
Update _ja/tour/polymorphic-methods.md
tockri c3db42c
Update _ja/tour/polymorphic-methods.md
tockri b0e045d
Update _ja/tour/polymorphic-methods.md
tockri 943b878
Update _ja/tour/type-inference.md
tockri b658b9b
Update _ja/tour/type-inference.md
tockri 9c35586
Update _ja/tour/type-inference.md
tockri a8034fa
Update _ja/tour/type-inference.md
tockri ab90954
Update _ja/tour/type-inference.md
tockri 44b3d8e
Update _ja/tour/type-inference.md
tockri 37d2bb3
Update _ja/tour/type-inference.md
tockri a5f5b53
Update _ja/tour/operators.md
tockri 4bcd828
Update _ja/tour/operators.md
tockri fb29d19
Update _ja/tour/operators.md
tockri a496eb2
Update _ja/tour/operators.md
tockri 41417f4
Update _ja/tour/operators.md
tockri 09a0c32
Update _ja/tour/operators.md
tockri b8c296b
Update _ja/tour/operators.md
tockri a2041c4
Update _ja/tour/operators.md
tockri 58036ba
Update _ja/tour/by-name-parameters.md
tockri 3936af8
Update _ja/tour/by-name-parameters.md
tockri 9c0e642
Update _ja/tour/by-name-parameters.md
tockri 720fcbc
Update _ja/tour/by-name-parameters.md
tockri 5fd0923
Update _ja/tour/by-name-parameters.md
tockri e17b616
Update _ja/tour/by-name-parameters.md
tockri 77df071
Update _ja/tour/annotations.md
tockri bfb32cd
Update _ja/tour/annotations.md
tockri 1c90bd9
Update _ja/tour/annotations.md
tockri 7d371b1
Update _ja/tour/annotations.md
tockri 5c8bb1b
Update _ja/tour/annotations.md
tockri 5b95d3b
Update _ja/tour/annotations.md
tockri c1471cd
Update _ja/tour/annotations.md
tockri 5a970f7
Update _ja/tour/annotations.md
tockri 4ed05a7
Update _ja/tour/annotations.md
tockri 2519b5f
Update _ja/tour/default-parameter-values.md
tockri a1b79ef
Update _ja/tour/default-parameter-values.md
tockri 7b91956
Update _ja/tour/default-parameter-values.md
tockri 57e12ed
Update _ja/tour/default-parameter-values.md
tockri a5db342
Update _ja/tour/named-arguments.md
tockri f0c94ef
Update _ja/tour/named-arguments.md
tockri b44d174
Update _ja/tour/named-arguments.md
tockri 57cc250
Update _ja/tour/packages-and-imports.md
tockri ee97eea
Update _ja/tour/packages-and-imports.md
tockri a97b1cc
Update _ja/tour/packages-and-imports.md
tockri 8401486
Update _ja/tour/packages-and-imports.md
tockri 2b3774f
Update _ja/tour/packages-and-imports.md
tockri e87d43a
Update _ja/tour/package-objects.md
tockri 76f2686
Update _ja/tour/package-objects.md
tockri 786f862
Update _ja/tour/package-objects.md
tockri 0826e33
Update _ja/tour/package-objects.md
tockri 4aaf146
Update _ja/tour/package-objects.md
tockri 203f6f6
Update _ja/tour/package-objects.md
tockri 0eb3e3a
Update _ja/tour/tuples.md
tockri 640559b
Update _ja/tour/tuples.md
tockri 0c57048
Update _ja/tour/tuples.md
tockri 45b2778
Update _ja/tour/tuples.md
tockri 1aaaaef
Update _ja/tour/tuples.md
tockri aa3ee0d
Update _ja/tour/tuples.md
tockri 7259020
Update _ja/tour/tuples.md
tockri 32261f0
Update _ja/tour/tuples.md
tockri 369dbde
訳が不自然なところを訂正
tockri 552b87f
よくよく
tockri da4d603
冗長すぎる部分とコンビニ敬語の指摘
tockri 09e4e2a
再確認
tockri fdcbbdc
再確認
tockri 624e3f0
再確認
tockri 0c61586
再確認
tockri 72dae7c
再確認
tockri 76fb205
再確認
tockri 127a0f6
再確認
tockri 6543402
再確認
tockri ea2aab7
リンク記法の誤り訂正
tockri 20fd0df
不変→非変に変更
tockri 470d3dc
訳し漏れとか、意味の通じにくいところとか再確認
tockri b3f69e7
再確認
tockri ed567e3
再確認
tockri 9bfa23f
再確認
tockri 26602a0
再確認
tockri b5aa7ee
再確認
tockri 2fe7b5f
自己型→自分型
tockri 2584008
adapt changes in 9afd159f5556ad9a772e8a264e000d30d910c168
take2webservice 44fa183
再確認
take2webservice 3cd5f38
てにをは修正
take2webservice 83d2635
不変=>非変, ジェネリックタイプ=>ジェネリック型
take2webservice 0495a48
別がいっぱいあって気持ち悪いので、他に変更
take2webservice 9b6981a
微修正
take2webservice 4e15e26
language属性追加、不要な「です」削除
take2webservice 5ce4947
ワード漏れ
take2webservice 6abc981
オペレータ=>演算子
take2webservice 0f9a11c
add language:ja
take2webservice 1e30446
微修正
take2webservice b58bf06
add language: ja
take2webservice fb364ff
再確認
tockri aaf656f
再確認
tockri 1cc2151
再確認
tockri 0942e77
Update _ja/tour/annotations.md
take2webservice 05303f6
原文に合わせ不要な改行を削除
take2webservice 637b23e
remove untranslated file
take2webservice a36eaf4
enumeratorsは複数の値を生成しうる
take2webservice ae81508
Update _ja/tour/for-comprehensions.md
take2webservice 9ae2596
Update _ja/tour/higher-order-functions.md
take2webservice 53484c7
Update _ja/tour/higher-order-functions.md
take2webservice 1f060b9
Update _ja/tour/implicit-conversions.md
take2webservice 3a838fd
Update _ja/tour/implicit-conversions.md
take2webservice f601b5f
Update _ja/tour/implicit-conversions.md
take2webservice 779d899
Update _ja/tour/mixin-class-composition.md
take2webservice 2f5b43d
Update _ja/tour/multiple-parameter-lists.md
take2webservice 83d164c
Update _ja/tour/multiple-parameter-lists.md
take2webservice aa23d5d
Update _ja/tour/nested-functions.md
take2webservice 04a2a94
Update _ja/tour/package-objects.md
take2webservice c6f7ce2
Update _ja/tour/package-objects.md
take2webservice 9fa868b
Update _ja/tour/package-objects.md
take2webservice c13f87c
Update _ja/tour/pattern-matching.md
take2webservice c063377
Update _ja/tour/pattern-matching.md
take2webservice 806b839
Update _ja/tour/pattern-matching.md
take2webservice cec1bf1
Update _ja/tour/pattern-matching.md
take2webservice c537688
Update _ja/tour/pattern-matching.md
take2webservice 1e87554
Update _ja/tour/pattern-matching.md
take2webservice 34925ab
Update _ja/tour/polymorphic-methods.md
take2webservice f89ee26
Update _ja/tour/polymorphic-methods.md
take2webservice e23dad7
Update _ja/tour/regular-expression-patterns.md
take2webservice ad6353a
Update _ja/tour/singleton-objects.md
take2webservice c62cb06
Update _ja/tour/tour-of-scala.md
take2webservice 4daa0b5
Update _ja/tour/tour-of-scala.md
take2webservice d111112
Update _ja/tour/tour-of-scala.md
take2webservice 69f6ea0
Update _ja/tour/traits.md
take2webservice 45b232f
Update _ja/tour/unified-types.md
take2webservice 5480bf7
Update _ja/tour/tour-of-scala.md
take2webservice b9de5c1
Update _ja/tour/tour-of-scala.md
take2webservice a9f8baf
Update _ja/tour/tour-of-scala.md
take2webservice 53b104d
Update _ja/tour/singleton-objects.md
take2webservice 9fe0f2a
add untranslated file to compile
take2webservice c2010ff
Update _ja/tour/tour-of-scala.md
take2webservice 5d2dfc6
Update _ja/tour/basics.md
take2webservice e2fccf1
Update _ja/tour/basics.md
take2webservice dfeb7ce
Update _ja/tour/basics.md
take2webservice d13c4d1
Update _ja/tour/basics.md
take2webservice 10bbc1a
Update _ja/tour/basics.md
take2webservice e6e9a72
Update _ja/tour/basics.md
take2webservice 584c2e6
Update _ja/tour/classes.md
take2webservice df31554
Update _ja/tour/classes.md
take2webservice 11b26fb
Update _ja/tour/higher-order-functions.md
take2webservice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
tour: | ||
languages: [ba, es, ko, pt-br, pl, zh-cn, th, ru] | ||
languages: [ba, es, ko, pt-br, pl, zh-cn, th, ru, ja] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--- | ||
layout: tour | ||
title: 抽象型メンバー | ||
language: ja | ||
|
||
discourse: true | ||
|
||
partof: scala-tour | ||
num: 23 | ||
next-page: compound-types | ||
previous-page: inner-classes | ||
topics: abstract type members | ||
prerequisite-knowledge: variance, upper-type-bound | ||
|
||
redirect_from: "/tutorials/tour/abstract-types.html" | ||
redirect_from: "/tour/abstract-types.html" | ||
--- | ||
|
||
トレイトや抽象クラスのような抽象型は抽象型メンバーを持つことができます。 | ||
これは具体的な実装で実際の型を定義するという意味です。 | ||
こちらが例です。 | ||
|
||
```tut | ||
trait Buffer { | ||
type T | ||
val element: T | ||
} | ||
``` | ||
こちらでは、抽象型`type T`を定義しています。それは`element`の型を記述するために使われます。このトレイトを抽象クラスで継承し、より具体的にするために上限型境界を`T`に追加することができます。 | ||
|
||
```tut | ||
abstract class SeqBuffer extends Buffer { | ||
type U | ||
type T <: Seq[U] | ||
def length = element.length | ||
} | ||
``` | ||
`T`の上限型境界の定義に出てきた、更に別の抽象型`U`の使い方に気をつけてください。この`class SeqBuffer`はこのバッファーの中にシーケンスのみを保持することができます。それは型`T`は新しい抽象型`U`を使った`Seq[U]`のサブタイプであると記述しているからです。 | ||
|
||
抽象型メンバーを持つトレイトと[クラス](classes.html)は無名クラスのインスタンス化と組み合わせてよく使われます。 | ||
これを説明するために、今から整数のリストを参照するシーケンスバッファーを扱うプログラムを見てみます。 | ||
|
||
```tut | ||
abstract class IntSeqBuffer extends SeqBuffer { | ||
type U = Int | ||
} | ||
|
||
|
||
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer = | ||
new IntSeqBuffer { | ||
type T = List[U] | ||
val element = List(elem1, elem2) | ||
} | ||
val buf = newIntSeqBuf(7, 8) | ||
println("length = " + buf.length) | ||
println("content = " + buf.element) | ||
``` | ||
ここで、ファクトリー`newIntSeqBuf`は抽象型`T`を具体的な型`List[Int]`に設定するために、`IntSeqBuf`(つまり`new IntSeqBuffer`)を無名クラスで実装します。 | ||
|
||
抽象型メンバーをクラスの型パラメータに変えることも、その逆も可能です。以下は上記コードの型パラメータのみを使うバージョンです。 | ||
|
||
```tut | ||
abstract class Buffer[+T] { | ||
val element: T | ||
} | ||
abstract class SeqBuffer[U, +T <: Seq[U]] extends Buffer[T] { | ||
def length = element.length | ||
} | ||
|
||
def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer[Int, Seq[Int]] = | ||
new SeqBuffer[Int, List[Int]] { | ||
val element = List(e1, e2) | ||
} | ||
|
||
val buf = newIntSeqBuf(7, 8) | ||
println("length = " + buf.length) | ||
println("content = " + buf.element) | ||
``` | ||
ここでは(`+T <: Seq[U]`)をメソッド`newIntSeqBuf`から戻されるオブジェクトの具体的なシーケンス実装の型を隠すために [変位指定アノテーション](variances.html)を使わなければなりません。さらに、抽象型メンバをパラメータで置換することができないケースがあります。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
--- | ||
layout: tour | ||
title: アノテーション | ||
language: ja | ||
|
||
discourse: true | ||
|
||
partof: scala-tour | ||
|
||
num: 32 | ||
next-page: default-parameter-values | ||
previous-page: by-name-parameters | ||
|
||
redirect_from: "/tutorials/tour/annotations.html" | ||
--- | ||
|
||
アノテーションはメタ情報と定義を関連づけます。例えば、メソッドの前のアノテーション`@deprecated`はメソッドが使われたらコンパイラに警告を出力させます。 | ||
``` | ||
object DeprecationDemo extends App { | ||
@deprecated("deprecation message", "release # which deprecates method") | ||
def hello = "hola" | ||
|
||
hello | ||
} | ||
``` | ||
これはコンパイルされますが、コンパイラは警告"there was one deprecation warning"を出力します。 | ||
|
||
アノテーション句はそれに続く最初の定義か宣言に適用されます。定義と宣言の前には1つ以上のアノテーション句を置くことができます。これらの句の順番は重要ではありません。 | ||
|
||
|
||
## エンコーディングの正確性を保証するアノテーション | ||
条件が一致しない場合にコンパイルを失敗させるアノテーションもあります。例えば、アノテーション`@tailrec`はメソッドが[末尾再帰](https://en.wikipedia.org/wiki/Tail_call)であると保証します。末尾再帰ではメモリ使用量が一定になります。こちらは階乗を計算するメソッドの中での使われ方です。 | ||
```tut | ||
import scala.annotation.tailrec | ||
|
||
def factorial(x: Int): Int = { | ||
|
||
@tailrec | ||
def factorialHelper(x: Int, accumulator: Int): Int = { | ||
if (x == 1) accumulator else factorialHelper(x - 1, accumulator * x) | ||
} | ||
factorialHelper(x, 1) | ||
} | ||
``` | ||
`factorialHelper`メソッドは`@tailrec`を持ちます。`@tailrec`はメソッドが実際に末尾再帰であると保証します。もし`factorialHelper`の実装を以下のように変更すれば、失敗し | ||
``` | ||
import scala.annotation.tailrec | ||
|
||
def factorial(x: Int): Int = { | ||
@tailrec | ||
def factorialHelper(x: Int): Int = { | ||
if (x == 1) 1 else x * factorialHelper(x - 1) | ||
} | ||
factorialHelper(x) | ||
} | ||
``` | ||
"Recursive call not in tail position"というメッセージを受け取ります。 | ||
|
||
|
||
## コード生成に影響するアノテーション | ||
`@inline`のようなアノテーションは生成されたコードに影響します(つまり、アノテーションを使わなかった場合とでjarファイルのバイト数が異なる場合があります)。インライン化とは、メソッドを呼び出している箇所にメソッド本体のコードを挿入することを意味します。結果のバイトコードはより長くなりますが、上手くいけば実行が早くなります。アノテーション`@inline`を使ってもメソッドのインライン化が保証されるわけではありません。しかし、生成されたコードのサイズに関するヒューリスティックスが満たされた場合に限りコンパイラにインライン化をさせます。 | ||
|
||
### Javaのアノテーション ### | ||
Javaと相互運用するScalaのコードを書いている時、記述するアノテーション構文は少し違います。 | ||
|
||
**注:** Javaアノテーションを使う場合、`-target:jvm-1.8`オプションを使ってください。 | ||
|
||
Javaには[アノテーション](https://docs.oracle.com/javase/tutorial/java/annotations/)の形をしたユーザー定義メタデータがあります。Javaのアノテーションの特徴は、要素の初期化のために名前と値のペアを指定する必要があることです。例えば、クラスのソースを追いかけるためのアノテーションが必要なとき、以下のように定義するかもしれません。 | ||
|
||
``` | ||
@interface Source { | ||
public String URL(); | ||
public String mail(); | ||
} | ||
``` | ||
|
||
そして、それは以下のように適用されます。 | ||
|
||
``` | ||
@Source(URL = "http://coders.com/", | ||
mail = "support@coders.com") | ||
public class MyClass extends TheirClass ... | ||
``` | ||
|
||
Scalaでのアノテーションの適用はコンストラクタの呼び出しと似ています。Javaのアノテーションをインスタンス化するためには名前付き引数を使う必要があります。 | ||
|
||
``` | ||
@Source(URL = "http://coders.com/", | ||
mail = "support@coders.com") | ||
class MyScalaClass ... | ||
``` | ||
|
||
アノテーションが(デフォルト値を除き)要素を1つだけ含む場合、この構文はかなり退屈です。そのため慣例により、名前が`value`と指定されていれば、コンストラクタのような構文でJavaに適用できます。 | ||
|
||
``` | ||
@interface SourceURL { | ||
public String value(); | ||
public String mail() default ""; | ||
} | ||
``` | ||
|
||
そして以下のように適用します。 | ||
|
||
``` | ||
@SourceURL("http://coders.com/") | ||
public class MyClass extends TheirClass ... | ||
``` | ||
|
||
この場合、Scalaでも同じことができます。 | ||
|
||
``` | ||
@SourceURL("http://coders.com/") | ||
class MyScalaClass ... | ||
``` | ||
|
||
`mail`要素はデフォルト値つきで定義されているので、明示的に値を指定する必要がありません。しかしながら、もし値を指定する必要がある場合、Javaでは2つのスタイルを混ぜて組み合わせることはできません。 | ||
|
||
``` | ||
@SourceURL(value = "http://coders.com/", | ||
mail = "support@coders.com") | ||
public class MyClass extends TheirClass ... | ||
``` | ||
|
||
Scalaはこの点においてより柔軟です。 | ||
|
||
``` | ||
@SourceURL("http://coders.com/", | ||
mail = "support@coders.com") | ||
class MyScalaClass ... | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
layout: tour | ||
title: Automatic Type-Dependent Closure Construction | ||
language: ja | ||
|
||
discourse: true | ||
|
||
partof: scala-tour | ||
--- | ||
|
||
Scala allows parameterless function names as parameters of methods. When such a method is called, the actual parameters for parameterless function names are not evaluated and a nullary function is passed instead which encapsulates the computation of the corresponding parameter (so-called *call-by-name* evaluation). | ||
|
||
The following code demonstrates this mechanism: | ||
|
||
object TargetTest1 extends Application { | ||
def whileLoop(cond: => Boolean)(body: => Unit): Unit = | ||
if (cond) { | ||
body | ||
whileLoop(cond)(body) | ||
} | ||
var i = 10 | ||
whileLoop (i > 0) { | ||
println(i) | ||
i -= 1 | ||
} | ||
} | ||
|
||
The function whileLoop takes two parameters `cond` and `body`. When the function is applied, the actual parameters do not get evaluated. But whenever the formal parameters are used in the body of `whileLoop`, the implicitly created nullary functions will be evaluated instead. Thus, our method `whileLoop` implements a Java-like while-loop with a recursive implementation scheme. | ||
|
||
We can combine the use of [infix/postfix operators](operators.html) with this mechanism to create more complex statements (with a nice syntax). | ||
|
||
Here is the implementation of a loop-unless statement: | ||
|
||
object TargetTest2 extends Application { | ||
def loop(body: => Unit): LoopUnlessCond = | ||
new LoopUnlessCond(body) | ||
protected class LoopUnlessCond(body: => Unit) { | ||
def unless(cond: => Boolean) { | ||
body | ||
if (!cond) unless(cond) | ||
} | ||
} | ||
var i = 10 | ||
loop { | ||
println("i = " + i) | ||
i -= 1 | ||
} unless (i == 0) | ||
} | ||
The `loop` function just accepts a body of a loop and returns an instance of class `LoopUnlessCond` (which encapsulates this body object). Note that the body didn't get evaluated yet. Class `LoopUnlessCond` has a method `unless` which we can use as a *infix operator*. This way, we achieve a quite natural syntax for our new loop: `loop { < stats > } unless ( < cond > )`. | ||
|
||
Here's the output when `TargetTest2` gets executed: | ||
|
||
i = 10 | ||
i = 9 | ||
i = 8 | ||
i = 7 | ||
i = 6 | ||
i = 5 | ||
i = 4 | ||
i = 3 | ||
i = 2 | ||
i = 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.