typealias FruitFilter = (Fruit) -> Boolean
fun filterFruits(fruits: List<Fruit>, filter: FruitFilter) {
}
data class UltraSuperGuardianTribe (
val name: String
)
typealias USGTMap = Map<String, UltraSuperGuardianTribe>
val person = Person("peron", 100)
val (name, age) = person
println("이름 : ${name}, 나이 : ${age}")
val person = Person("peron", 100)
val name = person.component1()
val age = person.component2()
class Person(
val name: String,
val age: Int
) {
operator fun component1(): String {
return this.name
}
operator fun component2(): Int {
return this.age
}
}
val numbers = listOf(1, 2, 3, 4, 5)
run {
numbers.forEach { number ->
if (number == 3) {
return@run // break
}
println(number)
}
}
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEach { number ->
if (number == 3) {
return@forEach // continue
}
println(number)
}
loop@ for (i in 1..100) {
for (j in 1..100) {
if (j == 2) {
break@loop
}
print("${i} ${j}")
}
}
fun getNumberOrNull(): Int? {
return if (number <= 0) {
null
} else {
number
}
}
fun getNumberOrNullV2(): Int? {
return number.takeIf { it > 0 }
}
fun getNumberOrNullV3(): Int? {
return number.takeUnless { it <= 0 }
}
참고