Nick Tiller
06/13/2019, 10:14 PMdata class Foo(val width: Float, val height: Float, val x: Float, val y: Float)
data class Bar(val width: Float, val height: Float, val x: Float, val y: Float)
To construct them, I have to write something like
when(clss)
is Foo -> constructFoo()
is Bar -> constructBar()
with each of those construct methods doing the exact same thing (setting the same variable names & types). Is there a structure or something in Kotlin that I can use to make this cleaner?karelpeeters
06/13/2019, 10:20 PMNick Tiller
06/13/2019, 10:35 PMJordan Stewart
06/14/2019, 7:53 AMfun <T> construct(constructor: (Float, Float, Float, Float) -> T) = constructor(1f, 2f, 3f, 4f)
so then constructFoo()
becomes construct(::Foo)
. Not sure what constructFoo and constructBar do under the hood — if they do different things then they should probably be different methods! if they really do only differ by which constructor they call then this could be a way to reduce the duplication, but it’s also a bit more complicated so 🤷♂️ tradeoffs . . .Nick Tiller
06/14/2019, 4:49 PM