445 posts
Posted 01 December 2012 - 01:29 PM
http://pastebin.com/WJ2HmKp0Short explanation (thanks to kazagistar for writing this explanation):
with(table) temporarily merges the table into the global table, so you can call functions in the table as if they were global. You can still access functions that were overridden by using the out table, or call out as a function to restore back to how it was before with was called. With can be called multiple times using different tables
so I tested it with basic usage, not sure if it has any bugs, yet :3
basically it is for things like:
turtle.forward()
turtle.turnLeft()
turtle.digUp()
turtle.up()
turtle.digUp()
turtle.down()
turle.digDown()
turtle.digDown()
So with this you do:
with(turtle)
turnLeft()
digUp()
up()
digUp()
down()
digDown()
digDown()
out()
Another example:
function forward()
print("going forward")
end
with(turtle)
up()
print("meow") -- calls the normal-print function as turtle has no print function
forward() -- calls the turtle.forward function
out.forward() -- calls the forward function defined above
out()
forward() -- calls the forward function defined above
Multiple levels should work too:
with(turtle)
forward()
with(term)
setCursorPos(1,2)
write("meow")
out()
back()
out()
3790 posts
Location
Lincoln, Nebraska
Posted 01 December 2012 - 01:34 PM
From the title, I thought you were spamming. But this looks really neat!
278 posts
Posted 01 December 2012 - 02:14 PM
So, if I get this correctly, it essentially replaces _G, allowing you to call API functions without prefixing the API name and a period?
Brilliant, but I have no use for it. My period key currently likes his job.
Also, does out() "reset" the changes you made with "with()"?
496 posts
Location
New Zealand
Posted 01 December 2012 - 02:14 PM
On line 12 of the paste you have:
outMT.__index = function(t,k,v)
Which I believe should be:
outMT.__newindex = function(t,k,v)
445 posts
Posted 01 December 2012 - 02:29 PM
Yeah just fixed that :)/>
So, if I get this correctly, it essentially replaces _G, allowing you to call API functions without prefixing the API name and a period?
Brilliant, but I have no use for it. My period key currently likes his job.
Also, does out() "reset" the changes you made with "with()"?
Hmm you can put it like that I think, it basically allows you to assume you act "inside" a table and function calls/variables default to stuff within it..
and yeah out() ends the with(table), putting you back to "default"
180 posts
Posted 01 December 2012 - 10:53 PM
Nice, basically a "change directory" command for name space scope!
This sounds really helpful for interactive usage, I'm going to have to give it a try.
Thanks Pinkishu!
1243 posts
Location
Indiana, United States
Posted 01 December 2012 - 11:00 PM
Welcome back, Pinkishu. :P/>
Also, good work on this.
1688 posts
Location
'MURICA
Posted 02 December 2012 - 01:54 PM
There's actually something like this in moonscript, one of my favorite features. Very nice implementation. :D/>
351 posts
Posted 08 December 2012 - 03:24 AM
There's actually something like this in moonscript, one of my favorite features. Very nice implementation. :D/>
/me looks up moonscript
…this…looks… AMAZING