0

So I really like using type aliases in Kotlin, but I am wondering if my approach to it is considered to be bad, indifferent or good practice. See the following:

typealias MyAlias = Map<String, String>

fun MyAlias() : MyAlias = emptyMap()

fun MyAlias(vararg mapEntries: Pair<String, String>) : MyAlias = {
    val map = mutableMapOf<String, String>
    mapEntries.forEach{map[it.first] = it.second]}
    return map
}

Thoughts? (May be a question subject to opinions, but I have seen some good discussions in the past with very helpful pointers, so I am hoping this will be one of those)

k88
  • 1,858
  • 2
  • 12
  • 33
  • 1
    I'd say indifferent (except it should just be `= mutableMapOf(*mapEntries)`). – Alexey Romanov May 28 '20 at 07:54
  • Where are the extension functions mentioned in the title? I see only top-level functions, not extending anything. – gidds May 28 '20 at 08:11
  • @gidds The extension functions are constructors in this case. – k88 May 28 '20 at 10:02
  • @k88 They're functions, but they're not extending anything.  An [extension function](https://kotlinlang.org/docs/reference/extensions.html#extension-functions) has the receiver type and a dot before the function name. – gidds May 28 '20 at 10:09
  • You're right, but this mimics a constructor. https://stackoverflow.com/questions/46582809/is-it-possible-to-create-extension-constructors-in-kotlin/46582945#46582945 – k88 May 28 '20 at 10:12

0 Answers0