빈 ꡬ멍 μ±„μš°κΈ°

[Kotlin] Local Functions 둜컬 ν•¨μˆ˜

⭐⭐기둝 2024. 10. 15. 11:16

좜처

https://kotlintutorial.io/local-functions-in-kotlin/

 

Local functions in Kotlin

In Kotlin there are local functions that can be defined within a larger function. These functions are only visible within this larger function and cannot be accessed from outside. Local functions have the advantage of making code more readable and clear by

kotlintutorial.io

ChatGPT

https://cornswrold.tistory.com/464

 

[Kotlin] μ½”ν‹€λ¦° λ‘œμ»¬ν•¨μˆ˜μ™€ ν™•μž₯

μ½”ν‹€λ¦°μ—μ„œλŠ” ν•¨μˆ˜μ—μ„œ μΆ”μΆœν•œ ν•¨μˆ˜λ₯Ό 원 ν•¨μˆ˜ 내뢀에 μ€‘μ²©μ‹œν‚¬ 수 μžˆλ‹€. κ·Έλ ‡κ²Œ ν•˜λ©΄ 문법적인 λΆ€κ°€ λΉ„μš©μ„ 듀이지 μ•Šκ³  κΉ”λ”ν•˜κ²Œ μ½”λ“œλ₯Ό 쑰직할 수 μžˆλ‹€. (DRY. Don't Repeat Yourself) ν”νžˆ λ°œμƒν•˜λŠ”

cornswrold.tistory.com

https://realkotlin.com/tutorials/2018-06-12-local-functions-in-kotlin/

 

Local functions in Kotlin

Kotlin supports local functions, i.e. it allows you to put a function inside a function.

realkotlin.com

 


둜컬 ν•¨μˆ˜ Local Functions

ν•¨μˆ˜ μ•ˆμ— μ •μ˜λœ ν•¨μˆ˜. μ™ΈλΆ€ ν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ λ³Ό 수 있고, κ·Έ μ™ΈλΆ€μ—μ„œλŠ” 접근이 λΆˆκ°€λŠ₯ν•˜λ‹€. 

 

둜컬 ν•¨μˆ˜μ˜ λͺ©μ 

1. 둜직 μΊ‘μŠν™”

둜컬 ν•¨μˆ˜λŠ” μ™ΈλΆ€ ν•¨μˆ˜ μ•ˆμ—μ„œ 잘 μ •μ˜λ˜κ³  νŠΉν™”ν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•¨μœΌλ‘œμ¨ λ‘œμ§μ„ μΊ‘μŠν™”ν•œλ‹€.

2. 가독성

νŠΉμ • λͺ©μ  달성을 μœ„ν•΄ λͺ…ν™•ν•˜κ²Œ μ •μ˜λœ 둜컬 ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ μ½”λ“œ 가독성이 ν–₯μƒλœλ‹€.

3. μž¬μ‚¬μš©μ„±

μ½”λ“œ 쀑볡 없이 μ™ΈλΆ€ ν•¨μˆ˜ μ•ˆμ—μ„œ μž¬μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€. 였λ₯˜ μ†ŒμŠ€λ₯Ό 쀄이고 더 μΌκ΄€λ˜κ³  μœ μ§€λ³΄μˆ˜ κ°€λŠ₯ν•œ μ½”λ“œλ₯Ό λ§Œλ“ λ‹€.

4. ν•¨μˆ˜ μ£Όμœ„μ˜ μ»¨ν…μŠ€μ— μ ‘κ·Ό

둜컬 ν•¨μˆ˜λŠ” μ™ΈλΆ€ ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ™€ λ³€μˆ˜μ— μ ‘κ·Όν•  수 μžˆλ‹€. 이λ₯Ό 톡해 둜컬 ν•¨μˆ˜μ™€ μ»¨ν…μŠ€νŠΈ κ°„μ˜ μƒν˜Έμž‘μš©μ΄ κ°€λŠ₯ν•΄μ„œ 둜컬 ν•¨μˆ˜μ— μΆ”κ°€ λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.

 

둜컬 ν•¨μˆ˜λŠ” λ©”μ„œλ“œ μΆ”μΆœ λ¦¬νŒ©ν† λ§μ—μ„œλ³΄λ‹€ 더 효율적으둜 μ½”λ“œλ₯Ό κ΄€λ¦¬ν•˜κ³  λΆˆν•„μš”ν•œ 쀑볡을 방지할 수 μžˆλ‹€.

λ©”μ„œλ“œ μΆ”μΆœ λ¦¬νŒ©ν† λ§μ—μ„œ μ’…μ’… λ°œμƒν•˜λŠ” λΆˆν•„μš”ν•œ μ½”λ“œλ₯Ό 쀄일 수 μžˆλŠ” 방법 쀑 ν•˜λ‚˜κ°€ 둜컬 ν•¨μˆ˜μ˜ μ‚¬μš©μ΄λ‹€. 

λ©”μ„œλ“œ μΆ”μΆœ λ¦¬νŒ©ν† λ§μ„ 톡해 μ½”λ“œλ₯Ό 더 μž‘μ€ λ‹¨μœ„λ‘œ λ‚˜λˆŒ λ•Œ, μ™ΈλΆ€ ν΄λž˜μŠ€λ‚˜ λ‚΄λΆ€ 클래슀, 파일둜 ν•¨μˆ˜λ₯Ό λΆ„λ¦¬ν•˜λŠ” λŒ€μ‹  ν•΄λ‹Ή ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλ§Œ μ‚¬μš©λ˜λŠ” 둜직이라면 둜컬 ν•¨μˆ˜λ‘œ μ •μ˜ν•  수 μžˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ μ½”λ“œκ°€ 더 κΉ”λ”ν•˜κ³  λΆˆν•„μš”ν•˜κ²Œ λ§Žμ€ λ©”μ„œλ“œλ₯Ό ν΄λž˜μŠ€λ‚˜ νŒŒμΌμ— μΆ”κ°€ν•  ν•„μš”κ°€ μ—†λ‹€.

 

예

https://cornswrold.tistory.com/464

 

[Kotlin] μ½”ν‹€λ¦° λ‘œμ»¬ν•¨μˆ˜μ™€ ν™•μž₯

μ½”ν‹€λ¦°μ—μ„œλŠ” ν•¨μˆ˜μ—μ„œ μΆ”μΆœν•œ ν•¨μˆ˜λ₯Ό 원 ν•¨μˆ˜ 내뢀에 μ€‘μ²©μ‹œν‚¬ 수 μžˆλ‹€. κ·Έλ ‡κ²Œ ν•˜λ©΄ 문법적인 λΆ€κ°€ λΉ„μš©μ„ 듀이지 μ•Šκ³  κΉ”λ”ν•˜κ²Œ μ½”λ“œλ₯Ό 쑰직할 수 μžˆλ‹€. (DRY. Don't Repeat Yourself) ν”νžˆ λ°œμƒν•˜λŠ”

cornswrold.tistory.com

https://realkotlin.com/tutorials/2018-06-12-local-functions-in-kotlin/

 

Local functions in Kotlin

Kotlin supports local functions, i.e. it allows you to put a function inside a function.

realkotlin.com