hmm. Is there a name for a kind've backwards opera...
# compiler
g
hmm. Is there a name for a kind've backwards operation for destructure, a kind of implied structuring? eg
Copy code
sealed class Configuration {
    data class Customer(val name: String, val age: Int): Configuration()
    data class User(val email: String): Configuration()
}

class Whatever {
  fun doStuff(config: Configuration): Int {
    //...
  }
}
and I would like kotlin to do something automagical with
Copy code
w.doStuff("bob", 42) // --calls Customer.<init>, doStuff()
w.doStuff("<mailto:jane_doe@email.com|jane_doe@email.com>") // calls User.<init>, doStuff()
I feel like I use sealed-classes as a way to expand a functions capability a lot. Right now (at least 3x in recent memory), I'll add exension functions to call the constructor and the method for you. eg
Copy code
fun Whatever.doStuff(name: String, age: Int) = doStuff(Customer(name, age))
but this is kinda tedious, and i feel like the compiler could do it for me.
maybe an
@JvmImpliedOverloads
for kotlinc?
d
No, kotlin doesn't have such operation (and I suppose it won't). Theoretically you can achieve that with some compiler plugin, but it steel very questionable feature. For example, what if you have two inheritors of sealed class with same types in constructors?
Copy code
sealed class Configuration {
    data class Customer(val name: String, val age: Int): Configuration()
    data class User(val email: String, val id: Int): Configuration()
}
1