espertus / blockly-lua

Control ComputerCraft turtles in Minecraft with Blockly
https://github.com/espertus/blockly-lua/wiki
Apache License 2.0
50 stars 19 forks source link

[Suggestion] Multiple returns #2

Open theoriginalbit opened 10 years ago

theoriginalbit commented 10 years ago

Lua is a weird language, it can actually return multiple values from functions as individual return values like so

local function foo()
  local bar = 5
  return bar, "hello", "world"
end

Which when called like so means that you only gain one of the values

local v1 = foo()

As such to pickup all values you must either do

local v = { foo() }

or

local v1, v2, v3 = foo()

I think adding the ability to do this in blockly-lua could be handy, especially when starting to build event sensitive programs.

espertus commented 10 years ago

I thought a little about this in the context of error messages.

With App Inventor and Blockly, we had/have an explicit philosophy of prioritizing ease for the beginner over the desires of experienced programmers, since we expect the latter to use textual programming languages. I was planning to stick to that philosophy unless you can convince me otherwise. My thinking is that experienced programmers could use Lua and would prefer to do so. Debugging requires messing with the Lua code anyway.

That said, I was thinking of providing support for error messages. For example, the code generated for:

if turnRight :

would be:

result, error = turnRight()
if result
   : 

The special variable "error" would contain the most recent error message.

Actually, the code would really be:

function turnRight_()
  result, error = turnRight()
  return result
end

What do you think?

theoriginalbit commented 10 years ago

I do understand, and agree with that philosophy, unfortunately however with ComputerCraft there is nothing that an unexperienced programmer doesn't want to do, for example rednet messages, they're always finding an excuse to use them, and their format is the following

local senderId, message, distanceSent = rednet.receive()

as you can see, requires multiple variable declarations. Obviously helper functions could be made for them in order for it to work, but I've noticed that a large amount of the community tend to simply copy/paste without actually learning anything from it, so my opinions on these matters are slightly conflicting to what I would normally say compared to what I think with ComputerCraft.

Side-note: you've overrode the global error function there in your turnRight_

espertus commented 10 years ago

On Mon, Nov 4, 2013 at 8:19 PM, Josh Asbury notifications@github.comwrote:

I do understand, and agree with that philosophy, unfortunately however with ComputerCraft there is nothing that an unexperienced programmer doesn't want to do, for example rednet messages, they're always finding an excuse to use them, and their format is the following

local senderId, message, distanceSent = rednet.receive()

That's good to know.

Side-note: you've overrode the global error function there in your turnRight_

Thanks for pointing that out. Your domain expertise is priceless.

espertus commented 10 years ago

On Mon, Nov 4, 2013 at 8:19 PM, Josh Asbury notifications@github.comwrote:

I do understand, and agree with that philosophy, unfortunately however with ComputerCraft there is nothing that an unexperienced programmer doesn't want to do, for example rednet messages, they're always finding an excuse to use them, and their format is the following

local senderId, message, distanceSent = rednet.receive()

as you can see, requires multiple variable declarations.

The Rednet API page http://computercraft.info/wiki/Rednet_(API) says: Rednet no longer supports transmitting data through Bundled Cables. However, interacting with Bundled Cables is still available via the Redstone APIhttp://computercraft.info/wiki/Redstone_(API)

Does that mean that the API is obsolete? I've never used the API (or even Redstone) so don't understand.

Thanks.

Ellen

theoriginalbit commented 10 years ago

Not at all!

The rednet API is designed for communication of data between computers. Methods of communication we have is Wireless Modems and Wired Modems via Network/Peripheral cables.

In previous versions of ComputerCraft when we did not have network cables, the rednet API was able to function over RedPower2 bundled cables as a wired (and thus more distance) connection.

All that note is stating is that we can no longer use Bundled cables from RP2, because we have our own cable and RP2 isn't updated for the last few versions of Minecraft. We can however still interact with RedPower2 cables (if it were updated) through the redstone API, via rs.setBundledOutput, rs.getBundledOutput, rs.getBundledInput, etc... These functions also work with the MineFactory Reloaded RedNet cables (not to be confused with the rednet API) which are a semi-updated version of the RP2 bundled cables. Of course the redstone API allows for analog input/output of vanilla redstone signals too.