Created
October 19, 2011 10:47
-
-
Save j5ik2o/1297955 to your computer and use it in GitHub Desktop.
Scala - 型推論
This file contains 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
$ scala | |
// 代入される値の型により変数の型が決定する | |
scala> val num1 = 1 | |
num1: Int = 1 | |
scala> val num2 = 1.5 | |
num2: Double = 1.5 | |
// 型を明示的に指定するなら、:型で指定する。型アノテーションという。 | |
scala> val num:Number = 1 | |
num: java.lang.Number = 1 | |
scala> var name = "HOGE" | |
name: java.lang.String = HOGE | |
// 異なる型はタイプミスマッチでコンパイルエラーになる。 | |
scala> name = 1 | |
<console>:8: error: type mismatch; | |
found : Int(1) | |
required: java.lang.String | |
name = 1 | |
^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment