Well. We have lots and lots of other objects (views, animations, JsonObjects, JsonArrays...), but those are protocol messages that are serialized/deserialized from a socket and they are not too much. I don't mind creating other objects.
If you really want to optimize that much.
HaXe for example has abstracts: http://haxe.org/manual/types-abstract.html
I don’t think it is a very good design, but there are other alternatives.
For example, in the case a class is completely immutable, you can optimize it to do some stuff by value and not by reference.
For example
class Point(val x:Int, val y:Int) { operator fun plus(that:Point):Point = Point(this.x + that.x, this.y + that.y) }
class Object {
val position:Point // this could end being just two play fields that are expanded
}
You could convert this one:
val a = Point(1, 2) + Point(3, 4)
into:
val temp0_x = 1
val temp0_y = 2
val temp1_x = 3
val temp1_y = 4
val temp2_x = 1 + 2
val temp2_y = 3 + 4</span>
Or just create a “struct class” that treats them as value types instead of reference types.
I know that JVM doesn’t support, but you could do stuff like this, and store in arrays with object pools.
Being able to have non-gc value types is a benefit that you have with .NET CLR.
Also with swift you have value types, but also you have predictable object lifecycle without unpredictable stops from the GC.
That would be a very high benefic in gaming, because you could use pretty small primitives without creating garbage: points, rectangles. That includes immutable things like IntRange and so on.
So I don’t see the point that forcing not being able to have “data class” without arguments because you are avoiding to create one single instance in my case, for example, each 50000 frames when I have to create probably dozes just for seralizing is going to help me with GC. I’m already doing lots of object pooling and lazy initialization in critical paths.
Also you have: var, val, fun, and that’s great because code is pretty organized and is pretty good to see things in column, and I have to put obejct or class with classes that falls ino the same category isntead of just put “data class MyDataClassWithoutArguments()”.
I don’t mind creating other object in that case. If I would mind I would use swift, that is going to be opensource pretty soon, with ARC and GC just for detecting leaks with non-weak references.
Also you are not using the “new” keyword for making explicit that you are creating garbage that will require being collected. And I think it is good, since it makes it more hard to read, and disallows you to change a class with a function without removing or adding new everywhere. And also DI, IoC, factories, are allocating without making it explicit. So Probably I know that if I’m using Class0() instead of Object, that () makes me think that maybe I’m creating something, because there is code executing. But again you can even create instances when using a getter that in Kotlin is a normal property that could be confused as a field.
So with GC you will always have to profile your APP, and I don’t think that making able to do “data class Class()” won’t hurt more than other things in the GC direction.