Posted 19 October 2014 - 12:06 PM
Introduction
This is an utility which synces/links primitive types like numbers or strings.
These are normally passed by value:
in some cases, it's useful if numbers are passed by reference, like in this example:
I made a little library with 2 functions, that uses the tables property to be passed byref (=by reference :P/>) and some metamethods.
pastebin get Qp3SYHr4 linkapi
How to use it?
It has 2 functions:
The normal link
Creating one is easy:
getting the absolute value of a link:
creating copies of that link:
doing arithmetic:
only comparing doesn't work that well, lua's __eq metamethod is not thaaat optimal:
You can't compare a link with a number, but you can compare it with a link and you can compare the absolute value (a.g) with the number.
The stronglink
You can use stronglinks exactly like normal links, the only difference is the behavior when doing arithmetic with them.
The absolute value of a stronglink is dependent on the stronglinks it's created with.
You can create them like a normal link:
But the behave different in arithmetic:
Thats it.
Links, especially stronglinks are useful for, for example when a screen or a window resizes, and you don't want to compute all your stuff again. Stronglinks will automatically recalculate your measurements.
I know, it's a bit complicated, but once you understood it, it's easy and pretty useful.
Originally, I wanted #a to return the absolute value, but the usage of the __len metamethod for tables is new in lua 5.2, but computercraft uses lua 5.1.
I hope you understand how this can be used, and I hope you understood my bad english :)/>
This is an utility which synces/links primitive types like numbers or strings.
These are normally passed by value:
local a = 100
local b = a
a = 50
print(b ) --still 100
in some cases, it's useful if numbers are passed by reference, like in this example:
local a = 100
local b = a
a = 50
print(b ) --its... 50! :0
This is not how this API works, it's just an example.I made a little library with 2 functions, that uses the tables property to be passed byref (=by reference :P/>) and some metamethods.
pastebin get Qp3SYHr4 linkapi
How to use it?
It has 2 functions:
link(value, id) -- create a normal link
slink(value, id) -- create a stronglink
The normal link
Creating one is easy:
local a = link(value, id) -- value and id are optional
a.s = 125 -- set the value of the link
print(a) -- will print 125
getting the absolute value of a link:
local absolutevalue = a.g -- absolutevalue is a number, a is a table
print(absolutevalue) -- will print 125
creating copies of that link:
b = a() -- creates a new copy of that link
print(b ) -- will print 125
doing arithmetic:
local numb = 25 + a -- numb is a number (notice that, its important for the behavior of stronglinks!)
print(numb) -- will print 150
-- a and b are both tables, but they can fully be handled as numbers
only comparing doesn't work that well, lua's __eq metamethod is not thaaat optimal:
print(a == 125) -- false
print(a == b ) -- true
print(a.g == 125) -- true
You can't compare a link with a number, but you can compare it with a link and you can compare the absolute value (a.g) with the number.
The stronglink
You can use stronglinks exactly like normal links, the only difference is the behavior when doing arithmetic with them.
The absolute value of a stronglink is dependent on the stronglinks it's created with.
You can create them like a normal link:
local a = slink(150, id) -- value and id are optional again
But the behave different in arithmetic:
local b = a + 50 -- b is a stronglink (remember? the type of numb was a number!)
print(b ) -- will print 200
a.s = 200 -- lets change a to 200
print(b ) -- will print 250! (which is a + 50)
Thats it.
Links, especially stronglinks are useful for, for example when a screen or a window resizes, and you don't want to compute all your stuff again. Stronglinks will automatically recalculate your measurements.
I know, it's a bit complicated, but once you understood it, it's easy and pretty useful.
Originally, I wanted #a to return the absolute value, but the usage of the __len metamethod for tables is new in lua 5.2, but computercraft uses lua 5.1.
I hope you understand how this can be used, and I hope you understood my bad english :)/>
Edited on 19 October 2014 - 10:09 AM