songining
Published 2022. 10. 28. 13:30
Kotlin inline fun, reified Language/Kotlin

Reified ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ μ œλ„€λ¦­ ν•¨μˆ˜μ—μ„œ Runtime에 νƒ€μž… 정보λ₯Ό μ•Œ 수 μžˆλ‹€. (보톡은 컴파일 μ‹œμ—λ§Œ νƒ€μž…μ„ μ•Œ 수 있고 λŸ°νƒ€μž„μ—μ„œ νƒ€μž… μ •λ³΄λŠ” μ‘΄μž¬ν•˜μ§€ μ•Šκ²Œ λœλ‹€.) 

즉, λŸ°νƒ€μž„μ‹œμ—λ„ νƒ€μž… 정보λ₯Ό 가지고 있기 λ•Œλ¬Έμ— μΈμŠ€ν„΄μŠ€λ₯Ό μ°ΎκΈ°κ°€ 쉽닀. 

 

reified λŠ” inline funκ³Ό ν•¨κ»˜ μ‚¬μš©λ˜λ©° ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜λ‘œ T  νƒ€μž… 클래슀λ₯Ό λ°›λŠ” 것을 μƒλž΅ν•  수 μžˆλ‹€. μΆ”κ°€μ μœΌλ‘œ T 클래슀λ₯Ό λ„˜κ²¨μ€„ ν•„μš”κ°€ μ—†λ‹€λŠ” 점이 쒋은 것 κ°™λ‹€. 

 

λ‹€μŒμ€ R2dbcEntityTemplate 쿼리 싀행을 μœ„ν•œ ν•¨μˆ˜μ΄λ‹€. 

 

κΈ°μ‘΄ μ½”λ“œ)

    private fun <T: Any> executeQuery(build: Select, entityClass: KClass<T>): Flow<T> = r2dbcEntityTemplate.query(
        QueryBuilder(
            SqlRenderer.toString(build)
        ),
        entityClass.java
    ).all().asFlow()

 

λ³€κ²½λœ μ½”λ“œ) 

    private inline fun <reified T: Any> executeQuery(build: Select): Flow<T> = r2dbcEntityTemplate.query(
        QueryBuilder(
            SqlRenderer.toString(build)
        ),
        T::class.java
    ).all().asFlow()

 

'Language > Kotlin' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Kotlin μœ„μž„ ν”„λ‘œνΌν‹° Observable  (0) 2023.09.05
[Kotlin] associateBy, groupBy  (0) 2023.02.21
Kotlin Coroutine Context, Coroutine Builder  (0) 2022.11.01