Skip to content

Instantly share code, notes, and snippets.

@j5ik2o
Created October 19, 2011 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j5ik2o/1297955 to your computer and use it in GitHub Desktop.
Save j5ik2o/1297955 to your computer and use it in GitHub Desktop.
Scala - 型推論
$ 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