> 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/takeif.md).

# takeIf

```kotlin
// (T) -> it으로 접근가능
inline fun <T> T.takeIf(predicate: (T) -> Boolean)
: T? = if (predicate(this)) this else null

// T로 부터 takeIf 를 사용가능 반환값 T는 Nullable
// Boolean 을 반환하는 predicate 함수를 하나 받아서 그 함수의 반환값이 true 면 자기 자신을 반환하고
// false 면 null을 반환한다.
// 보통 체크하는것을 predicate 라는 이름으로 많이 사용한다. 
```

```kotlin
// false 인 경우
val ari = person.takeIf { it.name == "ari" }
println(ari) // null

// true 인 경
val gold = person.takeIf { it.name == "gold" }
println(gold) // Person(name=gold, age=18)

val default = person.takeIf { it.name == "ari" } ?: Person("ari", 24)
println(default) //Person(name=ari, age=24)
```


---

# 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/takeif.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.
