Is there any Plugin that will let me generate func...
# announcements
k
Is there any Plugin that will let me generate function that creates an instance of
data class
with its default values? For example I have:
Copy code
data class User(
    val id: String,
    val deviceId: String
)
and plugin will generate:
Copy code
fun userOf(
    id: String = "",
    deviceId: String = "",
): User =
    User(
        userId = userId,
        deviceId = deviceId,
    )
or something similar? (Sorry if wrong channel)
m
what is default value of String? btw you can have such function if you add the default value to constructor
Copy code
data class User(
    val id: String = "",
    val deviceId: String = ""
)
k
I would say empty string isn’t it? When field is nullable then null, when field is
Int
then 0, etc 🙂 Yea, sure default values in constructor is always an option, but those functions I’m using in unit tests and I wouldn’t like to write default values because of correctness of using them in app (it would be easy to forget to initialize some value, because IDE would allow for it)
r
I think you mean https://plugins.jetbrains.com/plugin/10942-kotlin-fill-class -> You get a complete option that adds a sensible default + named parameters when adding a class
k
That looks great, thank you @renatomrcosta! 🙂
✌️ 2