Accessing field of a different instance of the same class in Kotlin

Vojtěch

Consider this Kotlin code:

var parent: T? = null
    get() = if (isParent) this as T else field
    set(value) { field = if (value == null) null else value.parent }

val isParent: Boolean
    get() = parent == null

var description = ""
    get() = if (isParent) field else parent!!.description
    set(value) { if (isParent) field = value else parent!!.description = value }

Assume that isParent returns true if this instance is a parent instance. If not getParent() will return the parent instance. In Java you are allowed to access directly field of a different instance of same class like this:

String getDescription() { return getParent().description; }
void setDescription(String value) { getParent().description = value; }

(I am not saying that is a best thing to do, I simplified it for demostration). Comparing to Java, it would be nice to be able to do following:

var description = ""
    get() = parent.field
    set(value) { parent.field = value }

However this does not work and unfortunately it makes the code less readable. Especially if you have a lot of such variables, which are bound to this parent.

yole

A backing field of a property can only be accessed from a getter or setter of that property, and only for the instance on which the getter or setter has been invoked. If you need to provide multiple ways to access an attribute of a class, you need to define two distinct properties, one of which has a backing field to store the data and another has a getter and setter referring to the first property.

class Foo {
    var parent: Foo? = null

    val parentOrSelf: Foo get() = parent ?: this

    private var _description: String? = null

    var description = ""
         get() = parentOrSelf._description
         set(value) { parentOrSelf._description = value }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python:different files accessing same single instance of a class instead of creating separate single instances in each of them

From Dev

Accessing Internal field of nested class from different AppDomain

From Dev

CoreData Or Segue For Accessing Class Instance

From Dev

Represent instance of different classes with the same base class in pyyaml

From Dev

How to pass same browser instance across different class in selenium framework

From Dev

Accessing values from different levels of a nested object within the same append instance using D3.js

From Dev

Kotlin class instance assertEqual

From Dev

Kotlin class instance assertEqual

From Dev

Different Property setter for three same class objects in Kotlin

From Dev

Grabbing unique field from different checkboxes with same class on jQuery .change

From Dev

Accessing a class field using a string

From Dev

Accessing a private field in class module

From Dev

accessing the same member of different structs

From Dev

Accessing class instance from a previous class

From Dev

Accessing a static method when initializing a static field of same class from another thread

From Dev

Accessing a file in a different Java class?

From Dev

Accessing a HashMap from a different class

From Dev

different signatures for accessing class methods

From Dev

Accessing method of different class in jsp

From Dev

Accessing a file in a different Java class?

From Dev

Accessing method of different class in jsp

From Dev

Accessing a field of outer class from inner class

From Dev

Accessing an instance variable from a different view controller

From Dev

Accessing the current view class instance in Django middleware

From Dev

accessing instance methods from class method proc

From Dev

Accessing instance method from class method

From Dev

Accessing class method vairable in instance method

From Dev

Accessing a siblings children, not the first instance of a class

From Dev

Get attribute of class by directly accessing instance

Related Related

  1. 1

    Python:different files accessing same single instance of a class instead of creating separate single instances in each of them

  2. 2

    Accessing Internal field of nested class from different AppDomain

  3. 3

    CoreData Or Segue For Accessing Class Instance

  4. 4

    Represent instance of different classes with the same base class in pyyaml

  5. 5

    How to pass same browser instance across different class in selenium framework

  6. 6

    Accessing values from different levels of a nested object within the same append instance using D3.js

  7. 7

    Kotlin class instance assertEqual

  8. 8

    Kotlin class instance assertEqual

  9. 9

    Different Property setter for three same class objects in Kotlin

  10. 10

    Grabbing unique field from different checkboxes with same class on jQuery .change

  11. 11

    Accessing a class field using a string

  12. 12

    Accessing a private field in class module

  13. 13

    accessing the same member of different structs

  14. 14

    Accessing class instance from a previous class

  15. 15

    Accessing a static method when initializing a static field of same class from another thread

  16. 16

    Accessing a file in a different Java class?

  17. 17

    Accessing a HashMap from a different class

  18. 18

    different signatures for accessing class methods

  19. 19

    Accessing method of different class in jsp

  20. 20

    Accessing a file in a different Java class?

  21. 21

    Accessing method of different class in jsp

  22. 22

    Accessing a field of outer class from inner class

  23. 23

    Accessing an instance variable from a different view controller

  24. 24

    Accessing the current view class instance in Django middleware

  25. 25

    accessing instance methods from class method proc

  26. 26

    Accessing instance method from class method

  27. 27

    Accessing class method vairable in instance method

  28. 28

    Accessing a siblings children, not the first instance of a class

  29. 29

    Get attribute of class by directly accessing instance

HotTag

Archive