Travis Griggs
04/13/2021, 9:58 PMTheNewClass:TheParentClass {
var a:Foo
var b:Bar
var c:Baz
}
But primary constructors make it more interesting. Now I can declare them right in one line, which I don't mind when it's just that guy, something like:
data class Point<T>(val x:T, val y:T) { }
Now I've got to decode two types of object declarations when looking at them. But then I can mix and mash them:
class BLEConduit(val peripheral:BLEPeripheral):ConnectionConduit {
var sendBuffer = ubyteArrayOf()
var receiveBuffer:UByteArray? = null
So now I'm having to parse (and rationalize) two lists, to get at what the "guts" of an object is. And if I mix parameters without val/var in the primary constructor, any conciseness really breaks down. Are there a set of rules/guidelines others follow on when to put what where? To me, Kotlin's properties/init/constructors feels very evolutionary, like they started in one place, and then evolved things, but left backwards compatibility in place, and now there's less of a clear/idiomatic way to declare what an object is and how it gets initialized.nanodeath
04/13/2021, 10:02 PMTravis Griggs
04/13/2021, 10:08 PMnanodeath
04/13/2021, 10:23 PMTravis Griggs
04/13/2021, 10:39 PMnanodeath
04/13/2021, 10:44 PMCasey Brooks
04/13/2021, 10:55 PMclass BLEConduit(
val peripheral:BLEPeripheral
): ConnectionConduit {
var sendBuffer = ubyteArrayOf()
var receiveBuffer:UByteArray? = null
}
Even if there’s only 1 constructor parameter, it goes on the next line, and the superclass also goes on the next line after parameters. So the superclass or opening class bracket gives me that nice, clear separation between properties that are passed in vs the ones that are in the class body.
In addition to using val
whenever possible, I also usually put those “class body” properties as extensions to the class, rather than within the class itself. If everything is immutable, then there’s really nothing to hide, so those properties don’t need to be directly inside the class. For the most part, the only “class body” variables I use regularly are some kind of property delegate, or lateinit var
and injectedokarm
04/14/2021, 11:36 AMBLEConduit(val peripheral:BLEPeripheral):ConnectionConduit
really is difficult to decode.