//kiss.kts
fun greet() = "hello"
println(greet())
val message: Int = greet() // Error
//type mismatch: inferred type is String but Int was expected
fun goodGreet(): String = "Hello"
fun Keyword
return 키워드는 사용 불가
Unit
이라는 특별한 타입을 사용. Java의 void
와 대응된다.//inferunit.kts
fun sayHello() = println("Well, hello")
val message: String = sayHello() //ERROR
//type mismatch: inferred type is Unit but String was expected
fun goodSayHello(): Unit = println("Well, hello")
val message: Unit = sayHello()
println("The result of sayHello is $message")
Unit
타입은 toString()
, equals()
, hashCode()
메소드를 가지고 있다.코틀린은 함수나 메소드에 파라미터의 타입을 명시하도록 했다.
// passingarguments.kts
fun greet(name: String): String = "Hello $name"
println(greet("Eve")) //Hello Eve