This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Pinkishu's profile picture

With

Started by Pinkishu, 01 December 2012 - 12:29 PM
Pinkishu #1
Posted 01 December 2012 - 01:29 PM
http://pastebin.com/WJ2HmKp0

Short 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()
Cranium #2
Posted 01 December 2012 - 01:34 PM
From the title, I thought you were spamming. But this looks really neat!
KillaVanilla #3
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()"?
Xtansia #4
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)
Pinkishu #5
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"
dissy #6
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!
Tiin57 #7
Posted 01 December 2012 - 11:00 PM
Welcome back, Pinkishu. :P/>
Also, good work on this.
Kingdaro #8
Posted 02 December 2012 - 01:54 PM
There's actually something like this in moonscript, one of my favorite features. Very nice implementation. :D/>
kazagistar #9
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