Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference of Getter/Setter functions

Tags:

dart

I have the following variable and getter / setter defined in my data model:

class Actor {
    int _x;
    int get x => _x;
    set x(int value) => _x = value;
}

And there is this generic class that requires a getter / setter function pointer

class PropertyItem {
   var getterFunction;
   var setterFunction;
   PropertyItem(this.getterFunction, this.setterFunction);
}

How do i pass a reference of the getter / setter function of X to the PropertyItem class?

// Something like this
var item = new PropertyItem(x.getter, x.setter);

EDIT: Updated with a more clear question

like image 967
Ali Akbar Avatar asked Nov 24 '12 21:11

Ali Akbar


People also ask

What are getter and setter functions?

Getters and Setters play an important role in retrieving and updating the value of a variable outside the encapsulating class. A setter updates the value of a variable, while a getter reads the value of a variable.

What is getter function used for?

The getter function is used to retrieve the variable value and the setter function is used to set the variable value. Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.

What is the other name of getter methods?

And a getter is a method that reads the value of a variable. Getter and setter are also known as accessor and mutator in Java.

Should you use getters and setters in JavaScript?

Conclusion. You don't necessarily have to use getters and setters when creating a JavaScript object, but they can be helpful in many cases. The most common use cases are (1) securing access to data properties and (2) adding extra logic to properties before getting or setting their values.

Why do we use Getter and setter functions?

Getter and Setter functions are collectively known as accessor functions. In my previous two articles, I talked about how I created mix because I wanted to use Getter and Setter functions. But why do we even use Getters and Setters in the first place? I have two reasons. When you try to get the length of an array, you write array.length.

What is the difference between getter and setter in JavaScript?

The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. Once the getter and setter have been defined, we use it in our main: Getters and setters allow control over the values.

What is the difference between getter and setter in Kotlin?

In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and setters are auto-generated in the code.

How do you write getters and setters in C++?

By convention, getters start with the word "get" and setters with the word "set", followed by a variable name. In both cases the first letter of the variable's name is capitalized:


2 Answers

In short, you don't. Getters and setters are not extractable - they are indistinguishable from just having a field (if you don't do side-effects, of course).

In your example, you could just do:

class Actor {
  int x;
} 

and get exactly the same effect.

What you want is, for some Actor "actor", to make the functions yourself:

var item = new PropertyItem(() => actor.x, (v) { actor.x = v; });

This proposal about generalized tear offs is approved and will probably implemented soon and allows to closurize getters and setters like:

var item = new PropertyItem(actor#x, actor#x=);
like image 131
Lasse Nielsen Avatar answered Oct 10 '22 04:10

Lasse Nielsen


In Dart, the following:

class Foo {
  int _offsetX;
  int get offsetX => _offsetX;
  set offsetX(int ox) => _offsetX = ox;
}

is equivalent to:

class Foo {
  int offsetX;
}
like image 34
Cutch Avatar answered Oct 10 '22 05:10

Cutch