kotlin
  • Kotlin
  • Variable
  • Function
  • First Class Citizen
  • High Order Function
  • Pure Function
  • Call by Value, Call by Name
  • Null Safe
  • Generic
  • Expression
    • if - else
    • when
    • try - catch
    • while
    • for
  • Class
    • abstract
    • constructor
    • Data Class
    • Default Value
    • enum
    • inheritance
    • interface
    • open
    • property
    • sealed
  • Kotlin Standard
    • let
    • with
    • apply
    • run
    • also
    • takeIf
    • takeUnless
    • use
    • repeat
  • Collections
    • Mutable
    • Immutable
    • Collections
    • Aggregate operations
      • any
      • all
      • count
      • fold
      • foldRight
      • fold vs foldRight
      • forEach
      • forEachIndexed
      • max
      • maxBy
      • min
      • minBy
      • none
      • reduce
      • reduceRight
      • sum
      • sumBy
    • Filtering operations
      • drop
      • dropLast
      • dropWhile
      • dropLastWhile
      • filter
      • filterNot
      • slice
      • take
      • takeLast
      • takeWhile
    • Mapping operations
      • map
      • mapIndexed
      • flatMap
      • groupBy
    • Elements operations
      • contains
      • elementAt
      • elementAtOrElse
      • elementAtOrNull
      • first
      • firstOrNull
      • indexOf
      • indexOfFirst
      • indexOfLast
      • last
      • lastIndexOf
      • lastOrNull
      • single
      • singleOrNull
    • Generation operations
      • partition
      • plus
      • minus
      • zip
      • unzip
    • Ordering operations
      • reversed
      • sorted
      • sortedBy
      • sortedDescending
      • sortedByDescending
Powered by GitBook
On this page
  1. Kotlin Standard

let

data class Person(var name: String = "gold", var age: Int = 20)
val person = Person()

//let의 원형
//T에 한에서 사용할 수 있다 
//함수를 파라미터로 받고 (T) 요런식으로 괄호가 쳐져있는 것들은 it을 사용할 수 있다. return은 R타입.
//block(this) -> 블록의 마지막줄의 값을 리턴한다 라고 이해하면 된다. ex) T블록의 마지막줄을 리턴한다.
inline fun <T, R> T.let(block: (T) -> R) : R = block(this)

val name = person.let {it.name} //자기자신을 it 이라는 키워드를 통해 참조할 수 있다.
println(name) //gold

val person: Person? = Person()
val name = person?.let {it.name}
println(name) //gold

val person: Person? = null
val name: person?.let {it.name}
println(name) //null

val person: Person? = null
val name: person?.let {it.name} ?: "silver" //person객체가 null이므로 엘비스 오퍼레이터 뒤의 값인 "silver"를 반환
println(name) //silver

fun sendEmail(email: String) {
    //Do Something...
}

var email: String? = null

email?.let { sendEmail(it) } //null이 아닌 경우에만 실행

  • type T(자신)을 block으로 감싼다.

  • block 안에서 자기자신에게 접근 할때는 it을 사용한다.

  • 표현식으로 사용한다면 반환값 R은 괄호 제일 마지막 라인의 값이 된다.

  • ?. 을 사용해 null check를 할 수 있다.

  • null일 경우에 ?.을 사용하면 null을 반환한다.

PreviousKotlin StandardNextwith

Last updated 6 years ago