Instantly share code, notes, and snippets.

# kmizu/file0.scala Last active Jan 15, 2018

What would you like to do?
ScalaプログラマのためのKotlin入門 ref: https://qiita.com/kmizu/items/248bcf30c44595e6d091
 // Scala object Main extends App { println("Hello, world!") }
 // Kotlin fun main(args: Array) { println("Hello, world!") }
 // Scala val foo = if (bar < 42) { "abc" } else { "xyz" }
 // Kotlin val foo = if (bar < 42) { "abc" } else { "xyz" }
 // Scala for (number <- numbers) { println(number) }
 // Kotlin for (number in numbers) { println(number) }
 // Scala for(i <- 0 until 100) { println(i) }
 // Kotlin for (i in 0 until 100) { println(i) }
 // Scala for(i <- 10 to 0 by - 1) println(i) for(i <- 0 until 100 by 2) println(i) for(i <- 1 to 100) println(i)
 // Kotlin for (i in 99 downTo 0) println(i) // for (int i = 99; i >= 0; i--) for (i in 0 until 100 step 2) println(i) // for (int i = 0; i < 100; i += 2) for (i in 1..100) println(i) // for (int i = 1; i <= 100; i++)
 // Scala val s = a match { case 0 => "abc" case 1 | 2 => "def" case _ => "xyz" }
 // Kotlin val s = when (a) { 0 -> "abc" 1, 2 -> "def" else -> "xyz" }
 // Scala var s: String = "abc" val t: String = "xyz"
 // Scala val foo = new Foo
 // Kotlin val foo = Foo()
 // Scala class Person(val firstName, val lastName, age: Int) { def fullName: String = firstName + " " + lastName }
 // Kotlin class Person(val firstName: String, val lastName: String, var age: Int) { val fullName: String get() = firstName + " " + lastName }
 // Scala val age = person.age person.age = age + 1
 // Kotlin val age = person.age person.age = age + 1
 // Scala class Person { ... def fullName: String = firstName + " " + lastName }
 // Kotlin class Person { ... val fullName: String get() { return firstName + " " + lastName } }
 // Kotlin class Person(val firstName: String, val lastName: String, var age: Int) { ... }
 // Scala class Person(val firstName: String, val lastName: String, var age: Int) { ... }
 // Kotlin var s: String = "abc" val t: String = "xyz"
 // Scala class Person(val firstName: String, val lastName: String, var age: Int) { def this(firstName: String, lastName: String){ this(firstName, lastName, 0) } ... }
 // Kotlin class Person(val firstName: String, val lastName: String, var age: Int) { constructor(firstName: String, lastName: String) : this(firstName, lastName, 0) { } ... }
 // Kotlin class Person(val firstName: String, val lastName: String, var age: Int = 0) { ... }
 // Scala class Person(val firstName: String, val lastName: String, var age: Int = 0) { ... }
 // Kotlin class Person(val firstName: String, val lastName: String) { ... // 時間経過を表すメソッド fun elapse(years: Int): Int { age += years return age } }
 // Scala class Person(val firstName: String, val lastName: String) { ... // 時間経過を表すメソッド def elapse(years: Int): Int = { age += years age } }
 // Scala trait Foo { val bar: Int def baz(qux: String) }
 // Kotlin interface Foo { val bar: Int fun baz(qux: String) }
 // Kotlin class Box(var value: T) // {} の中に何も書かないなら↑の後の {} も不要
 // Scala class Box[T](var value: T) // {} の中に何も書かないなら↑の後の {} も不要
 // Scala var s = "abc" val t = "xyz"
 // Kotlin val cat: Box = Box(Cat()) val animal: Box = cat val animal: Box = Box(Animal()) val cat: Box = animal
 // Scala val cat: Box[_ <: Cat] = Box(Cat()) val animal: Box[_ <: Animal> = cat val animal: Box[_ >: Animal] = Box(Animal()) val cat: Box[_ >: Cat] = animal