# 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는 이미 생성해 놓은 변수등을 가지고 연속적인 액션을 취해야 할 때 사용하면 유용하다


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gold.gitbook.io/kotlin/kotlin-standard/apply.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
