https://kotlinlang.org logo
Title
h

Hullaballoonatic

06/10/2019, 6:39 PM
I'm guessing the following is impossible, but I figured it can't hurt to ask:
class Foo(val title: String = *the name of the field storing the instance of this class*)

val foo = Foo()
So, in this case "foo"
s

Shawn

06/10/2019, 6:40 PM
you could probably do something incredibly cursed with reflection and traversing the call stack, but I definitely wouldn’t recommend it
☝️ 1
h

Hullaballoonatic

06/10/2019, 6:40 PM
yep. i figured that much
would be nice for writing generic errors whose messages include the name of the function that threw it.
but, i mean, logs show that already so nbd
k

karelpeeters

06/10/2019, 7:25 PM
If you do it with delegates it's only semi-cursed:
data class Foo(val title: String) {
    companion object {
        operator fun provideDelegate(thisRef: Any, prop: KProperty<*>): ReadOnlyProperty<Any, Foo> {
            val foo = Foo(prop.name)
            return object: ReadOnlyProperty<Any, Foo> {
                override fun getValue(thisRef: Any, property: KProperty<*>) = foo
            }
        }
    }
}

class Test {
    val test by Foo
}

fun main() {
    println(Test().test) //"Foo(title=test)"
}
😂 3
👏 2
h

Hullaballoonatic

06/10/2019, 7:28 PM
That's really cool.
k

karelpeeters

06/10/2019, 7:28 PM
Or if you want it to work outside of classes too:
data class Foo(val title: String) {
    companion object {
        operator fun invoke(): ReadOnlyProperty<Nothing?, Foo> {
            return object: ReadOnlyProperty<Nothing?, Foo> {
                var foo: Foo? = null
                override fun getValue(thisRef: Nothing?, property: KProperty<*>) =
                        foo ?: Foo(property.name).also { foo = it }
            }
        }
    }
}

fun main() {
    val test by Foo()
    println(test)   //"Foo(title=test)"
}
h

Hullaballoonatic

06/10/2019, 7:28 PM
i actually didn't know that
by
was an overridable operator
k

karelpeeters

06/10/2019, 7:28 PM
More cursed because of nullability and
Nothing?
As you can tell I've been having some fun with delegates lately 😒imple_smile:
💯 1
I'm using them to fake multiple inheritance, the possibilities are endless.
h

Hullaballoonatic

06/10/2019, 7:31 PM
haha, yay! most of my experience with delegates is stuff like
class MySpecialList<T>(val data: MutableList<T>) : MutableList<T> by data {
     override fun add(element: T): Boolean {
          foo(element)
          return (this as MutableList<T>).add(element)
     } 
}
mind sharing some of the cool things you've been doing with it?
k

karelpeeters

06/10/2019, 7:33 PM
So I'm writing a compiler based on SSA form, think low level, something like
x = add(2, 3)
y = add(x, 2)
Every operation needs to keep track of the values it uses and every value needs to keep track of the operations that use it.
h

Hullaballoonatic

06/10/2019, 7:34 PM
ah
k

karelpeeters

06/10/2019, 7:34 PM
So
y.operands == setOf(x, 2)
and
x.users = setOf(y)
And to keep track of that automatically I have something like
class Add: User by User() {
    var left by operand<Value>()
    var right by operand<Value>()
}
And then mutating the code is as simple as
add2.left = add1
and everything is updated automatically.
h

Hullaballoonatic

06/10/2019, 7:36 PM
that seems like something i'm really interested in, since i wanted to expand the Units of Measurement library port to Kotlin called Physikal so that it could handle conversion of types involving math operators, e.g.
val mass = <http://4.kg|4.kg>
val volume = 1.m * <http://3.cm|3.cm> * 5.dm
val density = mass / volume
but iirc it would require some compiler stuff
k

karelpeeters

06/10/2019, 7:36 PM
That looks like valid Kotlin to me 😒imple_smile:
h

Hullaballoonatic

06/10/2019, 7:37 PM
it is. i'd have to think for a bit why you'd want some compiler trickery
oh
for stuff like
val density = 4.lbs/sqInch
there was more, since i think you can overload
div
here and make a
sqInch
object and do some funny shit to get that to work
k

karelpeeters

06/10/2019, 7:39 PM
Still looks okay to me, define
val inch = 1.inch
and
val sqInch = inch * inch
h

Hullaballoonatic

06/10/2019, 7:42 PM
oh, i think when i talked to the folks that made the port, they wanted you to be able to forgo the
.
val acceleration = 52m/s/s
which i don't think is too big a deal. it all seems possible, so i might go and do it sooner than later
k

karelpeeters

06/10/2019, 7:44 PM
Yeah that's great until you want the atomic mass unit
u
.
h

Hullaballoonatic

06/10/2019, 7:45 PM
i'd also be interested in learning how do mess with the compiler so i could add stuff like swift's
until
operator:
println(5..<8)
// 5, 6, 7
and unbounded ranges:
println(6..) // 6, 7, 8, 9, 10, 11, 12, ...
that i could use in indexing:
println(foo[..4])
k

karelpeeters

06/10/2019, 7:46 PM
That's not possible right now, wasn't there a question about this in #language-proposals a while ago?
h

Hullaballoonatic

06/10/2019, 7:46 PM
i don't know. i've never been to that channel. didn't know it existed. now i know i've been posting some of my ideas for the language in the wrong channel...
h

Hullaballoonatic

06/10/2019, 7:47 PM
found it
ghedeon posted it