Skip to content
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

mdocの機能を使ってエラーを自動で表示したい #1028

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 9 additions & 17 deletions src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Scalaのクラス定義は次のような形を取ります。

```scala
```scala mdoc
class <クラス名> '(' (<引数名1> : <引数型1>, <引数名2>: <引数型2> ...)? ')' {
(<フィールド定義> | <メソッド定義> )*
}
Expand Down Expand Up @@ -52,15 +52,15 @@ class Point(val x: Int, val y: Int) {

先ほど既にメソッド定義の例として`+`メソッドの定義が出てきましたが、一般的には、次のような形をとります。

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? def <メソッド名> '('
(<引数名> : 引数型 (, 引数名 : <引数型>)*)?
')': <返り値型> = <本体>
```

実際にはブロック式を使った以下の形式を取ることが多いでしょう。

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? def <メソッド名> '('
(<引数名> : 引数型 (, 引数名 : <引数型>)*)?
')': <返り値型> = {
Expand Down Expand Up @@ -93,7 +93,7 @@ p1 + p2

メソッドは以下のように複数の引数リストを持つように定義することができます。

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? def <メソッド名> '('
(<引数名> : 引数型 (, 引数名 : <引数型>)*)?
')'( '('
Expand Down Expand Up @@ -137,7 +137,7 @@ fun(3)

フィールド定義は

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? (val | var) <フィールド名>: <フィールド型> = <初期化式>
```

Expand All @@ -147,15 +147,15 @@ fun(3)

その時点では実装を書くことができず、後述する継承の際に、メソッドやフィールドの実装を与えたいということがあります。このような場合に対応するため、Scalaでは抽象メンバーを定義することができます。抽象メンバーは、メソッドの場合とフィールドの場合があり、メソッドの場合は次のようになります。

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? def <メソッド名> '('
(<引数名> : 引数型 (, 引数名 : <引数型>)*)?
')': <返り値型>
```

フィールドの定義は次のようになります。

```scala
```scala mdoc
(private([this | <パッケージ名>])? | protected([<パッケージ名>])?)? (val | var) <フィールド名>: <フィールド型>
```

Expand All @@ -180,7 +180,7 @@ Scalaではトレイトという仕組みで複数の実装の継承を実現し

ここでは通常のScalaのクラスの継承について説明します。Scalaでのクラスの継承は次のような構文になります。

```scala
```scala mdoc
class <クラス名> <クラス引数> (extends <スーパークラス>)? (with <トレイト名>)* {
(<フィールド定義> | <メソッド定義>)*
}
Expand Down Expand Up @@ -212,22 +212,14 @@ new BPrinter().print()

ここで`BPrinter#print()`の`override`キーワードをはずすと、次のようにメッセージを出力して、**コンパイルエラー**になります。

```scala
```scala mdoc:nest:fail
class BPrinter() extends APrinter {
def print(): Unit = {
println("B")
}
}
```

```
[error] .../Printer.scala:8:7: `override` modifier required to override concrete member:
[error] def print(): Unit (defined in class APrinter)
[error] def print(): Unit = {
[error] ^
[error] one error found
```

このような仕組みのない言語ではしばしば、気付かずに既存のメソッドをオーバーライドするつもりで新しいメソッドを定義してしまうというミスがありますが、
Scalaでは`override`キーワードを使って言語レベルでこの問題に対処しているのです。

Expand Down