Named varargs

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:

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

Why not adding all the parameters with default values?

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

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).

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.