getter and setter syntax

58 de afișări
Accesați primul mesaj necitit

Sha

necitită,
11 nov. 2017, 14:31:2611.11.2017
– lemon-lang
Hi,


I am looking at these examples of getter and setter syntax in Lemon, but I do not understand it well nor can I figure out its purpose as to when it should be used.

Can someone please explain it to me?

Thanks
~ sha


// --- Getter

def f(var x) {  // The value of x is the original value of a, in this case nil

  return 100;   // The return value is the new value of a


@getter(f) 

var a;    // same as: @f var a


a = 1;

print(a); // No matter what the value of a is, it will always output 100


// --- Setter

def g(var y){    // The value of y is assigned to the value of b, this time is 1

  return 200;    // The return value is the new value of b

}


@setter(g) 

var b;


b = 1;

print(b);  // output 100



admin

necitită,
11 nov. 2017, 15:02:5511.11.2017
– lemon-lang
It's syntax sugar for getter and setter, if you don't know what getter and setter is, you should google it.

Sha

necitită,
12 nov. 2017, 21:16:4412.11.2017
– lemon-lang


I have only used getters and setters in the context of a class, never in the context of a free standing function as the example suggests.

My (limited) understanding of Python syntax of @property comes close to what Lemon has to offer. I will keep on Googling though.

I usually do not ask a question on this forum without first doing some basic research ... my apologies if they sound very basic for you.

~ sha

admin

necitită,
12 nov. 2017, 21:56:1412.11.2017
– lemon-lang
the module is also a scope, every lemon file is a module, so the bare variable is also a property of module. the getter and setter also working with class. example

def g(var x) {
       
return self.y; // self is Foo instance auto binding by accessor
}

class Foo() {
       
def __init__() {
               
self.y = 200;
       
}

       
@g
       
var x;
}

var foo = Foo();
print(foo.x);

Sha

necitită,
12 nov. 2017, 23:44:5412.11.2017
– lemon-lang


Perfect!

Thanks for taking time to clarify.
Răspundeți tuturor
Răspundeți autorului
Redirecționați
0 mesaje noi