I'm guessing the following is impossible, but I fi...
# getting-started
h
I'm guessing the following is impossible, but I figured it can't hurt to ask:
Copy code
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
you could probably do something incredibly cursed with reflection and traversing the call stack, but I definitely wouldn’t recommend it
☝️ 1
h
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
If you do it with delegates it's only semi-cursed:
Copy code
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
That's really cool.
k
Or if you want it to work outside of classes too:
Copy code
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
i actually didn't know that
by
was an overridable operator
k
More cursed because of nullability and
Nothing?
As you can tell I've been having some fun with delegates lately simple smile
💯 1
I'm using them to fake multiple inheritance, the possibilities are endless.
h
haha, yay! most of my experience with delegates is stuff like
Copy code
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
So I'm writing a compiler based on SSA form, think low level, something like
Copy code
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
ah
k
So
y.operands == setOf(x, 2)
and
x.users = setOf(y)
And to keep track of that automatically I have something like
Copy code
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
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.
Copy code
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
That looks like valid Kotlin to me simple smile
h
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
Still looks okay to me, define
val inch = 1.inch
and
val sqInch = inch * inch
h
oh, i think when i talked to the folks that made the port, they wanted you to be able to forgo the
.
Copy code
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
Yeah that's great until you want the atomic mass unit
u
.
h
i'd also be interested in learning how do mess with the compiler so i could add stuff like swift's
until
operator:
Copy code
println(5..<8)
// 5, 6, 7
and unbounded ranges:
Copy code
println(6..) // 6, 7, 8, 9, 10, 11, 12, ...
that i could use in indexing:
Copy code
println(foo[..4])
k
That's not possible right now, wasn't there a question about this in #language-proposals a while ago?
h
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
found it
ghedeon posted it