> For the complete documentation index, see [llms.txt](https://gold.gitbook.io/kotlin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gold.gitbook.io/kotlin/kotlin-standard/apply.md).

# apply

```kotlin
//T.() -> 마찬가지로 자기자신에 키워드 없이 그냥 접근가능
//Unit을 반환하는 함수를 받아서 apply는 자기자신을 반환한다
inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
```

```kotlin
val person = Person().apply {
    name = "ari"
    age = 24
}

println(person) //Person(name=ari, age=24)
```

```kotlin
AlertDialog.Builder(this).apply {
    setTitle(getString(R.string.title))
    setMessage(getString(R.string.message))
    setPositiveButton(getString(R.string.ok))
    { dlg, _ -> dlg.dissmis() })
    create()
    show()
}
```

* type.apply { ... }
* type T를 입력받아 { ... } 안을 수행하고 자기 자신을 반환한다
* apply 는 생성과 동시에 어떤 추가적인 작업을 해줘야 할 때 유용하다
* with는 이미 생성해 놓은 변수등을 가지고 연속적인 액션을 취해야 할 때 사용하면 유용하다
