Questions tagged [delegated-properties]
13 questions
21
votes
3 answers
How to get the name of a variable in Kotlin?
I have a Kotlin class in my application with a lot of attributes, what I want to build is a method that stores the variable name in a dictionary. The dictionary looks like this:
HashMap>()
The purpose of this is to store the…

SparklyUnicorn
- 1
- 2
- 5
- 18
6
votes
2 answers
Kotlin: How can I use delegated properties in Java?
I know that you can't use the delegated property syntax in Java, and won't get the convenience of "overriding" the set/get operators as in Kotlin, but I'd still like to use an existing property delegate in Java.
For instance, a simple delegate of an…

Jire
- 9,680
- 14
- 52
- 87
3
votes
1 answer
Delegated properties inside functions
I don't really understand why we can use delegated properties inside functions. We cannot create properties inside functions because inside functions we can only create variables.
How come is possible creating a delegated property inside a function…

Marti Serra Molina
- 423
- 3
- 11
3
votes
3 answers
Can't use custom getter with delegated property
I am new to kotlin and can't figure out this issue.
I have a property that is not nullable and may or may not be used.
Hence I have delegated it in a lazy way so that it is initialized when required.
private val labelAnimator: ObjectAnimator by lazy…

Pramod Garg
- 2,183
- 1
- 24
- 29
1
vote
1 answer
Is there any way to trigger a 'change' event listener if input value change dynamically?
Suppose, I have a delegated input field with the class name 'bulk_number' inside a parent class name 'all_bulk_inputs', and a button that will add a random number to that input field whenever I click it. Now I want to trigger a change event listener…

Ebrahim Khalil Amid
- 398
- 1
- 4
- 12
1
vote
1 answer
Which feature of kotlin can be used as a replacedment of method interception via java proxies
In java if I want to intercept the method calls on a class I can use Proxy.newProxyInstance() and provide an InvocationHandler implementing invoke().
I've seen that in kotlin you can use delegated properties to kind of intercept property access. …

Marinos An
- 9,481
- 6
- 63
- 96
1
vote
1 answer
How to pass Delegated property anonymously in Kotlin?
Let's assume a simple class:
class Foo(val p:String)
I would like to create instance of the class by passing lazy delegate:
val lazyP by lazy {"foo"}
println(Foo(lazyP).p)
but I don't need the boilerplate lazyP variable. How to write a one-liner?…

Maciej Sz
- 11,151
- 7
- 40
- 56
1
vote
1 answer
dynamically created elements by ajax wont work using delegated event
i am a beginner in javascript/jquery. I want to use delegated event because i created a dynamic class and i want to get it's data-value on it.
Now i made like this:
function getProducts(category_id) {
$("#product-list").empty();
$.ajax({
…

Jc John
- 1,799
- 2
- 34
- 69
0
votes
1 answer
Kotlin Jetpack Compose Understanding Type in Delegate property
I'm having difficulty to understand Type of property when using delegated property. For example, from this Jetpack Compose documentation, it says both things are same.
1. val name: String by helloViewModel.name.observeAsState("")
2. val nameState:…

Rahul Chaurasia
- 1,601
- 2
- 18
- 37
0
votes
1 answer
Java Preferences API with Kotlin delegated properties
I want to use the Java Preferences API to store some data for my application. Since I'm writing the application in Kotlin, I would like to take advantage of delegated properties to get and set my preferences.
My current setup is like this
object…

Altline
- 193
- 3
- 10
0
votes
1 answer
DelegatedProperty reflection doesn't work on release build
During runtime I need to access properties in the delegated property's delegate instance.
When I compile the following code in debug, it works just fine:
class Potato {
val somePropGoesHere: Int by PotatoDeletgate("This is the key", 0)
fun…

MichaelThePotato
- 1,523
- 2
- 10
- 19
0
votes
1 answer
How to improve lazy delegate?
How i can improve this declaration:
val __name: String by lazy {
createHTML().small {
+name
}
}
like as my own delegate builder html:
val __name: String by html {
small {
+name
}
}

qwert_ukg
- 490
- 5
- 15
0
votes
1 answer
How do I test generators for delegated properties?
In my current project there is a class that will later on be implemented by many others. This class provides some generators for delegated properties.
abstract class BaseClass {
protected val delegated1 get() = new Delegated1Impl()
protected…

danielspaniol
- 2,228
- 21
- 38