# Named varargs

**URL:** https://discuss.kotlinlang.org/t/named-varargs/16475
**Category:** Support
**Created:** [February 24, 2020, 7:09pm UTC](https://discuss.kotlinlang.org/t/named-varargs/16475 "2020-02-24T19:09:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lucasqueiroz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lucasqueiroz/32/5589_2.png) [@lucasqueiroz](https://discuss.kotlinlang.org/u/lucasqueiroz)
#### Post date: [February 24, 2020, 7:09pm UTC](https://discuss.kotlinlang.org/t/named-varargs/16475/1 "2020-02-24T19:09:18Z")

</div>

Is there any way to create “named varargs”, something close to Python’s kwargs?  
To give a better context, I’m trying to create a “Factory” class, where I have a `build()` function that returns an instance of a data class. But I want to receive named arguments inside the build function to be able to override the default construction of the instance.

Example:

```auto
data class SampleEntity(
    val id: Int,
    val name: String
)

class SampleEntityFactory {
    companion object {
        fun build(kwargs: <named varargs here>): SampleEntity {
            return SampleEntity(
                id = kwargs.getOrDefault("id", 0),
                name = kwargs.getOrDefault("name", "Foo Bar")
            )
        }
    }
}

```

Is there any way I can achieve this, even if it is with a lib or reflection?

Thanks

---

<div class="post-metadata">

### Author: ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)
#### Post date: [February 24, 2020, 8:50pm UTC](https://discuss.kotlinlang.org/t/named-varargs/16475/2 "2020-02-24T20:50:37Z")

</div>

Why not adding all the parameters with default values?

```auto
data class SampleEntity(
    val id: Int,
    val name: String
)

class SampleEntityFactory {
    companion object {
        fun build(id: Int = 0, name: String = "Foo Bar"): SampleEntity {
            return SampleEntity(
                id = id,
                name = name
            )
        }
    }
}

```

But if you really want to use something like `kwargs` you could use a `Map` but you will lose type-safety

---

<div class="post-metadata">

### Author: ![lucasqueiroz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lucasqueiroz/32/5589_2.png) [@lucasqueiroz](https://discuss.kotlinlang.org/u/lucasqueiroz)
#### Post date: [February 24, 2020, 9:16pm UTC](https://discuss.kotlinlang.org/t/named-varargs/16475/3 "2020-02-24T21:16:39Z")

</div>

It is a possibility, but then it doesn’t let me create a `BaseFactory` interface, where I could declare the `build()` function, and also it seems kinda repetitive, and when you have a data class with a lot of variables, the amount of parameters inside the `build()` function will be huge.  
I was looking for something like the `copy` method that Kotlin implements, but I’m not sure if it is actually implemented somewhere (like an interface) or if the compiler creates the methods (Or other approaches).

---

<div class="post-metadata">

### Author: ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)
#### Post date: [February 24, 2020, 11:05pm UTC](https://discuss.kotlinlang.org/t/named-varargs/16475/4 "2020-02-24T23:05:04Z")

</div>

You could use reflection to invoke your constructor, and take a `Map<String, Any>`.  
In general I’d discourage that as well as classes which have many constructor parameters, but I think that’s as close as you get. Obviously you won’t have compile time type checks.
