Jump to content




Lua rants, because we all love it!


30 replies to this topic

#1 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 11 January 2015 - 03:18 PM

So yeah, I've been doing some metatables lately and felt like ranting a bit.

__index: What the actual f, lua devs? A bloody function which name indicates that it is run every time a field in table is indexed, but no no no! That would be too easy to use, wouldn't it? So instead of doing a normal index handler let's do one that is called whenever indexed field is nonexistent! Great idea! So instead of doing

local t = {
  print = function(self) print(self.text) end,
  text = "quix"
}
local mt = {
  __index = function(t , k)
	if type(rawget(t, k)) == "function" then
	  return function(...) rawget(t, k)(t, ...) end
	end
	return rawget(t, k)
  end
}
setmetatable(t, mt)

t.print()

You have to do

local t = {
  print = function(self) print(self.text) end,
  text = "quix"
}

local _t = t
t = {}

local mt = {
  __index = function(t, k)
	if type(_t[k]) == "function" then
	  return function(...)
		_t[k](_foo, ...)
	  end
	end
	return _t[k]
  end
})

setmetatable(t, mt)

t.print()

Because it looks so much better, RIGHT!?

local: Oh yes, local variables. This keyword shouldn't exist. Because there should be no need for it's existence! Every goddamn variable should be local by default! And if You need global variable (what happens never very rarely with proper design) you cloud use for example a "global" keyword, uh?


So that was it for now, please use this general stress relief thread to your pleasure!

Edited by lucy-san, 11 January 2015 - 03:27 PM.


#2 Lignum

  • Members
  • 558 posts

Posted 11 January 2015 - 03:47 PM

String concatenation. When I first used Lua, I tried to do this:
"Hello " + "World"

But no, that's too complex for such a lightweight language. We have to do this:
"Hello " .. "World"


#3 Agoldfish

  • Members
  • 451 posts
  • LocationSome Fish Bowl in Ohio.

Posted 11 January 2015 - 04:14 PM

Posted Image

#4 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 11 January 2015 - 05:39 PM

Ok, ok... When I started with Lua I hated it. I've come from a C# & Python background so (In my opinion) have used the two best 'learning' languages. Python for its simplicity and flexibility, C# just being awesome (I'm writing Java at the moment and it makes me realise how much the .Net team got right).

However, I've come to almost love Lua, I'll go though these points one by one:

View Postlucy-san, on 11 January 2015 - 03:18 PM, said:

So yeah, I've been doing some metatables lately and felt like ranting a bit.

__index: What the actual f, lua devs? A bloody function which name indicates that it is run every time a field in table is indexed, but no no no! That would be too easy to use, wouldn't it? So instead of doing a normal index handler let's do one that is called whenever indexed field is nonexistent! Great idea! So instead of doing

Well, the trouble with Lua tables is that they aren't designed for how this type of (ab)use. __index is wonderful. Most of the time I use it to inherit environments (setmetatable({}, {__index = getfenv()})) or classes. I almost never use it to track access to a key.

Almost every other language uses a similar principal. Python has __getattr__, PHP has __get, I'm sure many other languages have a similar thing.

View Postlucy-san, on 11 January 2015 - 03:18 PM, said:

local: Oh yes, local variables. This keyword shouldn't exist. Because there should be no need for it's existence! Every goddamn variable should be local by default! And if You need global variable (what happens never very rarely with proper design) you cloud use for example a "global" keyword, uh?

Yep, agree with that. Sometimes it is useful because you can see when you define variables, however the first thing I check with any program is if it uses locals or not.

View PostLignum, on 11 January 2015 - 03:47 PM, said:

String concatenation. When I first used Lua, I tried to do this:
"Hello " + "World"

But no, that's too complex for such a lightweight language. We have to do this:
"Hello " .. "World"

In any normal language I would agree, but some languages (Lua, VB, I'm sure there are others - maybe ActionScript and PHP) have type conversion things. This is the trouble:

"1" + "1" -- 2

But I wanted 11! These are strings, but we get a number out of it? Explicitly defining addition or concatenation is really the only solution.

Anyway, it isn't as bad as some languages:
Spoiler

I'm going to suggest reading these two articles by the co-creator of StackOverflow. The general point is 'Language design is hard'. Despite Lua's many odd things, I do quite like it now.

Anyway! It is more fun writing massively complicated programs in a slightly obscure language then it is in one that actually works!

Sorry for the long post.

Edited by SquidDev, 11 January 2015 - 05:40 PM.


#5 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 11 January 2015 - 05:47 PM

View PostSquidDev, on 11 January 2015 - 05:39 PM, said:

In any normal language I would agree, but some languages (Lua, VB, I'm sure there are others - maybe ActionScript and PHP) have type conversion things. This is the trouble:

"1" + "1" -- 2

But I wanted 11! These are strings, but we get a number out of it? Explicitly defining addition or concatenation is really the only solution.
Well, unless javascript it shouldn't be a problem ;)
>only solution
Uhmm, for example: don't store ints as strings? :D

View PostSquidDev, on 11 January 2015 - 05:39 PM, said:

Anyway, it isn't as bad as some languages:
Spoiler
It would be a rather hard thing to be worse than a fractal of bad design :D
We are actually going to cast some of these fine sidehammers from aluminum in local Hackerspace :D

View PostSquidDev, on 11 January 2015 - 05:39 PM, said:

Sorry for the long post.
Why would You apologize for long post? Please post!

Edited by lucy-san, 11 January 2015 - 05:48 PM.


#6 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 11 January 2015 - 06:03 PM

View Postlucy-san, on 11 January 2015 - 05:47 PM, said:

Well, unless javascript it shouldn't be a problem ;)
>only solution
Uhmm, for example: don't store ints as strings? :D

This would be fine, but what if you are reading user input? Or sub-strings? Javascript just forces it to be a string, which I guess is the better solution, but then you need to call tonumber. *SIGH*.

View Postlucy-san, on 11 January 2015 - 05:47 PM, said:

It would be a rather hard thing to be worse than a fractal of bad design :D
We are actually going to cast some of these fine sidehammers from aluminum in local Hackerspace :D
What about VBA/VB6, Fortran, Applescript, HTML and Milton Keynes? Laravel makes PHP nice to write.

The thing is Lua works. People have written Web browsers, encryption software, and Java in it. It is pretty easy to learn. From Dan200's point of view, there really isn't another embeddable language for ComputerCraft. I know the language I would end up designing would look like a C#/D mutant mess. I can't complain really!

I would suggest looking at ElvishJerricco's LuaLua. He has managed to get Yueliang working in CC. See if you can use it to make fix concatenation and locals!

Edited by SquidDev, 11 January 2015 - 06:33 PM.


#7 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 11 January 2015 - 06:58 PM

>This would be fine, but what if you are reading user input? Or sub-strings?
It should stay as string, since it is string. If you will need an int, you will convert type. Calling int, tonumber or whatever else is the way to go. Excatly because "1"+"1"==2.

>The thing is Lua works.
PHP works as well.
AFAIK lua was never meant to be a professional language, but a simple language for modding, etc. It gets job done, but it could get job done in nicer way. No language is perfect and lua is not bad, neither is excellent rough.

>Dan200's point of view, there really isn't another embeddable language for ComputerCraft
He could use Jython, but ain't complaining - we could be running BASIC or JavaScript.

> See if you can use it to make fix concatenation and locals!
Maybe, but it would be pointless - it's like lua compiled to JavaScript, but worse. Running lua in lua in Java VM will not be an efficient solution ;)

#8 Agent Silence

  • Members
  • 319 posts
  • Location[string "FindMe"]:23143: bad argument #1 to 'returnPos' (vector expected, got nil)

Posted 11 January 2015 - 07:26 PM

View Postlucy-san, on 11 January 2015 - 06:58 PM, said:


>The thing is Lua works.
PHP works as well.
AFAIK lua was never meant to be a professional language, but a simple language for modding, etc. It gets job done, but it could get job done in nicer way. No language is perfect and lua is not bad, neither is excellent rough.

There are occasions when you have a game/engine that uses Lua. For example, World of Warcraft and Crysis1/2/3(Uses Lua along with C++) both use Lua.

#9 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 11 January 2015 - 10:40 PM

Removing "local" altogether would completely break the language, as explained here. How would you prevent new variables in functions and loops and so on from accessing upvalues of the same name? You couldn't do it - you'd be forced to make sure every new variable in your script had a unique title. It'd be a royal mess.

#10 ElvishJerricco

  • Members
  • 803 posts

Posted 11 January 2015 - 11:43 PM

I hate that Lua 5.2 removed setfenv and uses that stupid _tEnv thing. There's so much brilliant hackery that you can do with setfenv it's wonderful.

I despise the wordiness of the language. Do / then end is awful. I just want brackets...

This is more a problem with scripting languages than Lua itself: I wish I could just compile my code so that I'm not using unsafe globals and stuff all the time. Access is important, and it lets the compiler / VM do extra optimization. Indexing globals by string names, for example, is slow.

Related to that point: Using multiple files in CC Lua is a nightmare. You have to use os.loadAPI, which means you can't have a .lua extension unless you override loadAPI manually. It also means your code is now in the global scope because why not? The other option is manually loading files but that's tedious. If we could just have a function similar to require...

Problem with CC rather than Lua: I want the debug library dammit! I know it's not exactly safe to enable by default, but it needs to be a config option.

#11 ElvishJerricco

  • Members
  • 803 posts

Posted 11 January 2015 - 11:56 PM

View PostSquidDev, on 11 January 2015 - 06:03 PM, said:

I would suggest looking at ElvishJerricco's LuaLua. He has managed to get Yueliang working in CC. See if you can use it to make fix concatenation and locals!

Unfortunately there's no fixing concatenation with LuaLua. The + operator must emit an add instruction. And it's the VM that determines how an add instruction works with string types. The reason I say it can't emit a concat instruction is this:

function concat(s1, s2)
    return s1 + s2
end

Lua has no way of knowing if s1 and s2 are strings or numbers or whatever. So the only option is to emit an add instruction, which the VM doesn't handle with strings like it should.

View Postlucy-san, on 11 January 2015 - 06:58 PM, said:

Maybe, but it would be pointless - it's like lua compiled to JavaScript, but worse. Running lua in lua in Java VM will not be an efficient solution ;)

I should point out that LuaLua is not a Lua VM. It's a Lua compiler. It just adds syntax to Lua and runs the resulting bytecode through ordinary means. There's no in-between except the compilation step, which is tiny.

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 12 January 2015 - 07:48 AM

Nobody forces you to use os.loadAPI. I have lately been writing configuration files as valid lua, then doing something like what os.loadAPI does (set up its env table, run the file, then use the resulting filled env table) to load them up and make use of them. It's quite handy and doesn't pollute the global table.

#13 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 12 January 2015 - 07:53 AM

View PostBomb Bloke, on 11 January 2015 - 10:40 PM, said:

Removing "local" altogether would completely break the language, as explained here. How would you prevent new variables in functions and loops and so on from accessing upvalues of the same name? You couldn't do it - you'd be forced to make sure every new variable in your script had a unique title. It'd be a royal mess.

View Postlucy-san, on 11 January 2015 - 03:18 PM, said:

Every goddamn variable should be local by default



View PostElvishJerricco, on 11 January 2015 - 11:43 PM, said:

I just want brackets...
+2


View PostElvishJerricco, on 11 January 2015 - 11:43 PM, said:

Related to that point: Using multiple files in CC Lua is a nightmare. You have to use os.loadAPI, which means you can't have a .lua extension unless you override loadAPI manually. It also means your code is now in the global scope because why not? The other option is manually loading files but that's tedious. If we could just have a function similar to require...
Just the execute file, or construct it that way, that it will return a "class" containing all functions.


View PostElvishJerricco, on 11 January 2015 - 11:56 PM, said:

Unfortunately there's no fixing concatenation with LuaLua. The + operator must emit an add instruction. And it's the VM that determines how an add instruction works with string types. The reason I say it can't emit a concat instruction is this:

function concat(s1, s2)
  if type(s1) == "string" or type(s2) == "string" then --# Or whatever type returns
	return tostring(s1) .. tostring(s2)
  else
	return s1 + s2
  end
end


View PostElvishJerricco, on 11 January 2015 - 11:56 PM, said:

I should point out that LuaLua is not a Lua VM. It's a Lua compiler. It just adds syntax to Lua and runs the resulting bytecode through ordinary means. There's no in-between except the compilation step, which is tiny.
Oh, this changes a lot then ;)

Edited by lucy-san, 12 January 2015 - 07:54 AM.


#14 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 12 January 2015 - 08:00 AM

View Postlucy-san, on 12 January 2015 - 07:53 AM, said:

View PostBomb Bloke, on 11 January 2015 - 10:40 PM, said:

Removing "local" altogether would completely break the language, as explained here. How would you prevent new variables in functions and loops and so on from accessing upvalues of the same name? You couldn't do it - you'd be forced to make sure every new variable in your script had a unique title. It'd be a royal mess.

View Postlucy-san, on 11 January 2015 - 03:18 PM, said:

Every goddamn variable should be local by default

Yes, I saw that, and I'm saying that wouldn't work. How would you declare a new local with the same name as an old one? If it's done automatically the first time you refer to a variable in a function, then how would you refer to upvalues?

View Postlucy-san, on 12 January 2015 - 07:53 AM, said:

function concat(s1, s2)
  if type(s1) == "string" or type(s2) == "string" then --# Or whatever type returns
	return tostring(s1) .. tostring(s2)
  else
	return s1 + s2
  end
end

Something along these lines could be applied, but the overhead... You'd need to add it to the bytecode around every single + in each script. Ouch.

Edited by Bomb Bloke, 12 January 2015 - 08:05 AM.


#15 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 12 January 2015 - 08:30 AM

View PostBomb Bloke, on 12 January 2015 - 08:00 AM, said:

View Postlucy-san, on 12 January 2015 - 07:53 AM, said:

View PostBomb Bloke, on 11 January 2015 - 10:40 PM, said:

Removing "local" altogether would completely break the language, as explained here. How would you prevent new variables in functions and loops and so on from accessing upvalues of the same name? You couldn't do it - you'd be forced to make sure every new variable in your script had a unique title. It'd be a royal mess.

View Postlucy-san, on 11 January 2015 - 03:18 PM, said:

Every goddamn variable should be local by default

Yes, I saw that, and I'm saying that wouldn't work. How would you declare a new local with the same name as an old one? If it's done automatically the first time you refer to a variable in a function, then how would you refer to upvalues?

Same way You do now?

#16 ElvishJerricco

  • Members
  • 803 posts

Posted 12 January 2015 - 08:55 AM

View Postlucy-san, on 12 January 2015 - 07:53 AM, said:

View PostElvishJerricco, on 11 January 2015 - 11:56 PM, said:

Unfortunately there's no fixing concatenation with LuaLua. The + operator must emit an add instruction. And it's the VM that determines how an add instruction works with string types. The reason I say it can't emit a concat instruction is this:

function concat(s1, s2)
  if type(s1) == "string" or type(s2) == "string" then --# Or whatever type returns
	return tostring(s1) .. tostring(s2)
  else
	return s1 + s2
  end
end

My point was more that the + operator can't be changed in the compiler to concatenate strings. I'm aware that Lua code can be written to handle doing either concatenation or addition. It's just that the + operator must equate to an add instruction.

View PostLyqyd, on 12 January 2015 - 07:48 AM, said:

Nobody forces you to use os.loadAPI. I have lately been writing configuration files as valid lua, then doing something like what os.loadAPI does (set up its env table, run the file, then use the resulting filled env table) to load them up and make use of them. It's quite handy and doesn't pollute the global table.

Well what I mean to say is that there is no decent way to work in multiple files without writing your own code loading system. You can, and it's not too bad but, but I think a better way should just be included in CraftOS

Edited by ElvishJerricco, 12 January 2015 - 08:55 AM.


#17 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 12 January 2015 - 02:33 PM

View PostElvishJerricco, on 12 January 2015 - 08:55 AM, said:

My point was more that the + operator can't be changed in the compiler to concatenate strings. I'm aware that Lua code can be written to handle doing either concatenation or addition. It's just that the + operator must equate to an add instruction.
By compiler You mean LuaLua? I haven't read the code, but I'm pretty sure that this can be accomplished in one way or another :)

View PostElvishJerricco, on 12 January 2015 - 08:55 AM, said:

View PostLyqyd, on 12 January 2015 - 07:48 AM, said:

Nobody forces you to use os.loadAPI. I have lately been writing configuration files as valid lua, then doing something like what os.loadAPI does (set up its env table, run the file, then use the resulting filled env table) to load them up and make use of them. It's quite handy and doesn't pollute the global table.

Well what I mean to say is that there is no decent way to work in multiple files without writing your own code loading system. You can, and it's not too bad but, but I think a better way should just be included in CraftOS

Yes, it sucks big.

#18 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 12 January 2015 - 03:23 PM

Well, from my point of view Lua's 'require' is basically 'dofile' but with a little more functionality for managing libraries. 'dofile' works quite well instead of 'require'.

What I don't like about CC Lua is the lack of not just the debug library but all Lua's libraries. Literally, why CC doesn't have all the default Lua's libraries. In fact almost every library can be recreated in CC Lua so I do not see why, for example, the io library couldn't be completely added and functional in CC. And the package library with 'require'. And the debug library (allowed by config option). Why Dan? Why??

#19 thedobbles

  • Members
  • 5 posts

Posted 12 January 2015 - 06:29 PM

View PostRequiem of Silence, on 11 January 2015 - 07:26 PM, said:

View Postlucy-san, on 11 January 2015 - 06:58 PM, said:

>The thing is Lua works.
PHP works as well.
AFAIK lua was never meant to be a professional language, but a simple language for modding, etc. It gets job done, but it could get job done in nicer way. No language is perfect and lua is not bad, neither is excellent rough.

There are occasions when you have a game/engine that uses Lua. For example, World of Warcraft and Crysis1/2/3(Uses Lua along with C++) both use Lua.

As well as G-Mod and Roblox!

#20 lucy-san

  • Members
  • 23 posts
  • LocationPolen

Posted 12 January 2015 - 07:34 PM

View PostMKlegoman357, on 12 January 2015 - 03:23 PM, said:

Well, from my point of view Lua's 'require' is basically 'dofile' but with a little more functionality for managing libraries. 'dofile' works quite well instead of 'require'.

What I don't like about CC Lua is the lack of not just the debug library but all Lua's libraries. Literally, why CC doesn't have all the default Lua's libraries. In fact almost every library can be recreated in CC Lua so I do not see why, for example, the io library couldn't be completely added and functional in CC. And the package library with 'require'. And the debug library (allowed by config option). Why Dan? Why??

Probably somewhere "because f you" and fact, that lua libraries are usually written in C/Cop. Is there debug library in luaj?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users