Jump to content




Lua: What would you change?


83 replies to this topic

#41 MCGamer20000

  • Members
  • 49 posts

Posted 07 April 2014 - 02:34 AM

I'm trying to not be rude, but here are some solutions.
Spoiler


#42 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

Posted 07 April 2014 - 03:13 AM

View Postoeed, on 03 December 2013 - 03:49 AM, said:

View Postdistantcam, on 03 December 2013 - 03:20 AM, said:

View Postoeed, on 03 December 2013 - 03:01 AM, said:

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.

#43 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 April 2014 - 03:38 AM

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

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.

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

Quote

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

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

Quote

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

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

Quote

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'

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

Quote

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.

View Postbunny365, on 07 April 2014 - 02:34 AM, said:

Quote

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 by theoriginalbit, 07 April 2014 - 03:46 AM.


#44 MCGamer20000

  • Members
  • 49 posts

Posted 07 April 2014 - 04:45 AM

Well, I'm still (kinda) a Lua noob... But I did have fun trying to create code for each thing you said.

#45 Graypup

  • Members
  • 90 posts

Posted 07 April 2014 - 05: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"


#46 6677

  • Members
  • 197 posts
  • LocationCambridgeshire, England

Posted 07 April 2014 - 06: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.

#47 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 April 2014 - 01:32 AM

View PostGraypup, on 07 April 2014 - 05:33 PM, said:

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 by theoriginalbit, 08 April 2014 - 01:36 AM.


#48 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 08 April 2014 - 04: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.

#49 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 April 2014 - 04:19 AM

View PostLyqyd, on 08 April 2014 - 04:15 AM, said:

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

View Posttheoriginalbit, on 02 December 2013 - 09:30 PM, said:

2. data type checking function foo(string bar) why: mainly 'cause of the next point;
3. function overloading requires above;


#50 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 08 April 2014 - 04:46 AM

View Posttheoriginalbit, on 08 April 2014 - 04:19 AM, said:

From the OP

View Posttheoriginalbit, on 02 December 2013 - 09:30 PM, said:

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

#51 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 April 2014 - 06:47 AM

View PostLyqyd, on 08 April 2014 - 04:46 AM, said:

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

#52 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 April 2014 - 12:24 PM

View PostLyqyd, on 08 April 2014 - 04:46 AM, said:

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 by theoriginalbit, 08 April 2014 - 12:24 PM.


#53 LoneRangerGuy

  • Members
  • 11 posts
  • LocationCalifornia near the 101

Posted 11 April 2014 - 09: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.

#54 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 April 2014 - 01:06 AM

View PostLoneRangerGuy, on 11 April 2014 - 09:34 PM, said:

-snip-
the best I've really found is the reindenter in Sublime Text. Made a little better by the ComputerCraft plugin GravityScore made.

#55 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 12 April 2014 - 01: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.

#56 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 12 April 2014 - 11:43 PM

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 by Kingdaro, 12 April 2014 - 11:43 PM.


#57 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 13 April 2014 - 02:10 AM

View PostKingdaro, on 12 April 2014 - 11:43 PM, said:

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.

#58 Shazz

  • Members
  • 175 posts

Posted 16 April 2014 - 09:25 PM

View PostJiloacom, on 12 April 2014 - 01:39 AM, said:

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


#59 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 16 April 2014 - 09:52 PM

View PostShazz, on 16 April 2014 - 09:25 PM, said:

View PostJiloacom, on 12 April 2014 - 01:39 AM, said:

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

View Posttheoriginalbit, on 07 April 2014 - 03:38 AM, said:

of course there are always solutions, my point of this was to have it NATIVE not require us to implement us something.


#60 ElvishJerricco

  • Members
  • 803 posts

Posted 16 April 2014 - 10: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users