Ruckus
04/24/2018, 7:27 PMclass Basis(
var xx: Double = 1.0, var xy: Double = 0.0, var xz: Double = 0.0,
var yx: Double = 0.0, var yy: Double = 1.0, var yz: Double = 0.0,
var zx: Double = 0.0, var zy: Double = 0.0, var zz: Double = 1.0
) {
constructor(x: Vector3 = Vector3.x(), y: Vector3 = Vector3.y(), z: Vector3 = Vector3.z()) : this(
x.x, x.y, x.z,
y.x, y.y, y.z,
z.x, z.y, z.z
)
companion object {
fun ident() = Basis()
fun scale(x: Double = 1.0, y: Double = 1.0, z: Double = 1.0) = Basis(xx = x, yy = y, zz = z)
}
}
The ident
function in the companion object is calling the secondary constructor, and so is creating extra Vector3
objects when there is no need to. Wouldn't it make more sense to use the primary constructor whenever possible (that is the point of primary, is it not)?Ruckus
04/24/2018, 7:33 PMrook
04/24/2018, 7:34 PMIf the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword
https://kotlinlang.org/docs/reference/classes.htmlrook
04/24/2018, 7:35 PMRuckus
04/24/2018, 7:36 PMrook
04/24/2018, 7:37 PMRuckus
04/24/2018, 7:38 PMrook
04/24/2018, 7:38 PMRuckus
04/24/2018, 7:39 PMRuckus
04/24/2018, 7:39 PMkarelpeeters
04/24/2018, 7:40 PMrook
04/24/2018, 7:40 PMRuckus
04/24/2018, 7:40 PMINVOKESPECIAL transformation/transform/Basis.<init> (Ltransformation/transform/Vector3;Ltransformation/transform/Vector3;Ltransformation/transform/Vector3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Which translates to
@NotNull
public final Basis ident() {
return new Basis((Vector3)null, (Vector3)null, (Vector3)null, 7, (DefaultConstructorMarker)null);
}
karelpeeters
04/24/2018, 7:43 PMrook
04/24/2018, 7:44 PMrook
04/24/2018, 7:46 PMRuckus
04/24/2018, 7:47 PMrook
04/24/2018, 7:47 PMRuckus
04/24/2018, 7:49 PMrook
04/24/2018, 7:50 PMrook
04/24/2018, 7:50 PMclass Foo (bar: Boolean = true) {
constructor(thing: Int = 0) : this(thing == 1)
companion object {
fun ident() = Foo()
}
}
rook
04/24/2018, 7:51 PMFoo()
complains about overload resolution ambiguityRuckus
04/24/2018, 7:52 PMrook
04/24/2018, 7:52 PMRuckus
04/24/2018, 7:52 PMRuckus
04/24/2018, 7:53 PMrook
04/24/2018, 7:53 PMRuckus
04/24/2018, 7:54 PMRuckus
04/24/2018, 7:54 PMRuckus
04/24/2018, 7:54 PM