https://kotlinlang.org logo
#kapt
Title
# kapt
k

karelpeeters

09/26/2018, 10:39 AM
I need state for both parent classes.
e

Eugenio

09/26/2018, 1:06 PM
like this?
Copy code
interface Parent1 {
    var state1: String

    fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2 {
    var state2: Int

    fun manipulateState2() {
        state2 += 1
    }
}

class Child : Parent1, Parent2 {
    override var state1 = "hello"
    override var state2 = 0

    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:09 PM
Problem is that that allows
Child
to access both states, there's no encapsulation.
Good idea though
e

Eugenio

09/26/2018, 1:19 PM
oh in that case:
Copy code
interface Parent1_ {
    var state1: String

    fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2_ {
    var state2: Int

    fun manipulateState2() {
        state2 += 1
    }
}

interface Parent1 : Parent1_ {
    @Deprecated("", level = HIDDEN)
    override var state1: String
}

interface Parent2 : Parent2_ {
    @Deprecated("", level = HIDDEN)
    override var state2: Int
}

class Child : Parent1, Parent2 {
    override var state1 = "hello"
    override var state2 = 0

    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:20 PM
Can
Child
not interact with
state1
and
state2
then? That's evil simple smile
e

Eugenio

09/26/2018, 1:28 PM
you'd have to do:
Copy code
(this as Parent1_).state1
otherwise it's as if it doesn't exist, not even in the autocomplete
or, even better:
Copy code
interface Parent1 {
    fun manipulateState1()
}

interface Parent2 {
    fun manipulateState2()
}

interface Parent1State : Parent1 {
    var state1: String

    override fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2State : Parent2 {
    var state2: Int

    override fun manipulateState2() {
        state2 += 1
    }
}

class Child : 
    Parent1 by object : Parent1State { override var state1 = "" },
    Parent2 by object : Parent2State { override var state2 = 0 } 
{
    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:29 PM
Might even be able to create classes for those delegate objects then!
e

Eugenio

09/26/2018, 1:29 PM
sure, depends on your case
k

karelpeeters

09/26/2018, 1:29 PM
I'll play around a bit later today, thanks for the ideas!
e

Eugenio

09/26/2018, 1:31 PM
or just this:
Copy code
fun Parent1(state1: String): Parent1 = object : Parent1State { override var state1 = state1 }
fun Parent2(state2: Int): Parent2 = object : Parent2State { override var state2 = state2 }

class Child :
    Parent1 by Parent1(""),
    Parent2 by Parent2(0)
{
    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
you're welcome!
k

karelpeeters

09/26/2018, 1:31 PM
Yeah that's what I mean with my last comment.
Don't even need the deprecation then I guess.
e

Eugenio

09/26/2018, 2:54 PM
exactly
k

karelpeeters

09/29/2018, 1:56 PM
I'm kind of stuck again, do you happen to have another trick for
protected
visibility?
Copy code
interface User {
    fun foo()
}

class ConstUser(size: Int) : User {
    private val values = Array(size) { "$it" }

    override fun foo() = print(values)

    fun get(index: Int) = values[index]
}

class MyUser: User by ConstUser(2) {
    //I want ConstUser.get(index) to be protected-level visible here
}
e

Eugenio

09/29/2018, 2:25 PM
@karelpeeters in this case you might do:
Copy code
interface User {
    fun foo()
}

class ConstUser(size: Int) : User {
    private val values = Array(size) { "$it" }

    override fun foo() = print(values)

    operator fun get(index: Int) = values[index]
}

open class MyUser 
private constructor (
    private val user: ConstUser
): User by user {
    
    constructor() : this(ConstUser(2))
    
    protected operator fun get(index: Int) = user[index]
}
k

karelpeeters

09/29/2018, 2:30 PM
The annoying thing is that that requires a bunch of code duplication (there are lots of
MyUser
-type classes). I wonder if there's a way to get access to the delegate object instance, that might simplify things a bit.
e

Eugenio

09/29/2018, 2:39 PM
There isn't
You could have an internal method on the interface in another module
k

karelpeeters

09/29/2018, 2:49 PM
Hmm. Now I'm thinking generating code might be a better solution, but that then requires multiple modules and gradle fuckery. Dammit!
e

Eugenio

09/29/2018, 2:55 PM
It won't get easier than this I'm afraid... What's the use case?
k

karelpeeters

09/29/2018, 3:35 PM
Faking multiple inheritance still. I spend some time cleaning up, if you want a full explanation: I'm writing an optimizing compiler inspired by LLVM. It works on an SSA form IR made of `Instruction`s, and those instructions have operands of type
Value
. Instructions themselves are also values, representing the result of their computation (this works and is elegant because of SSA, don't worry about it). Current code, commented with things I'm not happy about: https://gist.github.com/flaghacker/bfc6c09e50bacdbb236e79b8729ffe24
e

Eugenio

09/29/2018, 3:52 PM
A few questions: * What exactly is User? * Why do you need Value to be User? * Why not make things immutable?
k

karelpeeters

09/29/2018, 3:58 PM
• User is "something that uses values", it only has that single function defined on it. This comes up in optimization: let's say someone uses
Add(Constant(1), Constant(2))
as an operand, that operand can be replaced with
Constant(3)
everywhere in the IR. In practice this would be a
replaceWith(Constant(3))
call on the add instruction I mentioned above, that then loops over the users and replaces itself with the new value. • I specifically don't want Value to be a User, see the comment above Value. That is just my current crappy solution to the fundamental multiple inheritance problem: things can be Values and Users at the same time. • Because the optimizations mutate stuff all over the place, eg. in the above example replacing
1 + 2
with
3
requires mutating of everything that uses
1 + 2
. Creating so many new objects is both bad for performance and difficult to do polymorphically (see Java's
Clonable
disaster).
e

Eugenio

09/29/2018, 4:00 PM
what I mean is: why are you currently having Value be User?
k

karelpeeters

09/29/2018, 4:01 PM
Because eg. Instruction is both a Value and a User, and multiple direct parent classes aren't allowed. Having User be a Value would be just as good/bad of a solution here.
e

Eugenio

09/29/2018, 4:02 PM
so you want Instruction to be a User
could you provide a snippet with the ideal implementation you'd want (pseudo code) and usages of things that are one and the other?
k

karelpeeters

09/29/2018, 4:14 PM
Yes, instruction is a User. Ideal implementation (User and ConstUser stayed the same): https://gist.github.com/flaghacker/79db37998107d2261fdb5a993da5c26c. I'll write up some usage examples, give me a minute.
And a basic example of how all of it should be used: https://gist.github.com/flaghacker/5c48a6005bf166376fff4ebf162599a5
(Messed up the example, should be fixed now)
e

Eugenio

09/29/2018, 5:22 PM
ok, here's a way to do it
ping @karelpeeters
k

karelpeeters

09/29/2018, 5:41 PM
Hmm, the problem is that that leaks
operator
outside of
Add
, right? I guess it's not that bad.
e

Eugenio

09/29/2018, 5:43 PM
it doesn't really
in the sense: if you put yourself in the right scope you might be able to create an instance of
OperandDelegate
, BUT that by itself is stateless!
it only does anything when
provideDelegate
is invoked, which can only be invoked on the right kind of object, and can access internals of
ConstUser
because it's enclosed in it (but it's not an inner class!)
so you could even re-use the instance of
OperandDelegate
(for a certain
Value
) across different properties and even different instructions
k

karelpeeters

09/29/2018, 5:59 PM
Oh I see I missed a couple of steps of indirection there simple smile . I forgot about the
provideDelegate
thing, thanks! I used to cache the delegate instances but I cut out that part to have simpler code. I'll toy around a bit with this.
Copy code
val zero = Constant(0)
val x = Add(zero, zero)
with(x) { x.operand(zero) }
but that's pretty unlikely.
e

Eugenio

09/30/2018, 6:04 PM
@karelpeeters yes but that's what I mean: you can do that, but creating an instance won't do anything, you only do "something" when you use it as a property delegate with
by
and you can only do that in the correct type, plus it won't affect the instance that created the delegate, but the instance on which the delegate is applied
k

karelpeeters

09/30/2018, 6:45 PM
Right it won't do anything bad, that's true.
2 Views