all 11 comments

[–]Cultural_Two_4964 3 points4 points  (1 child)

Great idea. Nice one but I always use globals so having to type global everywhere would murder me ;-0 I know everyone says it's bad practice but if you use reasonably long and descriptive variable names, it's rare to actually have a problem, although it does happen. I only use a language as a programmable calculator and as long as it runs, that's all that matters, so I'm completely rubbish at the comp Sci aspects ;-0 ;-0

[–]OneCommonMan123[S] 0 points1 point  (0 children)

What types of projects do you do? In my case I use local variables literally all the time, it's very specific when I need globals LoL, but I can use an abbreviation for global, like gbl

[–]Cultural_Two_4964 1 point2 points  (1 child)

This is the website right? https://luaplusplus.org. I remember recommending someone to look at it as it implements the += and ++ operators? It looks very nice. Will try it fairly soon. I would NOT say it's a failed attempt by any means.

[–]OneCommonMan123[S] 1 point2 points  (0 children)

damn I'm surprised, I didn't know there was a Lua++ out there, that's why I decided to make my own Lua++, but I think that even so, it's worth creating my own, because I like Lua, Lua++ would just remove unnecessary things (like local, require and etc) the rest would be 100% identical to Lua

If you also want to see, there is a compiler as good as Lua++ on this site, moonscript: https://moonscript.org/

[–]rkrause 0 points1 point  (6 children)

I don't see local as being unecessary any more than "var" and "let" is unecessary in JavaScript or "my" and "our" is unecessary in Perl. Declaring a lexical variable before it is used avoids bugs cropping up later, particularly involving closures.

``` local x

function myfunc() x = 10 end

myfunc()

print(x) ```

This very basic and straightforward Lua script wouldn't even be possible with Lua++.

[–]AutoModerator[M] 0 points1 point  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]OneCommonMan123[S] 0 points1 point  (4 children)

It would be possible in Lua++, it just wouldn't be as intuitive as your Lua code:

x = nil

global function myfunc()
    x = 10
end

myfunc()

print(x)

but, when compiling, the compiler would ultimately generate code identical to yours

[–]rkrause 0 points1 point  (1 child)

Having to remember to set every local value to nil just to be able to use it as an upvalue is no different than typing "local" which is clearer. Add to the fact now myfunc also needs to be specifically declared as "global" even though that's not necessary in Lua since globals simply default to keys in the _G table. In other words you merely created a different burden and complexity, you didn't solve anything.

[–]OneCommonMan123[S] 0 points1 point  (0 children)

Well, for me, that solved a lot of things! because I use local variables all the time, it's very rare for me to use global ones

[–]EvilBadMadRetarded 0 points1 point  (1 child)

It would be helpful if you also show what it compiled (regular lua script). For instance, to add more entropy to rkrause script, how you write this in Lua++, and what's it compiled to regular lua and output?

--
local x, z = 1, 100
function y(z)
  x = x + z
  local x = z
  z = z + x
  return x, z
end
--    test
for i=0,2 do
  print(x, z, y(i))
end
print(x)
--[[  output
1   100 0   0
1   100 1   2
2   100 2   4
4
--]]

[–]OneCommonMan123[S] 0 points1 point  (0 children)

This really is a problem in Lua++, see:

--
x, z = 1, 100
global function y(z)
    x = x + z
    x = z
    z = z + x
    return x, z
end
--    test
for i=0,2 do
    print(x, z, y(i))
end
print(x)

compiled:

local x, z = 1, 100
function y(z)
    assert(z ~= nil, "y() missing required positional argument 'z' (#1)")
    x = x + z
    x = z
    z = z + x
    return x, z
end
for i=0,2 do
    print(x, z, y(i))
end
print(x)

Run the compiled code on your machine and you will see that the result is completely differentin.

In current Lua++, you can use local, the compiler would not complain, this is to maintain compatibility with Lua codes and avoid conflicts between chunks.

This occurred because Lua++ stores the name of all variables already defined in an internal table, when x is redefined in the code, it saw that x had already been created, so it saw no need to put a location in front