Type Alias

์ƒˆ๋กœ์šด ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ์กด ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค. typealias๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณต์žกํ•œ ํƒ€์ž…์„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

typealias Username = String
typealias Password = String

fun login(username: Username, password: Password) {
    // ...
}

fun main() {
    val username: Username = "myusername"
    val password: Password = "mypassword"
    login(username, password)
}

๋ณต์žกํ•œ ํ‘œํ˜„์„ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ํ‘œํ˜„ํ• ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

typealias UserPrefs = Pair<List<Map<String, Any?>>, Set<String>>

val userPrefs: UserPrefs = listOf(
    mapOf("key1" to 1, "key2" to "value2"),
    mapOf("key3" to true, "key4" to 4.5f)
) to setOf("preference1", "preference2")

Last updated