์ถ์ฒ
https://torbjorn.tistory.com/753
[Kotlin] infix call (์ค์ ํธ์ถ)
์ฝํ๋ฆฐ์์๋ ํจ์์ infix ํค์๋๋ฅผ ๋ถ์ฌ ๋ฉ์๋๋ก ์ค์ํธ์ถ์ ํ ์ ์์ต๋๋ค public infix fun A.to(that: B): Pair = Pair(this, that) ์๋ฅผ ๋ค๋ฉด Pair๋ฅผ ๋ง๋๋ to ๋ฉ์๋๊ฐ ์์ต๋๋ค val numToEng = mapOf( 1 to "one",
torbjorn.tistory.com
https://kotlinlang.org/docs/functions.html#infix-notation
Functions | Kotlin
kotlinlang.org
ChatGPT
์ค์ ํธ์ถ(Infix call)์ ์ฝํ๋ฆฐ์์ ํน์ ํ ํจ์ ํธ์ถ ๋ฐฉ์์ด๋ค. ํจ์ ์ด๋ฆ์ ๋ ์ธ์ ์ฌ์ด์ ๋์ด ํธ์ถํ๋ ๊ฒ์ ์๋ฏธํ๋ค. ํจ์ ์ด๋ฆ์ ๋ ํผ์ฐ์ฐ์ ์ฌ์ด์ ์์น์ํค๋ ๋ฐฉ๋ฒ์ธ๋ฐ, ๊ฐ๋จํ ์๊ฐํ๋ฉด ํธ์ถ์ ๋ํ ์ ๊ณผ ๊ดํธ๋ฅผ ์๋ตํ๋ ๊ฒ์ด๋ค.
์ค์ ํธ์ถ์ ํน์ง
1. ๋จ์ผ ๋งค๊ฐ๋ณ์ ํจ์๋ง ์ค์ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
2. ํจ์๊ฐ ๋ฉค๋ฒ ํจ์๋ ํ์ฅ ํจ์๋ก ์ ์ธ๋์ด์ผ ํ๋ค.
3. ํจ์ ์ ์ธ์ infix ํค์๋๋ฅผ ๋ช ์ํด์ผ ํ๋ค.
์์ฃผ ์ฌ์ฉ๋๋ ์ค์ ํจ์
to : ๋ ๊ฐ์ ํ ์(Pair)์ผ๋ก ๋ฌถ์ ๋ ์ฌ์ฉํ๋ค.
val pair = "key" to "value" // "key" to "value"๋ Pair("key", "value")๋ก ๋ณํ
in, !in : ์ปฌ๋ ์ ์ด๋ ๋ฒ์ ์์ ๊ฐ์ด ํฌํจ๋๋์ง ํ์ธํ ๋ ์ฌ์ฉํ๋ค.
if (3 in 1..5) {
println("3์ 1์์ 5 ์ฌ์ด์ ์์ต๋๋ค.")
}
์ค์ ํธ์ถ์ ์ฌ์ฉํ๋ ์ด์
๊ฐ๋ ์ฑ ํฅ์
์ํ์ ํํ์ด๋ ์์ฐ์ค๋ฌ์ด ๋ฌธ์ฅ์ ๊ตฌ์ฑํ๋ ๋ฐ ๋์์ ์ค๋ค.
๊ฐ๊ฒฐํจ
๊ดํธ๋ฅผ ์๋ตํ๊ณ ์ธ์ ์ฌ์ด์ ํจ์ ์ด๋ฆ์ ์์น์์ผ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ ๋ค.
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin] Local Functions ๋ก์ปฌ ํจ์ (3) | 2024.10.15 |
---|---|
[ํ๋ก๊ทธ๋๋ฐ] ๋ฉ์๋ ์ถ์ถ ๋ฆฌํฉํ ๋ง Extract Method (0) | 2024.10.15 |
[Kotlin] Soft Keyword, Hard Keyword, Modifier Keyword (1) | 2024.10.14 |
[Kotlin] ์ ํ์ผ ํ๋์ ์ฌ๋ฌ ํด๋์ค๋ค์ ์ ์ธํ ์ ์๊ฒ ํ์๊น (0) | 2024.10.14 |
[Kotlin] statement(๋ฌธ)์ expression(์) (0) | 2024.10.14 |