Hi all! I am glad I was able to easily find an act...
# getting-started
a
Hi all! I am glad I was able to easily find an active Kotlin community. I have a simple question re: data classes + constructors, let me know if this is the right or wrong place to ask. I'm not exactly a beginner but maybe this will help others 😛 In short, I want to have a complex constructor for a
data class
. It's easiest to understand by example. I have something like this contrived example:
Copy code
data class ManageUserHelper(val canKick: Boolean, val canBan: Boolean)
but I want to have an initialization flow where those booleans are computed:
Copy code
constructor(val actingUser: User, val targetUser: User, val relationship: Relationship) {
  val canKick = // do something complicated w/ users + relationship
  val canBan = // do something complicated w/ users + relationship, which may depend on the above
  this(canKick, canBan)
}
^ Obviously the above does not compile because the secondary constructor does not immediately delegate to the primary constructor. Until now I have solved this problem by moving that secondary constructor into a
buildFrom
method inside of a
companion object
but that feels messy to me. Is there a more idiomatic way to accomplish what I want? Thanks for the help, and again let me know if there is a better place to ask!
v
angarron: I sometimes place factory functions like your
buildFrom
in the package alongside the class. That makes the code somewhat cleaner