I really want to be able to do this in kotlin :( `...
# language-proposals
I really want to be able to do this in kotlin :(
Copy code
val Application.cronUsername by this.environment.config
will refer to the receiver ie the
. Is there any limitation I’m missing why its not possible right now?
you cannot delegate directly, what it should do exactly?”
just declare getter
but anyway this syntax is not supported
Why the annotation? I’d say it’s clearer as the following, unless there’s a good reason to have the annotation.
Copy code
val Application.cronUsername get() =
Sorry, the annotation is not included/ necessary to what I’m proposing which is being able to access the receiver when delegating an extension property.
Basically I want to be able to delegate the implementation of an extension property to another (publicly accessible) property/method of the receiver of the extension property. My example is pretty contrived but above is the core of what I wanted
If this was allowed, for instance, the config keys could be “figured out” from the property name and you can write
Copy code
val Application.app_cron_username by environment.config
and the getValue will transform the property.name into key
Or we could abstract out type conversion by making the getValue generic on reified return type and have typesafe extension properties like
Copy code
val Application.port: Int by environment.config
where getValue will have
if (T::class == Int::class) getPropertyString("...").toInt()
Part of both of the above examples are supported right now of course. Whats not is being able to access the receiver of the extension property within the delegate expression. I would like to know if it was something considered before and if so what are the limitations that are not immediately obvious to me right now.
<EDITED OP to remove annotation @natpryce>
Ah I see. The receiver is passed to getValue (but as an untyped Any? value). You could write your own class or object that implements getValue and passes the receiver and property name & type to a function that calculates the property value.