getter and setter syntax

52 views
Skip to first unread message

Sha

unread,
Nov 11, 2017, 2:31:26 PM11/11/17
to 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

unread,
Nov 11, 2017, 3:02:55 PM11/11/17
to 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

unread,
Nov 12, 2017, 9:16:44 PM11/12/17
to 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

unread,
Nov 12, 2017, 9:56:14 PM11/12/17
to 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

unread,
Nov 12, 2017, 11:44:54 PM11/12/17
to lemon-lang


Perfect!

Thanks for taking time to clarify.
Reply all
Reply to author
Forward
0 new messages