๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Kotlin][ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ] ๋ฉค๋ฒ„ ์ฐธ์กฐ

์ถœ์ฒ˜

Kotlin In Action

https://antonioleiva.com/function-references-kotlin/

 

Function references in Kotlin: use functions as lambdas everywhere

Function references allow to use regular functions as lambdas. With them you can write cleaner code in a more functional style.

antonioleiva.com

https://inpa.tistory.com/entry/JAVA8-%E2%98%95-%EB%9E%8C%EB%8B%A4%EC%8B%9D%EC%9D%84-%EB%8D%94-%EC%A7%A7%EA%B2%8C-%EB%A9%94%EC%86%8C%EB%93%9C-%EC%B0%B8%EC%A1%B0Method-Reference

 

โ˜• ๋žŒ๋‹ค์‹์„ ๋” ์งง๊ฒŒ - ๋ฉ”์†Œ๋“œ::์ฐธ์กฐ ๋ฌธ๋ฒ•

๋žŒ๋‹ค์‹ ๋ฉ”์†Œ๋“œ::์ฐธ์กฐ ์ž๋ฐ”์˜ ๋žŒ๋‹คํ‘œํ˜„์‹์„ ํ†ตํ•ด ์ฝ”๋“œ ์ •์˜๋ฅผ ํ˜์‹ ์ ์œผ๋กœ ์ค„์—ฌ์ฃผ์—ˆ์ง€๋งŒ ์ด๋ณด๋‹ค ๋” ๊ฐ„๋žตํ•˜๊ฒŒ ์ค„์ด๋Š” ๋ฌธ๋ฒ•์ด ์žˆ๋‹ค. ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ(Method Reference)๋Š” ๋ง ๊ทธ๋Œ€๋กœ ์‹คํ–‰ํ•˜๋ ค๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ

inpa.tistory.com

https://kotlinlang.org/docs/reflection.html#bound-function-and-property-references

 

Reflection | Kotlin

 

kotlinlang.org

 

 

 


๋ฉค๋ฒ„ ์ฐธ์กฐ

์†์„ฑ์ด๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ::(์ด์ค‘ ์ฝœ๋ก )์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜ ๊ฐ’์„ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๋žŒ๋‹ค์™€ ์„œ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํ•จ์ˆ˜๋ฅผ ๋žŒ๋‹ค๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค๋ผ๊ณ  ์„ค๋ช…ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

 

์žฅ์  - ์™œ ์“ฐ๋Š”๊ฐ€ -> ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์ ํ•ฉํ•˜๋‹ค

1. ์ฝ”๋“œ ๊ฐ„๊ฒฐ์„ฑ

๋žŒ๋‹ค๋ฅผ ๋Œ€์ฒดํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ์–ด์„œ, ๋ฉค๋ฒ„ ์ฐธ์กฐ๊ฐ€ ๋žŒ๋‹ค๋ฅผ ๋” ์งง๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

์งง๋‹ค๊ณ  ๊ฐ€๋…์„ฑ์ด ๋‹ค ์ข‹์•„์ง€๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋•Œ๋กœ๋Š” ๋žŒ๋‹ค๊ฐ€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋ณด๊ธฐ์— ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ๋” ์‰ฌ์šด ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. 

2. ์žฌ์‚ฌ์šฉ์„ฑ

๊ธฐ์กด์˜ ๋ฉ”์„œ๋“œ๋‚˜ ์†์„ฑ์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ์— ์žฌ์‚ฌ์šฉ์„ฑ์ด ์ข‹๋‹ค. ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋‚˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

3. ์ˆœ์ˆ˜์„ฑ ์œ ์ง€

๋ฉค๋ฒ„ ์ฐธ์กฐ๋Š” ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค. ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ค‘์š”ํ•œ ์ˆœ์ˆ˜ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์œ ์šฉํ•˜๋‹ค.

๐Ÿค” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ž€ : ๋™์ผํ•œ ์ž…๋ ฅ์— ๋Œ€ํ•ด ํ•ญ์ƒ ๋™์ผํ•œ ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์™ธ๋ถ€ ์ƒํƒœ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜

 

๋ฉค๋ฒ„ ์ฐธ์กฐ ๋ฌธ๋ฒ•

{ํด๋ž˜์Šค}::{๋ฉค๋ฒ„}

 

์˜ˆ์‹œ

Person::age

run(::salute)
//salute()๋Š” ์ตœ์ƒ์œ„ ํ•จ์ˆ˜์ด๋‹ค.

 

๋ฉค๋ฒ„ ์ฐธ์กฐ์™€ ๋žŒ๋‹ค๋Š” ๊ฐ™๋‹ค?

์—ํƒ€ ๋ณ€ํ™˜์œผ๋กœ ์ธํ•ด ๋ฉค๋ฒ„ ์ฐธ์กฐ์™€ ๋žŒ๋‹ค๋Š” ๋™์ผํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ฉค๋ฒ„ ์ฐธ์กฐ์™€ ๋žŒ๋‹ค๋Š” ์„œ๋กœ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋‹ค.

๋žŒ๋‹ค ํ‘œํ˜„์‹

val printNameLambda: (Person) -> Unit = { person -> println(person.name) }

 

๋ฉค๋ฒ„ ์ฐธ์กฐ

val printNameReference: (Person) -> Unit = Person::name

 

๋ฐ”์šด๋“œ ๋ฉค๋ฒ„ ์ฐธ์กฐ

์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ธ์Šคํ„ด์Šค(๊ฐ์ฒด)์˜ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ.

๋‹ค์‹œ ๋งํ•˜์ž๋ฉด ํŠน์ • ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ ์†์„ฑ์ด๋‚˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ. ์—ฌ๊ธฐ์„œ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ํŠน์ • ๊ฐ์ฒด์™€ ๊ทธ ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๋‚˜ ์†์„ฑ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

๋ฐ”์šด๋“œ ๋ฉค๋ฒ„ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉค๋ฒ„ ์ฐธ์กฐ ์‹œ์— ๋”ฐ๋กœ ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์˜ˆ์‹œ

val alice = Person("Alice")
val greetAlice = alice::greet
greetAlice("Hello") // "Alice, Hello!" ์ถœ๋ ฅ

 

์—ฌ๊ธฐ์„œ alice::greet๋Š” alice ๊ฐ์ฒด์˜ greet ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฐ”์šด๋“œ ๋ฉค๋ฒ„ ์ฐธ์กฐ์ด๋‹ค.

์ด๋ฅผ ํ†ตํ•ด greetAlice๋Š” alice ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.