sometimes I wish I could destructuring directly to...
# language-proposals
e
sometimes I wish I could destructuring directly to existing `var`s
Copy code
var hovered = false
var held = false
if(..) {
    val (_, ho, he) = buttonBehavior(columnHitRect, columnId)
    hovered = ho
    held = he
}
4
s
Should be easy to come up with a usecase then
e
I dont know, like
(hovered, held) = buttonBehavior(columnHitRect, columnId)
s
But you dont use the fact that they are preexisting. You might as well just join declaration and assignment in your example
e
I shortened the code, there is an
if
in between
s
Much better 😉
e
sorry 😛
s
e
done and voted, thanks for suggesting the link, Johannes
d
I often wish I could destruct directly into a constructor A degenerate case, consider 2 classes with the same named properties but not related by inherance. Why I cannot do this val class1 = Class1(.. create as usual) val class2 = Class2( * class1 ) THe special case where the constructor for class2 is a vararg and class1 is an array happen to work 🙂
Why you ask ? COnsider a common class hierarcy open class A( val a1 : String , val b1 : Integer ) {} open class B( val c1: String , val a1 : String , val b1 : Integer ) : A( a1,b1) {} .... class Z( val one-property: String , .... Must Enumerate all 25 properties) : Y( Must enumerate all 25 ) { /* <facepalm > */ } instead why not val a = A( "a1" , 1 ) val b = B( "c1" , a ) ..... and likewise the decls open class B( val c1: String , a : A ) : A(a) ... class Z( val z : Int , y:Y ) : Y(y) And yes this could be done with class delgation BUT ONLY if you define the whole set as both interfaces and classes -- inteface AI( ... ) classs A : AI( duplicate all of A's properties one by one) .... interface ZI( ... ) here we go aaagin classs Z( .. ) Then yes finally you could do val y = Y( ... ) val z = Z( "z" , y: Y ) : Y by y // Finally the paydirt
h
@DALDEI I've taken to commonly using the following structure instead of `class`:
Copy code
interface A {
    val a1: String
    val b1: Int

    companion object {
        operator fun invoke(a1: String, b1: Int) = object : A {
            override val a1 = a1
            override val b1 = b1
        }
    }
}

class B(a1: String, b1: Int, val c1: Int): A by A(a1, b1)

val a = A("foo", 42)
you can of course implement B in the same fashion this has a lot of advantages in class delegation, like basically having "multiple inheritance", so I don't ever run into your concerns in my code.