|
| 1 | +このページは Scala 2と Scala 3の両方に対応しています。 |
| 2 | + |
| 3 | +## インストールなしで今すぐ Scala を試す! |
| 4 | + |
| 5 | +Scala を今すぐ試すには<a href="https://scastie.scala-lang.org/pEBYc5VMT02wAGaDrfLnyw" target="_blank">ブラウザで「Scastie」を使います。</a> |
| 6 | +Scastieは Scala のサンプルコードがどのように動作するかをブラウザで簡単に試すことができるオンライン「playground」で、様々なバージョンのコンパイラと公開されているライブラリが利用できます。 |
| 7 | + |
| 8 | +> Scastie は Scala 2と Scala 3の両方をサポートしていますが、デフォルトでは Scala 3になっています。Scala 2のスニペットをお探しの方は、[こちら](https://scastie.scala-lang.org/MHc7C9iiTbGfeSAvg8CKAA)をご覧ください。 |
| 9 | +
|
| 10 | +## コンピューターに Scala をインストールする |
| 11 | + |
| 12 | +Scala をインストールすると、コンパイラやビルドツールなどの様々なコマンドラインツールが同時にインストールされます。 |
| 13 | +私たちは必須ツール全てを確実にインストールするために「Coursier」の使用をお勧めしますが、それらを手動でインストールすることもできます。 |
| 14 | + |
| 15 | +### Scala インストーラーを使う(推奨) |
| 16 | + |
| 17 | +Scala のインストーラーは[Coursier](https://get-coursier.io/docs/cli-overview)というツールで、コマンドは`cs`です。このツールを使うと、JVM と標準 Scala ツールがシステムにインストールされます。 |
| 18 | +以下の手順でお使いのシステムにインストールしてください。 |
| 19 | + |
| 20 | +<div class="main-download"> |
| 21 | + <div id="download-step-one"> |
| 22 | + <p>Follow <a href="https://get-coursier.io/docs/cli-installation.html#native-launcher" target="_blank">the instructions to install the <code>cs</code> launcher</a> then run:</p> |
| 23 | + <p><code>$ ./cs setup</code></p> |
| 24 | + </div> |
| 25 | +</div> |
| 26 | + |
| 27 | + |
| 28 | +`cs setup` は JVM の管理だけでなく、便利なコマンドラインツールもインストールします: |
| 29 | + |
| 30 | +- JDK (インストール済みでなければ) |
| 31 | +- [sbt](https://www.scala-sbt.org/) ビルドツール |
| 32 | +- [Ammonite](https://ammonite.io/), 強化された REPL |
| 33 | +- [scalafmt](https://scalameta.org/scalafmt/), コードフォーマッター |
| 34 | +- `scalac` (Scala 2 コンパイラー) |
| 35 | +- `scala` (Scala 2 の REPL と script runner). |
| 36 | + |
| 37 | +`cs`の詳細については、[ccoursier-cliのドキュメント](https://get-coursier.io/docs/cli-overview)をご覧ください。 |
| 38 | + |
| 39 | +> 現在`cs setup` は Scala 2 のコンパイラとランナー(それぞれ`scalac`と`scala`コマンド)をインストールします。ほとんどのプロジェクトでは Scala 2 と Scala 3 の両方に対応したビルドツールを使用しているので、通常は問題になりません。しかし、以下の追加コマンドを実行することで、Scala 3のコンパイラとランナーをコマンドラインツールとしてインストールすることができます。 |
| 40 | +> ``` |
| 41 | +> $ cs install scala3-compiler |
| 42 | +> $ cs install scala3 |
| 43 | +> ``` |
| 44 | +
|
| 45 | +### 手動でのインストール |
| 46 | +
|
| 47 | +Scala プロジェクトのコンパイル、実行、テスト、パッケージ化に必要なツールは Java と sbt の2つだけです。 |
| 48 | +Java のバージョンは8または11です。 |
| 49 | +これらを手動でインストールするには: |
| 50 | +
|
| 51 | +1. Java 8または11がインストールされていない場合は、[Oracle Java 8](https://www.oracle.com/java/technologies/javase-jdk8-downloads.html)、[Oracle Java 11](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html)、または[AdoptOpenJDK 8/11](https://adoptopenjdk.net/)からJavaをダウンロードしてください。Scala と Java の互換性の詳細については、[JDK Compatibility](/overviews/jdk-compatibility/overview.html)を参照してください。 |
| 52 | +1. [sbt](https://www.scala-sbt.org/download.html)をインストールしてください。 |
| 53 | +
|
| 54 | +## sbt で「Hello World」プロジェクトを作成する |
| 55 | +
|
| 56 | +sbt をインストールしたら、次のセクションで説明する Scala プロジェクトを作成する準備ができました。 |
| 57 | +
|
| 58 | +プロジェクトの作成には、コマンドラインまたはIDEを使用します。コマンドラインに慣れている方は、その方法をお勧めします。 |
| 59 | +
|
| 60 | +### コマンドラインを使う |
| 61 | +
|
| 62 | +sbt は、Scala のビルドツールです。sbt は、Scala のコードをコンパイルし、実行し、テストします。(sbt は、Scala コードのコンパイル、実行、テストを行います(ライブラリの公開やその他多くのタスクも可能です)。 |
| 63 | +
|
| 64 | +sbt で新しい Scala プロジェクトを作成するには、以下の手順で行います: |
| 65 | +
|
| 66 | +1. 空のディレクトリに`cd`する. |
| 67 | +1. Scala 3プロジェクトを作成する場合は`sbt new scala/scala3.g8`、Scala 2プロジェクトを作成する場合は`sbt new scala/hello-world.g8`というコマンドを実行します。これは、GitHub からプロジェクトのテンプレートを引き出します。このとき"target"という名前のディレクトリが作成されますが無視してください。 |
| 68 | +1. プロンプトが表示されたら、アプリケーションの名前を`hello-world`とします。これにより、"hello-world "というプロジェクトが作成されます。 |
| 69 | +1. それでは、生成されたばかりのものを見てみましょう: |
| 70 | +
|
| 71 | +``` |
| 72 | +- hello-world |
| 73 | + - project (sbt が利用するファイル) |
| 74 | + - build.properties |
| 75 | + - build.sbt (sbt のビルド定義) |
| 76 | + - src |
| 77 | + - main |
| 78 | + - scala (あなたの Scala のコードはすべてここに入る) |
| 79 | + - Main.scala (プログラムのエントリーポイント) <-- 今、必要なのはこれだけです |
| 80 | +``` |
| 81 | +
|
| 82 | +sbt についての詳しいドキュメントは、[Scala Book](/scala3/book/tools-sbt.html)([Scala 2バージョンはこちら](/overviews/scala-book/scala-build-tool-sbt.html))と、[sbt の公式ドキュメント](https://www.scala-sbt.org/1.x/docs/ja/index.html)に掲載されています。 |
| 83 | +
|
| 84 | +### IDEを使う |
| 85 | +
|
| 86 | +このページの残りの部分を読み飛ばして、[Building a Scala Project with IntelliJ and sbt](/getting-started/intellij-track/building-a-scala-project-with-intellij-and-sbt.html)に進んでも問題ありません。 |
| 87 | +
|
| 88 | +## hello-world プロジェクトを開く |
| 89 | +
|
| 90 | +IDE を使ってプロジェクトを開いてみましょう。最もポピュラーなものは IntelliJ と VSCode です。どちらも豊富な IDE 機能を備えていますが、他にも[多くのエディタ](https://scalameta.org/metals/docs/editors/overview.html)を使うことができます。 |
| 91 | +
|
| 92 | +### IntelliJ を使う |
| 93 | +
|
| 94 | +1. [IntelliJ Community Edition](https://www.jetbrains.com/idea/download/)をダウンロードしてインストールします。 |
| 95 | +1. IntelliJ プラグインのインストール方法にしたがって、[Scala プラグインをインストール](https://www.jetbrains.com/help/idea/managing-plugins.html)します。 |
| 96 | +1. `build.sbt`ファイルを開き、*Open as a project*を選択します。 |
| 97 | +
|
| 98 | +### VSCode で metals を使う |
| 99 | +
|
| 100 | +1. [VSCode](https://code.visualstudio.com/Download)をダウンロードする |
| 101 | +1. [Marketplace](https://marketplace.visualstudio.com/items?itemName=scalameta.metals)から Metals extension をインストールする |
| 102 | +1. 次に、build.sbt ファイルがあるディレクトリを開きます(前の指示に従った場合は、hello-world というディレクトリになるはずです)。プロンプトが表示されたら、「Import build」を選択します。 |
| 103 | +
|
| 104 | +> |
| 105 | +>[Metals](https://scalameta.org/metals) は、[VS Codeや Atom、Sublime Textなど](https://scalameta.org/metals/docs/editors/overview.html)のエディタで Scala のコードを書くためのサポートを提供する「Scala 言語サーバ」であり、Language Server Protocol を使用しています。 |
| 106 | +> Metalsはバックグラウンドで[BSP(Build Server Protocol)](https://build-server-protocol.github.io/)を使用してビルドツールと通信します。Metalsの仕組みについては、[「Write Scala in VS Code, Vim, Emacs, Atom and Sublime Text with Metals」](https://www.scala-lang.org/2019/04/16/metals.html)を参照してください。 |
| 107 | +
|
| 108 | +### ソースコードをいじってみよう |
| 109 | +
|
| 110 | +この2つのファイルを IDE で表示します: |
| 111 | +
|
| 112 | +- _build.sbt_ |
| 113 | +- _src/main/scala/Main.scala_ |
| 114 | +
|
| 115 | +次のステップでプロジェクトを実行すると、_src/main/scala/Main.scala_のコードを実行するために、_build.sbt_の設定が使われます。 |
| 116 | +
|
| 117 | +## Hello World の実行 |
| 118 | +
|
| 119 | +IDE の使用に慣れている場合は、IDE から_Main.scala_のコードを実行することができます。 |
| 120 | +
|
| 121 | +または、以下の手順でターミナルからアプリケーションを実行できます: |
| 122 | +
|
| 123 | +1. `hello-world`ディレクトリに`cd` する |
| 124 | +1. `sbt`コマンドを実行し、sbt console を開く |
| 125 | +1. `~run`と打ち込む。 `~` は全てのコマンドの前に追加できるコマンドで、ファイル保存を検知してコマンドを再実行してくれるため、編集・実行・デバッグのサイクルを高速に行うことができます。sbt はここでも`target`ディレクトリを生成しますが無視してください。 |
| 126 | +
|
| 127 | +このプロジェクトの`run`を止めたければ、`[Enter]`を押して`run`コマンドを中断します。その後`exit`と入力するか`[Ctrl+D]`を押すと sbt が終了し、コマンドラインプロンプトに戻ります。 |
| 128 | +
|
| 129 | +## 次のステップ |
| 130 | +
|
| 131 | +上記のチュートリアルの後は以下の教材に進んでください。 |
| 132 | +
|
| 133 | +* [The Scala Book](/scala3/book/introduction.html) (Scala 2版は[こちら](/overviews/scala-book/introduction.html))はScalaの主な機能を紹介する短いレッスンのセットを提供します。 |
| 134 | +* [The Tour of Scala](/tour/tour-of-scala.html) Scalaの機能を一口サイズで紹介します。 |
| 135 | +* [Learning Resources](/learn.html) オンラインのインタラクティブなチュートリアルやコースです。 |
| 136 | +* [books](/books.html) 人気のある Scalaの 書籍を紹介します |
| 137 | +* [The migration guide](/scala3/guides/migration/compatibility-intro.html) 既存の Scala 2コードベースを Scala 3に移行する際に役立ちます。 |
| 138 | +
|
| 139 | +## ヘルプが必要な人は |
| 140 | +他の Scala ユーザーとすぐに連絡を取りたい場合は、多くのメーリングリストやリアルタイムのチャットルームがあります。これらのリソースのリストや、どこに問い合わせればよいかについては、[コミュニティページ](https://scala-lang.org/community/)をご覧ください。 |
| 141 | +
|
| 142 | +### (日本語のみ追記) |
| 143 | +Scala について日本語で質問したい場合、Twitterでつぶやくと気づいた人が教えてくれます。 |
| 144 | +
|
| 145 | +<!-- Hidden elements whose content are used to provide OS-specific download instructions. |
| 146 | + -- This is handled in `resources/js/functions.js`. |
| 147 | + --> |
| 148 | +<div style="display:none" id="stepOne-linux"> |
| 149 | + <code class="hljs">$ curl -fLo cs https://git.io/coursier-cli-linux && chmod +x cs && ./cs setup </code> <br> |
| 150 | +</div> |
| 151 | +
|
| 152 | +<div style="display:none" id="stepOne-unix"> |
| 153 | + <p>Follow <a href="https://get-coursier.io/docs/cli-installation" target="_blank">the instructions to install the <code>cs</code> launcher</a> then run:</p> |
| 154 | + <p><code>$ ./cs setup</code></p> |
| 155 | +</div> |
| 156 | +
|
| 157 | +<div style="display:none" id="stepOne-osx"> |
| 158 | + <div class="highlight"> |
| 159 | + <code class="hljs">$ brew install coursier/formulas/coursier && cs setup </code> <br> |
| 160 | + </div> |
| 161 | + <p>または、Homebrewを使用しない場合は</p> |
| 162 | + <div class="highlight"> |
| 163 | + <code class="hljs">$ curl -fLo cs https://git.io/coursier-cli-macos && chmod +x cs && (xattr -d com.apple.quarantine cs || true) && ./cs setup</code> <br> |
| 164 | + </div> |
| 165 | +</div> |
| 166 | +
|
| 167 | +<div style="display:none" id="stepOne-windows"> |
| 168 | + <p><a href="https://git.io/coursier-cli-windows-exe">the Scala installer for Windows</a>を、ダウンロードして実行すてください。</p> |
| 169 | +</div> |
0 commit comments