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

Lua: What would you change?

Started by theoriginalbit, 02 December 2013 - 08:30 PM
theoriginalbit #1
Posted 02 December 2013 - 09:30 PM
A discussion just came up with Symmetryc and I thought it'd be interesting to hear other peoples opinions as well.

Topic for discussion:
If you could change anything about Lua, what would you change? and if you wish to elaborate more, why?

I have but one rule.
If you're going to reply to this thread, be respectful, this is me asking peoples opinions, there is no such thing as a stupid or wrong opinion (I hope someone doesn't prove me wrong now :P/>). Feel free to ask them to elaborate, just don't be mean to them about their decisions, I'm trying to have this as an open discussion.

I guess I should start.
  1. proper ternary operators/statements someCondition ? if-true : if-false why: the implementation we have available now just doesn't always cut it, it'd be nice to have a real one;
  2. data type checking function foo(string bar) why: mainly 'cause of the next point;
  3. function overloading requires above;
  4. default parameter values function foo(string bar = "none") why: I hope this is fairly obvious too, its one of the only parts of Python that I like;
  5. binary numbers b10011000 why: idk, sometimes I think it could come in handy to help visualise numbers in an environment where you're doing a lot of binary manipulation. Hex is the best we have currently;
  6. a few random metamethods, such as __type why: I make my own implementation of this way too much for OO Lua;
  7. a better assert function, preferably one like I made all that time ago that has support for the throwback levels! why: same reason as above, I make it way too often;
  8. there was another, I've forgotten it, I'll edit this when I remember it;

Finally.
As you can see the things you'd love to be in can be completely bias as to why you want it, for example #5 and #6 for me is just because I always seem to be overriding to be adding functionality

What others have stated that I like the sound of.
  1. Declare things by default with a local scope, and specify global with global keyword or global symbol. — Bubba — Yes one million times yes!
Edited on 03 December 2013 - 06:13 AM
Symmetryc #2
Posted 02 December 2013 - 09:42 PM
(From the discussion we had)
1. __next, __type, __and, __or, __not, __len, __ne (~=)
2. Being able to set metatables of things that are not tables.
3. Better loadstring (that is not looked down upon :P/>/>)
4. table.insert returns the table that is being inserted into
Edited on 02 December 2013 - 09:17 PM
distantcam #3
Posted 02 December 2013 - 09:56 PM
* data type checking function foo(string bar) why: I hope this is fairly obvious

This isn't obvious to me. I can understand the desire for static typing but Lua isn't a static type language. Static typing is only really useful for compiled languages, which Lua isn't.

My main issues with Lua are syntactical. I prefer "if (condition) {…}" over "if condition then … end". I'm always forgetting the 'then'. Same for for..do and while..do.

When it comes to using higher order functions the syntax really gets worse.

For example, if I want to pass in a predicate function (that is, some function that acts as a filter by returning true or false) in Lua the syntax is much worse than say C# which is what I work in daily.

Lua
doFoo(function(x) return x % 2 == 0 end)

C#
doFoo(x => x % 2 == 0)
theoriginalbit #4
Posted 02 December 2013 - 10:05 PM
-snip-
well for one, having static typing does allow for function overloading, which is one of the major things that I do want in Lua, but can only be achieved through type checking.

i know that it is not possible for Lua to have the functionality because it is not a statically typed language, however that being said this topic doesn't need to make sense in the realm of what it possible, it was more just me seeing what other people thought, even if it were not possible without changing everything about Lua. I find it very interesting to find out peoples ideas, perceptions, and opinions of programming, no matter how feasible it is.
distantcam #5
Posted 02 December 2013 - 10:14 PM
well for one, having static typing does allow for function overloading, which is one of the major things that I do want in Lua, but can only be achieved through type checking.

Ahhhh, ok then. I was only thinking in terms of usefulness with compiling, but function resolution would be another use.

I do like these kinds of thought experiments.
jay5476 #6
Posted 03 December 2013 - 12:22 AM
i find that the moon is pretty good atm and i would not change it
theoriginalbit #7
Posted 03 December 2013 - 12:48 AM
Ahhhh, ok then. I was only thinking in terms of usefulness with compiling, but function resolution would be another use.
Yeh there's so many times where I've just wanted functions that are the same name but different args.

i find that the moon is pretty good atm and i would not change it
There's always one :P/>
distantcam #8
Posted 03 December 2013 - 01:42 AM
i find that the moon is pretty good atm and i would not change it

It should be made out of cheese.
jay5476 #9
Posted 03 December 2013 - 02:33 AM
I know it should be made out of cheese but I want it to please even the people who are lactouse intolerant
oeed #10
Posted 03 December 2013 - 03:01 AM
My main issues with Lua are syntactical. I prefer "if (condition) {…}" over "if condition then … end". I'm always forgetting the 'then'. Same for for..do and while..do.

I couldn't agree more. As I switch between many languages these little things get me off guard. Pretty much every language (that I use at least, Objective-C, JavaScript, PHP etc) use curly brackets. Another thing that gets me some times is using '=' in tables rather than ':' which is used in JavaScript.

Now I think of it, I do miss semicolons. I once wrote half my program with semicolons at the end of each line before having a massive facepalm a few minutes in. Although… it doesn't seem to cause crashes after a quick test. Hmmm
distantcam #11
Posted 03 December 2013 - 03:20 AM
I know it should be made out of cheese but I want it to please even the people who are lactouse intolerant

Soy cheese then?

Now I think of it, I do miss semicolons. I once wrote half my program with semicolons at the end of each line before having a massive facepalm a few minutes in. Although… it doesn't seem to cause crashes after a quick test. Hmmm

You can use semicolons in Lua. They're optional. http://www.lua.org/pil/1.1.html
oeed #12
Posted 03 December 2013 - 03:49 AM
Now I think of it, I do miss semicolons. I once wrote half my program with semicolons at the end of each line before having a massive facepalm a few minutes in. Although… it doesn't seem to cause crashes after a quick test. Hmmm

You can use semicolons in Lua. They're optional. http://www.lua.org/pil/1.1.html

Yea, ok. You never really see anyone use them though.
Kingdaro #13
Posted 03 December 2013 - 04:02 AM
proper ternary operators/statements someCondition ? if-true : if-false why: the implementation we have available now just doesn't always cut it, it'd be nice to have a real one

The only real case where a ternary condition doesn't work is in this case:

var = condition and false or something_else
Where you wanted to set var to false if the condition was true. However, the easiest way to fix that is to reverse the condition and the "or"

var = not condition and something_else or false
If for some weird reason, the second and third values were false and nil, e.g. "var = condition and false or nil" (this statement would always evaluate to the last value, nil in this case), you're better off using an if statement, which is fine since most people don't really need to do this anyway.

data type checking function foo(string bar) why: I hope this is fairly obvious
Personal preference, but I'm fine with checking the type of the value if necessary.

default parameter values function foo(string bar = "none") why: I hope this is fairly obvious too, its one of the only parts of Python that I like
I agree on this one; the whole "value = value or default_value" syntax is pretty verbose and doesn't work if you want false as a default.

a few random metamethods, such as __type why: I make my own implementation of this way too much for OO Lua.
__type and __len (implemented in 5.2) would be very nice to have, yes.

a better assert function, preferably one like I made all that time ago that has support for the throwback levels! why: same reason as above, I make it way too often
I actually looked this up a while ago, and there's a specific reason for the assert that works the way it does. A lot of lua functions usually return a value if successful, or nil and then an error otherwise. It makes a neat syntax in a situation like this:

file = assert(io.open('somefile'))
Where assert would error out with the actual error from io.open. Giving a level to assert would erase every other value given from io.open as function params, e.g. the error message.



As for what I'd personally like, assignment operators (+=, -=, etc.) since I add numbers to themselves quite a bit, especially when working in games. ++ and – are definitely redundant though, as it's only one less character to type: "var += 1" vs. "var++".

A part of the reason why they don't implement features like ^that is so Lua remains such a small and speedy language.
Edited on 03 December 2013 - 03:02 AM
Wobbo #14
Posted 03 December 2013 - 05:09 AM
I would like a switch statement that isn't a function with a lookup table. There are ways around needing one, obviously, but it would be nice to have one if you have a lot of states, for example when checking for command line arguments.

I haven't really had the need to use one yet, but I think it would make checking for keys a lot easier. And if it used a lookup table underneath, also faster.
  1. data type checking function foo(string bar) why: I hope this is fairly obvious
  2. default parameter values function foo(string bar = "none") why: I hope this is fairly obvious too, its one of the only parts of Python that I like

1. Type checking is possible: http://lua-users.org...LuaTypeChecking
2. I would like to have this as well, and it can be achieved by creating a function that dynamically calls other functions. The wrapper function would have a closure with defaults and the order of the arguments, and it would call the function accordingly. Not really what you are looking for, but it is better than nothing.
SpoilerI haven't tested it yet, but it should work. The resulting function can be called normally, or with named arguments and the defaults are respected.

function rStyle(f, argOrder, defaults)
return function(...)
local t
if type(...) == 'table' then
t = ...
else
t = {...}
end
local args = {}
for i = 1, #argOrder do
args[i] = t[argOrder[i]] or t[i] or defaults[argOrder[i]] or defaults[i]
end
f(unpack(args))
end
end
I have decided that since not all arguments might have a default value, the argument order has to be specified separately. If all options do have a default option, the loop could look like this:

local args = {}
local i= 1
for k, v in pairs(defaults) do
  args[i] = t[k] or t[i] or v
  i = i +1
end
Edited on 03 December 2013 - 04:12 AM
theoriginalbit #15
Posted 03 December 2013 - 06:07 AM
-snip-
-snip-
I would like to just point out to both of you that I know reasons behind their design choices, and I know that some of it is already implementable but the point of me writing this is to see what people WOULD change given the chance, even if it can already be done now.

The only real case where a ternary condition doesn't work is in this case:

var = condition and false or something_else
Where you wanted to set var to false if the condition was true. However, the easiest way to fix that is to reverse the condition and the "or"

var = not condition and something_else or false
If for some weird reason, the second and third values were false and nil, e.g. "var = condition and false or nil" (this statement would always evaluate to the last value, nil in this case), you're better off using an if statement, which is fine since most people don't really need to do this anyway.
in which case a ternary is just cleaner.

Personal preference, but I'm fine with checking the type of the value if necessary.
yeh there's just some times where it'd be handy, especially in this case:
well for one, having static typing does allow for function overloading, which is one of the major things that I do want in Lua, but can only be achieved through type checking.

I agree on this one; the whole "value = value or default_value" syntax is pretty verbose and doesn't work if you want false as a default.
Indeed, like I stated, one of the only things about Python that I actually like :P/>

__type and __len (implemented in 5.2) would be very nice to have, yes.
Indeed, I actually just discovered the other day that we actually do have __metatable, I didn't think we did, actually would have sworn that we didn't have it!

I actually looked this up a while ago, and there's a specific reason for the assert that works the way it does. A lot of lua functions usually return a value if successful, or nil and then an error otherwise. It makes a neat syntax in a situation like this:

file = assert(io.open('somefile'))
Where assert would error out with the actual error from io.open. Giving a level to assert would erase every other value given from io.open as function params, e.g. the error message.
Ahh of course, makes sense.

As for what I'd personally like, assignment operators (+=, -=, etc.) since I add numbers to themselves quite a bit, especially when working in games. ++ and – are definitely redundant though, as it's only one less character to type: "var += 1" vs. "var++".

A part of the reason why they don't implement features like ^that is so Lua remains such a small and speedy language.
Good options to have. implementing those operators wouldn't make the language much larger, and also wouldn't slow it down at all, for example += and -= are functionally no different than expanding, its just less verbose. As for ++ and – they can definitely help make some statements less verbose.

1. Type checking is possible: http://lua-users.org...LuaTypeChecking
Of course its possible, I know its possible, but what I mean is this

local function foo(number bar)
  print(bar + 1)
end

foo(1) --#> 2
foo("bar") --#> error: expected number, got string
which obviously is implementable, like so

local function foo(bar)
  if type(bar) ~= "number" then
	error("expected number, got "..type(bar), 2)
  end
end
but the point of the former example is its far less verbose!

2. I would like to have this as well, and it can be achieved by creating a function that dynamically calls other functions. The wrapper function would have a closure with defaults and the order of the arguments, and it would call the function accordingly. Not really what you are looking for, but it is better than nothing.
-code snip-
yeh I've made a similar script in the past that dealt with type checking and defaults, so the signature was

parseArgs(args, argTypes, argDefaults)
and each table was an indexed table for it to work
Edited on 03 December 2013 - 05:07 AM
Bubba #16
Posted 03 December 2013 - 06:53 AM
A lot of mine have already been mentioned, but I figure that I may as well state them.
  • Indentation requirements. More for my sake when debugging other people's code than anything else. I'm not a fan of the requirement when I'm actually writing a program myself due to the fact that sometimes I just want to test something quickly and not bother with indentation. But when I see some 600 line conglomeration without indentation I quickly exit the tab and return to browsing.
  • += and the like. The reasoning for this should be obvious.
  • Use curly braces for blocks - I'm not a fan of keywords for that purpose
  • Semi-colons to end statements. Simply for clarity's sake.
  • Function overloading. One of the reasons that I'm really liking Java.
  • Comments that begin with // not –. – Looks ugly to me.
  • Compact the language somewhat: Do away with things such as "and" and replace them with symbols. I've always been a fan of compacting.
  • Declare things by default with a local scope, and specify global with global keyword or global symbol.
Edited on 03 December 2013 - 05:55 AM
theoriginalbit #17
Posted 03 December 2013 - 07:10 AM
Indentation requirements. More for my sake when debugging other people's code than anything else. I'm not a fan of the requirement when I'm actually writing a program myself due to the fact that sometimes I just want to test something quickly and not bother with indentation. But when I see some 600 line conglomeration without indentation I quickly exit the tab and return to browsing.
Yeh I'm very torn about this one, I like it for the reason that you do, but ti was one of the main reasons that I hate Python the enforced indentation, especially when it changes between IDEs and compilers! for example Uni wanted me to have indentation level of 2 so they could test it, but the IDE I was using had a compiler that was an un-configurable indent of 4.

Function overloading. One of the reasons that I'm really liking Java.
Definitely one of my top ones that I mentioned (kinda, well it was implied), I especially like languages such as C++ which allow operator overloading, which Lua is kinda limited on, like __eq and all that only work when its comparing two of your objects, not your object and another disparate data type.

Compact the language somewhat: Do away with things such as "and" and replace them with symbols. I've always been a fan of compacting.
Thoughts on what it could be? 'cause using something we have in other languages is not much more compact, like for example with Java && is not much more compact than and, also you'd want to avoid using ones like & just so you make sure none of your C and C-like programmers think that you mean bitwise and.

Declare things by default with a local scope, and specify global with global keyword or global symbol.
Yes! One million times yes! Definitely being added to the list! Even if its to make it like Python where you have to declare that the variable is global in the scope.
Edited on 03 December 2013 - 06:15 AM
Bomb Bloke #18
Posted 03 December 2013 - 08:54 AM
I'm not familiar enough with the language to really dis it; I'm still in the honeymoon stage, so to speak.

I wouldn't mind having to go without some of the redundant grammar already mentioned ("end"s, "then"s, "x=x+y"s, etc), though on the other hand, I don't mind having to use it (beats the days when I had to type "let"). But to add to the list of that sort of stuff, I suppose I'd like to remove the need to use the bit API to perform basic statements like "var1&var2".

Oh, and concatenation. Lua's the first language I've (knowingly) used which doesn't let me append values using just the "+" symbol. I assume there's a speed concern there (in that with .. it doesn't have to "figure out" whether I'm adding numerals or clumping strings together), but still.

The only other thing that comes to mind is the need to use brackets with print statements. Sure, you don't HAVE to do this as things stand, but… without them you can only print one value at a time. Whah…?!
theoriginalbit #19
Posted 03 December 2013 - 09:28 AM
I wouldn't mind having to go without some of the redundant grammar already mentioned ("end"s, "then"s, "x=x+y"s, etc)
At least its not like Pascal where you have to open a block with "begin" and close a block with "end" :P/>

The only other thing that comes to mind is the need to use brackets with print statements. Sure, you don't HAVE to do this as things stand, but… without them you can only print one value at a time. Whah…?!
Its actually any function call. the only condition is that it must be only one argument and a string. but yes that is a bit of an odd thing to add to the language.
Kingdaro #20
Posted 03 December 2013 - 09:29 AM
It's actually good for a language to have a separate addition and concatenation operator, so that the difference between 3 + 3 and 3 .. 3 is clear.
Lyqyd #21
Posted 03 December 2013 - 10:49 AM
False as a default works just fine. Imagine _var is nil:

var = _var or false

Just nitpicking. Lua is fine, and it seems like a lot of these suggestions just want to make it more like Java. The two serve totally different roles. I agree that some of the syntactic sugar would be nice (+=, I'm looking at you!), but overall, it's pretty good. I'd like to see more ideas that actually fit the rest of the language instead of stuff that just fanboys about turning it into java. :P/>

Also, symmetryc's suggestion about metatables on things that aren't tables is possible with __type, since you'd just claim the type to be something else.
M4sh3dP0t4t03 #22
Posted 03 December 2013 - 11:01 AM
Most of the stuff I would changed has already been said, but I would also like 0-indexed tables.
Edited on 03 December 2013 - 10:01 AM
distantcam #23
Posted 03 December 2013 - 11:45 AM
…instead of stuff that just fanboys about turning it into java. :P/>

Java sux. I want to turn it into C#.

*runs for cover*
Bubba #24
Posted 03 December 2013 - 12:25 PM
Yeh I'm very torn about this one, I like it for the reason that you do, but ti was one of the main reasons that I hate Python the enforced indentation, especially when it changes between IDEs and compilers! for example Uni wanted me to have indentation level of 2 so they could test it, but the IDE I was using had a compiler that was an un-configurable indent of 4.

I have no more love for Python than you do, and yeah I doubt I would like enforced indentation if I had it. However, it would be nice if people would indent their code…

and use code tags!

Thoughts on what it could be? 'cause using something we have in other languages is not much more compact, like for example with Java && is not much more compact than and, also you'd want to avoid using ones like & just so you make sure none of your C and C-like programmers think that you mean bitwise and.
I guess I don't really mean compact it. I actually would just like Lua to follow some of the more typical standards such as using '&&' rather than 'and'. I get used to doing it one way and the switch is annoying.

Just nitpicking. Lua is fine, and it seems like a lot of these suggestions just want to make it more like Java. The two serve totally different roles. I agree that some of the syntactic sugar would be nice (+=, I'm looking at you!), but overall, it's pretty good. I'd like to see more ideas that actually fit the rest of the language instead of stuff that just fanboys about turning it into java. :P/>

Java is a nice language so meh :P/>

But suggesting ideas that "fit the language" would mean little or no changes at all. Sometimes you have to break away from the "it doesn't fit" mentality :)/>
Symmetryc #25
Posted 03 December 2013 - 02:38 PM
Um, how will number– work if is for comments?

Its actually any function call. the only condition is that it must be only one argument and a string. but yes that is a bit of an odd thing to add to the language.
It actually also works with tables.
Edited on 03 December 2013 - 01:38 PM
Engineer #26
Posted 03 December 2013 - 02:47 PM
Um, how will number– work if is for comments?
Really, nobody spoke about that syntactical sugar :P/>
They have talked about +=, -=, /= etc.

How awesome Java is, keep it on its own language. I see the problem where we cant progrram with Java in CC, but thats for obvious reasons.
Since we are just talking about programming languages:
Keep Lua Lua, and Java Java.

Really, its like comparing a brick with a banana. Pointless :P/>
H4X0RZ #27
Posted 03 December 2013 - 03:12 PM
Maybe a goto function to jump to declared position like in Batch.

Batch example:

firstplace:
echo first
goto secondplace

secondplace:
echo second

goto firstplace
Engineer #28
Posted 03 December 2013 - 03:20 PM
Maybe a goto function to jump to declared position like in Batch.

Batch example:

firstplace:
echo first
goto secondplace

secondplace:
echo second

goto firstplace
You can achieve that using functions, though you have a stack overflow then
Symmetryc #29
Posted 03 December 2013 - 04:06 PM
Um, how will number– work if is for comments?
Really, nobody spoke about that syntactical sugar :P/>
Well yeah, but BIT and Bubba did, so I just thought I'd mention it.
Bomb Bloke #30
Posted 03 December 2013 - 04:38 PM
Maybe a goto function to jump to declared position like in Batch.
I gather it's in, as of 5.2.
Bubba #31
Posted 03 December 2013 - 04:59 PM
Maybe a goto function to jump to declared position like in Batch.

Batch example:

firstplace:
echo first
goto secondplace

secondplace:
echo second

goto firstplace

I refer you to this thread. Goto is a valid tool in some situations - however, I don't believe that it would be helpful to new programmers, which seems to be what Lua caters to (or at least what ComputerCraft caters to). In many cases it would be hurtful - goto leaves a lot of uncertainty in some situations and complicates things more than necessary.

I disagree with the decision to add goto, but then again I am not on the PUC-Rio team so my opinion has little weight.

Um, how will number– work if – is for comments?


As I suggested in my earlier post, comments in Lua should be changed to the form of // rather than –. // Looks far better in my opinion and would allow for syntactic sugar such as n–.
Edited on 03 December 2013 - 04:30 PM
PixelToast #32
Posted 03 December 2013 - 05:13 PM
it seems many of this is in a language ive been making that compiles into lua

here is a breif explanation of it
everything global is localized at the top of the program unless marked as global
parentheses are optional and are only required if you want to call multiple functions in one line
function statements can support arg types including a special one for "…"

-- function argstest args:tArgs {
--  walrus
-- }
-- function test number:num {
--  for a 1 num {
--  print "Potato $a$!"
--  }
-- }
-- test 10

local argtest,test
function argstest(...)
  local tArgs={...}
  walrus()
end
function test(num)
  assert(type(num)=="number","bad argument number:num expected, got "..type(num))
  for a=1,num do
	print("Potato "..a.."!")
  end
end
test(10)

it is intended to be a language easier to code in and eliminate common mistakes new people do
distantcam #33
Posted 03 December 2013 - 06:55 PM
I don't think anyone is proposing we swap Lua for Java in ComputerCraft. But Java has some nice features that are missing from Lua and since most people here would be familiar with Java it's being used as the example.
theoriginalbit #34
Posted 03 December 2013 - 07:08 PM
Its actually any function call. the only condition is that it must be only one argument and a string. but yes that is a bit of an odd thing to add to the language.
It actually also works with tables.
only table constructors, you can't do this

local t = {}
print t
it has to be

print {}
which is kinda pointless

Just nitpicking. Lua is fine, and it seems like a lot of these suggestions just want to make it more like Java.
I wouldn't exactly say they are. Pretty much all the suggestions are generic to most high level programming language, except for the one that distantcam stated that was C#
Edited on 03 December 2013 - 06:10 PM
oeed #35
Posted 03 December 2013 - 07:40 PM
I don't think anyone is proposing we swap Lua for Java in ComputerCraft. But Java has some nice features that are missing from Lua and since most people here would be familiar with Java it's being used as the example.

I find Java such a pain to work with, especially Java tables/arrays/dictionaries/what-ever-you-want-to-call-them. I started making an Inception inspired mod but the arrays were just such a pain to work with.
distantcam #36
Posted 03 December 2013 - 07:47 PM
I wouldn't exactly say they are. Pretty much all the suggestions are generic to most high level programming language, except for the one that distantcam stated that was C#

Lambdas are coming in Java 8. http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

I find Java such a pain to work with, especially Java tables/arrays/dictionaries/what-ever-you-want-to-call-them. I started making an Inception inspired mod but the arrays were just such a pain to work with.

I do like the way tables work in Lua.
Symmetryc #37
Posted 03 December 2013 - 08:21 PM
Its actually any function call. the only condition is that it must be only one argument and a string. but yes that is a bit of an odd thing to add to the language.
It actually also works with tables.
only table constructors, you can't do this

local t = {}
print t
it has to be

print {}
which is kinda pointless
Same with strings though:

local s = "567"
print s
Wouldn't work
Edited on 03 December 2013 - 07:21 PM
GravityScore #38
Posted 04 December 2013 - 03:10 AM
Default arguments for functions. This is one of the few things I like about Python, and hate about Javascript.

I sort of like the if … then and for/while … do, so I wouldn't personally swap them for curly braces, although I still love curly braces.

I dislike having two different operators for concatenation and addition - it'd be better if it was just +.

I really hate the – for commenting. // or even # is better (// is superior IMO).

+= and -= are something I can't stand Lua not having. I use them in code and then get an error, and it just annoys me.

I dislike how some languages like to be different (from C/Java) with their for loops. I like the 3 separate statements in a for loop. It allows the conditional of a for loop to be completely unrelated to the initialisation and the increment, which is so useful when reading from a file, etc. This is something I dislike about python, although it's worse in Lua as Lua's for loop syntax is more confusing than Python's.

I dislike how when you try to print out something in Lua, it just shows the type and memory address. How I wish the tostring function for tables actually returned some useful data.

I loooovvveeee how tables work in Lua. I wish every higher level language would follow Lua's implementation of tables.

I hate enforced indenting. It is such a damned pain in Python, especially if I open the file with a different IDE/editor, and it starts using spaces for tabs instead of actual tabs. Python just complains to me and it takes me a while to figure out why it's complaining because visually they look the same.
distantcam #39
Posted 04 December 2013 - 03:19 AM
To be clear, my complaint about if…then was the then part. I'd be happy with "if (condition) … end" where the brackets denote the end of the condition instead of the then.
theoriginalbit #40
Posted 04 December 2013 - 03:35 AM
To be clear, my complaint about if…then was the then part. I'd be happy with "if (condition) … end" where the brackets denote the end of the condition instead of the then.
Again, try Pascal :P/>

if condition then
begin
  statements;
end;

Although the above example could be

if condition then
  statements;

Its just if your block will be more than 1 statement you need to add the begin…end
Edited on 04 December 2013 - 02:36 AM
MCGamer20000 #41
Posted 07 April 2014 - 03:34 AM
I'm trying to not be rude, but here are some solutions.
Spoiler
proper ternary operators/statements someCondition ? if-true : if-false
function iftxt(cond, tout, fout)
  if cond then
    return tout
  elseif fout~=nil then
    return fout
  end
end
print(iftxt(true,"Yes!","No!")) -- Output: Yes!
print(iftxt(false,"Meh.")) -- Won't output anything.
data type checking function foo(string bar)
OpenComputers has this code:
Spoiler
local function checkArg(n, have, ...)
  have = type(have)
  local function check(want, ...)
    if not want then
      return false
    else
      return have == want or check(...)
    end
  end
  if not check(...) then
    local msg = string.format("bad argument #%d (%s expected, got %s)",
                              n, table.concat({...}, " or "), have)
    error(msg, 3)
  end
end
function foo(bar)
  checkArg(1, bar, "string")
  -- Do something
end
Using the example on the linked page:
function volume(a, b, c)
  checkArg(1, a, "number", "nil")
  checkArg(2, b, "number", "nil")
  checkArg(3, c, "number", "nil")
  if type(a)=="number" and type(B)/>/>=="nil" and type(c)=="nil" then
    return a*a*a
  elseif type(a)=="number" and type(B)/>/>=="number" and type(c)=="nil" then
    return 3.14*a*a*b
  elseif type(a)=="number" and type(B)/>/>=="number" and type(c)=="number" then
    return a*b*c
  end
  print(volume(10))
  print(volume(2.5, 8))
  print(volume(100, 75, 15))
end
default parameter values function foo(string bar = "none")
function foo(bar)
  if bar==nil then bar = "none" end
  -- Do something
end
binary numbers b10011000
function binary(seq)
  local num = 0
  local cnt = 1
  seq = string.reverse(seq)
  for plc = 1, string.len(seq) do
    local chr = string.sub(seq, plc, plc)
    if chr == "1" then
      num = num + cnt
    elseif chr ~= "0" then
      error("Not a binary sequence.")
    end
    cnt = cnt * 2
  end
  return num
end
print(binary("10011000"))
a few random metamethods, such as __type
oldtype = type
type = function(obj)
  if obj.__type then
    return obj.__type
  end
  return oldtype(obj)
end
a better assert function, preferably one like I made all that time ago that has support for the throwback levels!
[Insert the one you made all that time ago here]
awsmazinggenius #42
Posted 07 April 2014 - 04:13 AM
Now I think of it, I do miss semicolons. I once wrote half my program with semicolons at the end of each line before having a massive facepalm a few minutes in. Although… it doesn't seem to cause crashes after a quick test. Hmmm

You can use semicolons in Lua. They're optional. http://www.lua.org/pil/1.1.html

Yea, ok. You never really see anyone use them though.

Now that this is bumped, I might as well give my opinion: I like using them on pieces of code like this:

local someVar;
if something then
  someVar = true
end
where I use the semicolon after the local someVar part, because I think it makes it look better.
theoriginalbit #43
Posted 07 April 2014 - 04:38 AM
I'm trying to not be rude, but here are some solutions.
of course there are always solutions, my point of this was to have it NATIVE not require us to implement us something.
I am quite fluent in Lua, all of your implementations are what I do on a regular basis when needed; again this was not the point I was getting at.

data type checking function foo(string bar)
-snip 'solution'-
very verbose and functionally no better than just doing asserts.
i am talking about this

local function foo( string bar, number count )
end

-snip 'suggestion'-
not quite the point I was getting at, I know its possible

default parameter values function foo(string bar = "none")
-'suggestion' snip'-
again, not the point I was getting at.

local function foo( bar = "hello world" )
  print(bar)
end
foo() --# prints 'hello world'
foo("fred wilma") --# prints 'fred wilma'

binary numbers b10011000
function binary(seq)
  local num = 0
  local cnt = 1
  seq = string.reverse(seq)
  for plc = 1, string.len(seq) do
	local chr = string.sub(seq, plc, plc)
	if chr == "1" then
	  num = num + cnt
	elseif chr ~= "0" then
	  error("Not a binary sequence.")
	end
	cnt = cnt * 2
  end
  return num
end
print(binary("10011000"))
wtf is wrong with you, I'm just not even going to- just- ugh.

a few random metamethods, such as __type
oldtype = type
type = function(obj)
  if obj.__type then
	return obj.__type
  end
  return oldtype(obj)
end
well actually you're wrong with that implementation. here's a better one that actually implements it as a metamethod.

local nativeType = type
function type( obj )
  local ok, mt = pcall( getmetatable, obj )
  if nativeType(mt) == "table" and mt.__type then
	return mt.__type
  end
  return obj.__type or nativeType( obj )
end
Edited on 07 April 2014 - 02:46 AM
MCGamer20000 #44
Posted 07 April 2014 - 05:45 AM
Well, I'm still (kinda) a Lua noob… But I did have fun trying to create code for each thing you said.
Graypup #45
Posted 07 April 2014 - 06:33 PM
A switch statement
A string-supporting switch statement
Who needs functional overloading? It can be done with some type() and some if statements.
Bracket notation, remembering ends is stupid, and brackets are easier
Ways to escape some boilerplate like

str = str or "blah"
6677 #46
Posted 07 April 2014 - 07:13 PM
I would force curly braces and semi colons while forcing all comparisons in while loops and if statements to be placed in brackets.
theoriginalbit #47
Posted 08 April 2014 - 02:32 AM
Who needs functional overloading? It can be done with some type() and some if statements.
I can become very verbose and annoying if there are too many arguments. Function overloading can make it a lot easier and cleaner. Take the following example:

local function assert( cdn, msg, lvl )
  if not cdn then
    error(msg, lvl + 1)
  end
  return cdn
end

local function createButton( x, y, width, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
  assert(type(x) == "number", "expected number", 3)
  assert(type(y) == "number", "expected number", 3)
  assert(type(width) == "number", "expected number", 3)
  assert(type(height) == "number", "expected number", 3)
  assert(type(text) == "string", "expected string", 3)
  assert(type(placeholderText) == "string", "expected string", 3)
  assert(type(func) == "function", "expected function", 3)
  assert(type(backColorNormal) == "number", "expected number", 3)
  assert(type(textColorNormal) == "number", "expected number", 3)
  assert(type(backColorSelected) == "number", "expected number", 3)
  assert(type(textColorSelected) == "number", "expected number", 3)

  --# create it all here
end

function newButton( x, y, text, func )
  createButton( x, y, #text + 2, 3, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( x, y, height, text, func )
  createButton( x, y, #text + 2, height, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( x, y, text, func, backColorNormal, textColorNormal )
  createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( x, y, height, text, func, backColorNormal, textColorNormal )
  createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( x, y, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
  createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( x, y, height, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
  createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( x, y, text, placeholderText, func )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( x, y, height, text, placeholderText, func )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( x, y, height, text, placeholderText, func, backColorNormal, textColorNormal )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( x, y, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

now go ahead and make something with the same functionality as above with if statements; it should be just as readable and contain no bugs/typos. k. go.
Edited on 08 April 2014 - 12:36 AM
Lyqyd #48
Posted 08 April 2014 - 05:15 AM

--5
function newButton( x, y, height, text, func )
function newButton( x, y, text, placeholderText, func )

--6
function newButton( x, y, height, text, placeholderText, func )
function newButton( x, y, text, func, backColorNormal, textColorNormal )

--7
function newButton( x, y, height, text, func, backColorNormal, textColorNormal )
function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal )

--8
function newButton( x, y, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
function newButton( x, y, height, text, placeholderText, func, backColorNormal, textColorNormal )

--9
function newButton( x, y, height, text, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
function newButton( x, y, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )

Uh-huh, sure. This isn't gonna work unless you also start assuming static typing, which makes it dumb for a language to handle varying argument counts any other way. With those assumptions, of course Lua's way doesn't work.
theoriginalbit #49
Posted 08 April 2014 - 05:19 AM
Uh-huh, sure. This isn't gonna work unless you also start assuming static typing, which makes it dumb for a language to handle varying argument counts any other way. With those assumptions, of course Lua's way doesn't work.
From the OP
2. data type checking function foo(string bar) why: mainly 'cause of the next point;
3. function overloading requires above;
Lyqyd #50
Posted 08 April 2014 - 05:46 AM
From the OP
2. data type checking function foo(string bar) why: mainly 'cause of the next point;
3. function overloading requires above;

But then you went ahead and didn't specify types in the function declarations! :P/>
theoriginalbit #51
Posted 08 April 2014 - 07:47 AM
But then you went ahead and didn't specify types in the function declarations! :P/>
Oh shush you :P/> I was writing that code during a lecture as it is :P/>
theoriginalbit #52
Posted 08 April 2014 - 01:24 PM
But then you went ahead and didn't specify types in the function declarations! :P/>
Just for you :P/>

Updated Example

local function assert( cdn, msg, lvl )
  if not cdn then
    error(msg, lvl + 1)
  end
  return cdn
end

local function createButton( number x, number y, number width, number height, string text, string placeholderText, function func, number backColorNormal, number textColorNormal, number backColorSelected, number textColorSelected )
  --# create it all here
end

function newButton( number x, number y, string text, function func )
  createButton( x, y, #text + 2, 3, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( number x, number y, number height, string text, function func )
  createButton( x, y, #text + 2, height, text, "", func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( number x, number y, string text, function func, number backColorNormal, number textColorNormal )
  createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( number x, number y, number height, string text, function func, number backColorNormal, number textColorNormal )
  createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( number x, number y, string text, function func, number backColorNormal, number textColorNormal, number backColorSelected, number textColorSelected )
  createButton( x, y, #text + 2, 3, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( number x, number y, number height, string text, function func, number backColorNormal, number textColorNormal, number backColorSelected, number textColorSelected )
  createButton( x, y, #text + 2, height, text, "", func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( number x, number y, string text, string placeholderText, function func )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( number x, number y, number height, string text, string placeholderText, function func )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, colors.blue, colors.white, colors.lightBlue, colors.black )
end

function newButton( number x, number y, string text, string placeholderText, function func, number backColorNormal, number textColorNormal )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( number x, number y, number height, string text, string placeholderText, function func, number backColorNormal, number textColorNormal )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorNormal, textColorNormal )
end

function newButton( number x, number y, string text, string placeholderText, function func, number backColorNormal, number textColorNormal, number backColorSelected, number textColorSelected )
  createButton( x, y, math.max(#text, #placeholderText) + 2, 3, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end

function newButton( number x, number y, number height, string text, string placeholderText, function func, number backColorNormal, number textColorNormal, number backColorSelected, number textColorSelected )
  createButton( x, y, math.max(#text, #placeholderText) + 2, height, text, placeholderText, func, backColorNormal, textColorNormal, backColorSelected, textColorSelected )
end
Edited on 08 April 2014 - 11:24 AM
LoneRangerGuy #53
Posted 11 April 2014 - 10:34 PM
One of my biggest difficulties in picking up Lua has been in reading other people's code. Most well written code isn't a problem but I've found a lot of code that works quite well but is just plain ugly and hard to read. I tried to find a beautifier that would clean up ugly code but haven't been able to find anything robust that works on all working but ugly code. I'd suspect a big reason for that is that in Lua it may be more difficult to do than in other more structured languages.

The Python haters won't like this, but along with some of the other posters I would also tend to favor stricter formatting rules, but not necessarily just through indentation as with Python, I'd also accept the use of something like curly brackets or anything that helps make code easier to parse and for a human to read. The main reason for me is finding other people's code to be vastly different and I've yet to find a good, robust beautifier that works with all working but ugly code. Many folks hate Python because of it's indentation rules, but that's one of the reasons I love it, it makes it much easier to understand someone else's code. I wasn't always a fan of such rules, but after spending some time with Python and especially in adapting to other people's Python code it grew on me.

It is evident that everyone's different, or every flavor would be vanilla, but I think anything promoting readability and understandability without otherwise having an affect on function would be a good thing in any language.
theoriginalbit #54
Posted 12 April 2014 - 02:06 AM
-snip-
the best I've really found is the reindenter in Sublime Text. Made a little better by the ComputerCraft plugin GravityScore made.
Saldor010 #55
Posted 12 April 2014 - 02:39 AM
If I could change 1 thing about Lua, I would make it so Lua automatically defines all of the functions that are on the main path of the program (not nested in any conditionals or loops) before running the program. That way stuff like this,


test("Hi!")
function test(str)
   print(str)
end

wouldn't error.
Kingdaro #56
Posted 13 April 2014 - 12:43 AM
I don't agree. Functions are a value type, and defining a function that way is the same as "test = function(str) print(str) end". By that logic,

print(var)
var = 5
would work, when obviously it shouldn't as var hasn't been defined yet.
Edited on 12 April 2014 - 10:43 PM
Saldor010 #57
Posted 13 April 2014 - 03:10 AM
I don't agree. Functions are a value type, and defining a function that way is the same as "test = function(str) print(str) end". By that logic,

print(var)
var = 5
would work, when obviously it shouldn't as var hasn't been defined yet.

Yeah, I guess you're right.
Shazz #58
Posted 16 April 2014 - 10:25 PM
If I could change 1 thing about Lua, I would make it so Lua automatically defines all of the functions that are on the main path of the program (not nested in any conditionals or loops) before running the program. That way stuff like this,


test("Hi!")
function test(str)
   print(str)
end

wouldn't error.


I don't see any point to that.
If you want indirect recursive functions, simply use forward declarations.
Example:

local foo, bar
function foo()
  bar()
end
function bar()
  foo()
end
MKlegoman357 #59
Posted 16 April 2014 - 10:52 PM
If I could change 1 thing about Lua, I would make it so Lua automatically defines all of the functions that are on the main path of the program (not nested in any conditionals or loops) before running the program. That way stuff like this,


test("Hi!")
function test(str)
   print(str)
end

wouldn't error.


I don't see any point to that.
If you want indirect recursive functions, simply use forward declarations.
Example:

local foo, bar
function foo()
  bar()
end
function bar()
  foo()
end

of course there are always solutions, my point of this was to have it NATIVE not require us to implement us something.
ElvishJerricco #60
Posted 16 April 2014 - 11:16 PM
I really wish Lua had a good way of working between files and with objects, but without relying on string based indexing. I know that nowadays that stuff is fast and extremely well optimized, but it still just feels bad compared to numeric indexing. Especially for performance intensive tasks like the JIT in JVML-JIT. Unfortunately this is the fundamental opposite of what Lua wants to be.
Shazz #61
Posted 18 April 2014 - 07:56 PM
of course there are always solutions, my point of this was to have it NATIVE not require us to implement us something.

This is native, you're not implementing anything. This is the way to do forward declarations in Lua.
Unless he was implying that all local functions should be forwarded automatically which doesn't make sense because you don't always want that and as Kingdaro pointed out, all functions are essentially variables so that would cause problems.
CCNerd a.k.a CCNoob #62
Posted 15 July 2014 - 08:44 AM
Goto in lua 5.1
SquidDev #63
Posted 16 July 2014 - 08:09 AM
Goto in lua 5.1

No. Please, please no. I know several people who started programming with CC and the last thing we need is to bring in another generation of goto users (BASIC anyone?).I know that deep down in the guts of the machine, goto (or branching as it is known (I think)) is how ifs, whiles and any conditional are evaluated, but that isn't the point - you just end up getting spaghetti code. I can't think of a situation when goto is more useful and sensible than functions.
Bomb Bloke #64
Posted 16 July 2014 - 08:22 AM
I can - every time I want to build a great big nested structure and escape from it, I have to lump the whole thing into a function to do so (via return), or else it's a matter of checking a ton of conditionals until I've extricated myself from the how-ever-many blocks I've buried myself in. I dislike making function calls unless it's to re-run repetitive code chunks (as it's redundant), and goto would solve that problem.

Other than that, though, I'm not aware of any other time its use would be justified. But still, lots of commands get mis-used… Probably shell.run() gets it the worst as far as ComputerCraft is concerned.
theoriginalbit #65
Posted 16 July 2014 - 08:27 AM
But still, lots of commands get mis-used… Probably shell.run() gets it the worst as far as ComputerCraft is concerned.
shell.exit too :P/> oh and the entire Parallel API.
Edited on 16 July 2014 - 06:27 AM
CCNerd a.k.a CCNoob #66
Posted 16 July 2014 - 08:49 AM
paraell APi… anything to do multitask as an alternative? (like: guys who make OSes, how do you make them miltitask? like having a million buttons and a trillion textfields and they still handle well

goto("hell")

::hell::
print("yay")
Bomb Bloke #67
Posted 16 July 2014 - 09:38 AM
Generally, if you want a section of code to handle multiple goings-on, you just throw in a loop with an event listener and a whole bunch of "if" statements to see what events are being thrown. If a timer expires, you might flash a cursor… if a key is typed, you might add the resulting character to the text field you've got set as active… if a mouse button is clicked, you might set a different text field as active… and then you just repeat your loop, and wait for the next event.

A problem arises if you want to call functions that pull events on their own, such as read() (as this has the potential to mess up your event listener loop). You've then got two choices: Either re-write those functions so that they can be integrated directly into your listener loop's set of "if" statements, or run the code that calls those functions in a separate co-routine to your listener loop. The best solution varies depending on the complexity of the functions in concern… For example, it's trivial to re-write sleep() or rednet.receive(), but functions like read() or the song playing function from the "note" song playing API are often better ran via the likes of the parallel API.
Edited on 16 July 2014 - 07:43 AM
Alice #68
Posted 02 August 2014 - 06:28 AM
This has probably been requested multiple times. Maybe even msyelf and I forgot, but here are to things I want:
Static typing
Method/Function overloading.
I like how Java enforces it and I want it to be implemented into Lua.
Geforce Fan #69
Posted 03 August 2014 - 04:20 AM
Add a default way of doing 2d and 3d graphics Obvoisly ComputerCraft wouldn't ever get this capability even if Lua does(and I wouldn't have it any other way) but when I'm doing normal Lua it'd be nice to not have to use some engine.
Alice #70
Posted 03 August 2014 - 05:07 AM
Add a default way of doing 2d and 3d graphics Obvoisly ComputerCraft wouldn't ever get this capability even if Lua does(and I wouldn't have it any other way) but when I'm doing normal Lua it'd be nice to not have to use some engine.
Good luck with that. As many people have told me in other areas, Lua's a scripting language. It's not likely they'll add custom support. As much as I hate it, Lua's supposed to be a lightweight language with minimal functions and syntax. They won't be adding that in any time soon.
zekesonxx #71
Posted 25 August 2014 - 07:24 PM
(this post was written without reading many of the other posts)
  • var++, var–, ++var, –var
  • Brackets instead of then/end
  • String concatenation done with `+` instead of `..`
  • Absolute comparisons `5 === "5"` = false
  • Arrays/objects with : instead of =
  • Comments done with // and /* */
  • Errors if you don't use local/global when declaring a variable
Lyqyd #72
Posted 25 August 2014 - 07:40 PM
Um , `5 == "5"` is already false.
TheOddByte #73
Posted 25 August 2014 - 08:12 PM
Oh I wish LuaJ had this, @zekesonxx Are you a fellow C# user? :D/>

local var = 10;
var += 1;
var++;
var--;
var *= 10;
var /= 2;
var ^= 3;
I'm not sure what else I would want to change, this is the only thing that comes to mind. I guess this topic is about Lua in general and if I'm not mistaken this has already been added in a new Lua version
Ferdi265 #74
Posted 25 August 2014 - 08:45 PM
Generally, if you want a section of code to handle multiple goings-on, you just throw in a loop with an event listener and a whole bunch of "if" statements to see what events are being thrown. If a timer expires, you might flash a cursor… if a key is typed, you might add the resulting character to the text field you've got set as active… if a mouse button is clicked, you might set a different text field as active… and then you just repeat your loop, and wait for the next event.

A problem arises if you want to call functions that pull events on their own, such as read() (as this has the potential to mess up your event listener loop). You've then got two choices: Either re-write those functions so that they can be integrated directly into your listener loop's set of "if" statements, or run the code that calls those functions in a separate co-routine to your listener loop. The best solution varies depending on the complexity of the functions in concern… For example, it's trivial to re-write sleep() or rednet.receive(), but functions like read() or the song playing function from the "note" song playing API are often better ran via the likes of the parallel API.

I wrote an event loop API that gives you listener functions and handles os.pullEvent from 3rd party code by overriding os.pullEvent and also forwarding it to the loop API. using coroutines and adding functions to interact with the coroutine loop. My API

event functions like read() work when called from within my loop, but it halts everything during read. (A custom read could fix this) However, all events that fired during read are still handled, which is nice :D/>

Code executes during read. Read is a little messed up if you print during read, but it works :D/>
Edited on 27 August 2014 - 03:28 PM
sci4me #75
Posted 26 August 2014 - 12:10 AM
A lot of mine have already been mentioned, but I figure that I may as well state them.
  • Indentation requirements. More for my sake when debugging other people's code than anything else. I'm not a fan of the requirement when I'm actually writing a program myself due to the fact that sometimes I just want to test something quickly and not bother with indentation. But when I see some 600 line conglomeration without indentation I quickly exit the tab and return to browsing.
  • += and the like. The reasoning for this should be obvious.
  • Use curly braces for blocks - I'm not a fan of keywords for that purpose
  • Semi-colons to end statements. Simply for clarity's sake.
  • Function overloading. One of the reasons that I'm really liking Java.
  • Comments that begin with // not –. – Looks ugly to me.
  • Compact the language somewhat: Do away with things such as "and" and replace them with symbols. I've always been a fan of compacting.
  • Declare things by default with a local scope, and specify global with global keyword or global symbol.

If the indentation requirement is 2 or 4 spaces, i'm fine with it. Although, in any other case I dislike that… maybe make it optional? IDK.
+=, etc. obvious reasons
real ternary operator, obvious reasons
Honestly, I dislike the semicolon thing. I mean, you CAN add semicolons can't you? I see them as extra typing…
I also prefer curly braces, don't like then and end…
Function overloading FTW! Although, it can be done… but its kind of a pain afaik… iirc SEE makes it a bit easier.
I don't really mind – but I prefer //
Yeah, I prefer symbols like &/| instead of and and or…
I also agree on the local scope thing. Makes much more sense.

I also dislike the concat operator.. I mean, sure it is good to seperate it from math operators… but.. maybe a different symbol? I suppose its no big deal…
I do love that lua is dynamic but I also love static typing. This issue has me partially torn… but I don't think I really NEED static typing…
I LOVE lua's functional style, but better language support for OO would be nice…
Also, language level support for the bit api!!!

Better metamethods and operator overloading might be nice.. but its not that bad as is.

Oh, of course! Functions with different parameters but the same name!!! Seriously, WHY IS THIS NOT A THING? I know it can sorta be done with normal lua functions, but … It could be so much better!

Don't get me wrong, I absolutely LOVE lua! It is my second favorite language besides Java. I think my biggest thing with it is the syntax… use curly braces and symbols for and and or. And real ternary.
InputUsername #76
Posted 27 August 2014 - 03:26 PM
The one thing I would love to have (probably mentioned before) are metatables for other datatypes than tables (numbers, functions). And obviously things (++, –, && etc) mentioned before.
Ferdi265 #77
Posted 27 August 2014 - 05:41 PM
sci4me said:
better language support for OO would be nice…
Lua uses an OO system that is different from the Classical System, the Prototypal System (which is also used in JavaScript)
In Lua you can easily implement inheritance via the __index metamethod, and constructor functions are also really easy to create.

Small Prototypal OO Library
Spoiler

extend = function (target, ...)
  target = target or {}
  local sources = {...}
  for index, source in ipairs(sources) do
	for key, value in pairs(source) do
	  target[key] = source[key]
	end
  end
  return target
end
proto = function (Proto, ...)
  Proto = Proto or {}
  local sources = {...}
  if #sources == 0 then
	table.insert(sources, Proto)
	Proto = {}
  end
  local instance = {}
  setmetatable(instance, {__index = Proto})
  return extend(instance, unpack(sources))
end
inst = function (Proto, ...)
  Proto = Proto or {}
  local args = {...}
  local instance = {}
  setmetatable(instance, {__index = Proto})
  if instance.init then
	instance:init(unpack(args))
  end
  return instance
end

-- use the library
local Person = {
  init = function (self, name)
	self.name = name
  end,
  sayhi = function (self)
	print("Hi, I'm " .. self.name)
  end
}
local FormalPerson = proto(Person, {
  sayhi = function (self)
	print("Hello, my name is " .. self.name)
  end
})

local peter = inst(Person, "Peter")

peter:sayhi()

local sirDerp = inst(FormalPerson, 'Sir Derp')

sirDerp:sayhi()

sci4me said:
Functions with different parameters but the same name!!! Seriously, WHY IS THIS NOT A THING?
Because functions are values. having two functions with the same name is like storing two functions in one variable, which isn't quite possible. (also lua wouldn't know what to call if you did

overloadedFunction(unpack(someTable))
Edited on 27 August 2014 - 03:44 PM
0099 #78
Posted 28 August 2014 - 02:07 PM
Who needs functional overloading? It can be done with some type() and some if statements.
I can become very verbose and annoying if there are too many arguments. Function overloading can make it a lot easier and cleaner. Take the following example:

--snip--

now go ahead and make something with the same functionality as above with if statements; it should be just as readable and contain no bugs/typos. k. go.

Speaking of annoyance, writing (and reading) 65536 different functions instead of 16 nested ifs doesn't seem very interesting… (Just an example, to make it obvious)
In addition, if statements are more flexible: sharing code chunks, checking arguments in different order (or in 2 orders at the same time), etc.

Everything that I "would change", actually can be changed from inside, that's the beauty of Lua. Currently I'm rewriting LÖVE framework (love2d.org) from inside, trying to completely remove callback mechanics from it, just because I don't like callbacks. Advice for theoriginalbit and all the others who want overloading: you can automate the process of "pseudo-overloading" by writing a function that checks argument types (call it "atype", for example) and using it like this:


function f(...)
   local args = {...}
   if atype(args, "string s1, number n, string s2") then
	  -- one function
   elseif atype(args, "string s1, string s2, thread cor, number m") then
	  -- another function
   else
	  error("Incorrect arguments to f")
   end
end

Also, see that variable names inside the strings? So, when atype at last finds correct argument types, before returning true, it rearranges the "args" table, so all your arguments are availble as args.s1, args.n etc.

If you want the syntax of overloading, well, that's a little harder, but also possible! Hint:

newfunc("boolean f(string s1, number n, string s2)", function(arg)
-- your code
end)
If you think that's ugly, well, there is one last way: write an additional program with bunch of "string.gsub"s that translate java-like definitions into that.
Edited on 28 August 2014 - 12:33 PM
MKlegoman357 #79
Posted 28 August 2014 - 09:55 PM
I think people are forgetting the purpose of this thread:

theoriginalbit said:
…and I know that some of it is already implementable but the point of me writing this is to see what people WOULD change given the chance, even if it can already be done now.
Edited on 28 August 2014 - 07:55 PM
KaoS #80
Posted 21 September 2014 - 08:47 AM
i know that it is not possible for Lua to have the functionality because it is not a statically typed language, however that being said this topic doesn't need to make sense in the realm of what it possible, it was more just me seeing what other people thought, even if it were not possible without changing everything about Lua. I find it very interesting to find out peoples ideas, perceptions, and opinions of programming, no matter how feasible it is.

I'm returning from a leave of absence so this may be late but you CAN actually implement all of the suggestions in your original post with a simple Lua code that changes the loadstring and similar functions. the binary number is the easiest example


local loads=loadstring

local convertToDecimal(nBinary)
  return tonumber(nBinary,2)
end

loadstring=function(sCode,...)
  if type(sCode)=="string" then
	sCode=sCode:gsub("b([01]+)",convertToDecimal)
  end
  return loads(sCode,...)
end
I realize you still have to ensure that that b[01]+ is not enclosed by quotation marks but you get the idea. I make a simple Lua file with similar workarounds that I use often and just include it in all my projects when I'm coding them, later I copy across the code I need or convert it to traditional syntax

EDIT: Lol I just read more of the thread, looks like I've fallen into the same trap as many others. At least I'm suggesting a code that changes the way Lua works rather than forcing you to code how Lua works but I apologise for not keeping to topic. I too would like Lua to support more metatable events. That's pretty much my only gripe other than perhaps a better Regex version and the RETARDED multiline comment thing
Edited on 21 September 2014 - 07:03 AM
KaoS #81
Posted 21 September 2014 - 08:58 AM
sci4me said:
Functions with different parameters but the same name!!! Seriously, WHY IS THIS NOT A THING?
Because functions are values. having two functions with the same name is like storing two functions in one variable, which isn't quite possible. (also lua wouldn't know what to call if you did

It actually IS possible believe it or not. You use the same solution as I suggested above to replace all function creations with a table of all functions of the same name.
The table has a metatable __call that when called checks the type of all arguments and selects the best function from the functions stored in the table.
You also need a metatable extending code so you can make the table appear to be a function etc etc
ElvishJerricco #82
Posted 21 September 2014 - 06:49 PM
sci4me said:
Functions with different parameters but the same name!!! Seriously, WHY IS THIS NOT A THING?
Because functions are values. having two functions with the same name is like storing two functions in one variable, which isn't quite possible. (also lua wouldn't know what to call if you did

It actually IS possible believe it or not. You use the same solution as I suggested above to replace all function creations with a table of all functions of the same name.
The table has a metatable __call that when called checks the type of all arguments and selects the best function from the functions stored in the table.
You also need a metatable extending code so you can make the table appear to be a function etc etc

This seems extremely inefficient. Calls are fast. Searching tables is slow. Other languages handle overloading by knowing at compile time what exactly needs to be called. Overloading in Lua just can't be done efficiently. I see no reason to let my code do so much work when I could just have a differently named function.
Agent Silence #83
Posted 22 September 2014 - 06:28 PM
Defining variables after a block as in

while true do
print(a)
end
a = "This variable was executed after the loop
Lyqyd #84
Posted 22 September 2014 - 07:44 PM
What benefit could that possibly give you? That would break… everything.