Create a gist now

Instantly share code, notes, and snippets.

@kmizu /file0.scala
Last active May 19, 2017

What would you like to do?
ScalaプログラマのためのKotlin入門 ref: http://qiita.com/kmizu/items/248bcf30c44595e6d091
// Scala
object Main extends App {
println("Hello, world!")
}
// Kotlin
fun main(args: Array<String>) {
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
constructor(firstName: String, lastName: String, age: Int) {
this.firstName = firstName
this.lastName = lastName
this.age = age
}
fun getFullName(): String {
return 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<T>(var value: T)
// {} の中に何も書かないなら↑の後の {} も不要
// Scala
class Box[T](var value: T)
// {} の中に何も書かないなら↑の後の {} も不要
// Scala
var s = "abc"
val t = "xyz"
// Kotlin
val cat: Box<out Cat> = Box(Cat())
val animal: Box<out Animal> = cat
val animal: Box<in Animal> = Box(Animal())
val cat: Box<in Cat> = animal
// Scala
val cat: Box[_ <: Cat] = Box(Cat())
val animal: Box[_ <: Animal> = cat
val animal: Box[_ >: Animal] = Box(Animal())
val cat: Box[_ >: Cat] = animal
// Kotlin
val a: MutableList<Int> = mutableListOf()
a.add(2)
a.add(3)
a.add(5)
val b: List<Int> = a
a.add(6)
println(b) //あ!6が入ってる!
// Scala
val squared = numbers.map{ x => x * x }
// Kotlin
val squared = numbers.map{ x -> x * x }
// Kotlin
import java.util.Date
import java.sql.Date as SqlDate
// Scala
import java.util.Date
import java.sql.{Date => SqlDate}
// Kotlin
var s = "abc"
val t = "xyz"
// Scala
var a: Int = 42
val b: Boolean = true
// Kotlin
var a: Int = 42
val b: Boolean = true
val s = a.toString()
val s = a.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment