## get set vs properties, whether they are a good idea

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
DarkShroom
Prole
Posts: 47
Joined: Mon Jul 17, 2017 2:07 pm

### get set vs properties, whether they are a good idea

is there any explicit reason why love 2d itself does not use properties? is it perhaps because there is no way to reference 'self' without essentially copying curried functions into everything (functions that are embedded with the self reference

ie

Code: Select all

function () function2(reference_to_unique_object) end

for extra info about what i'm doing, i have created a object system where i have a reflected version of all the physics functions, it allows me to set the physics settings of an object before "activating" the physics (which creates the body, triangulates and adds the fixtures etc)... the reflected functions in the case of "fixture" for example, they run the same function on all the fixtures.... i wanted to abstract the physics system to i can turn it off and on and use the same variables to set things

zorg
Party member
Posts: 2247
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: get set vs properties, whether they are a good idea

What do you mean by löve not using properities? I'm pretty sure both the Data and Object supertypes, and their subtypes both have tons of members, and getter and setter methods as well; case in point, SoundData has :getSample and :setSample, to name two.

As for whether it makes sense to use getters and setters, that depends on the coder, since by default, lua table access isn't really protected, so code could simply reference a member and either get or set the value that way, which is a more direct kind of thing than using accessor functions. (and as far as micro-optimizations go, probably just a tiny bit faster)
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

ivan
Party member
Posts: 1265
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: get set vs properties, whether they are a good idea

zorg wrote:
Fri May 18, 2018 11:37 am
What do you mean by löve not using properities?
I think he means things like:

Code: Select all

src = love.audio.newSource("bang.wav", "static")
src.volume = 0.5

It's just syntactic sugar and would probably be slightly slower than using functions.
Note that the default Lua modules (string, math, etc) do not use "properties" either, except for "constants" like math.huge or math.pi.
is it perhaps because there is no way to reference 'self' without essentially copying curried functions into everything (functions that are embedded with the self reference
That's not necessarily true - if you are using metatables.

raidho36
Party member
Posts: 1663
Joined: Mon Jun 17, 2013 12:00 pm

### Re: get set vs properties, whether they are a good idea

Why would that be slower? If anything, it should be faster, however trivial getters/setters are inlined and then there is no difference whatsoever. The practice of using getters and setters is useful when getting and setting a value does more than just changes its private variable, when it has some side-effects. If not, you might as well make those public and access them directly, there will be no difference, neither effective nor performance-wise - as previously mentioned... well, unless the runtime has to use vtables to figure out which function to call, and then it's a performance hit for no reason.

Love objects use getters and setters because they are light userdata, not actual Lua objects, so they don't have their own properties, only a metatable. If you implement a class, there's a good chance that using trivial getters and setters will be detrimental for performance, on account of having to do one extra table lookup and a function call. So if this sort of code goes anywhere near performance-critical section, testing will be very important to establish how it affects things. In general, I suggest treating Lua objects as structs with methods, and you don't need any methods to simply access a struct member.

DarkShroom
Prole
Posts: 47
Joined: Mon Jul 17, 2017 2:07 pm

### Re: get set vs properties, whether they are a good idea

okay thanks for the information there, i read all the replies

so yes the syntactic sugar type properties, not the actual get/set functions... cool well i think i will go ahead then as it's something i like doing for the clarity of things

### Who is online

Users browsing this forum: DarkShroom and 10 guests