4 ms·
A lot of those issues relate to the fact lua is dynamic and typeless. It can for sure be a pita when you have a simple typo in your variable and you don't find
by rstupek 6y ago
A lot of those issues relate to the fact lua is dynamic and typeless. It can for sure be a pita when you have a simple typo in your variable and you don't find out until you run the app and hit that code path.
- TulliusCicero 6y agoYes, I agree. Though I don't remember python behaving this way, exactly, in terms of ignoring attempted use of non-existent variables. > you don't find out until you run the app and hit that code path. The real problem was that we didn't -- or rather, he didn't -- even find out then, since it just looked like the variable was somehow holding nil, instead of the variable not actually existing. It looked like there was some other reason for the variable accidentally being nil -- and when you're a total beginner, it could be nearly anything. This setup is just...it's plainly more beginner-hostile than a compiler/runtime simply telling the coder that there's a problem, what the problem is, and what line it's at. So I don't understand the people talking up Lua like it's great for beginners. But yeah, I definitely dislike dynamically typed languages in general. Having static typing with type inference ala Kotlin just seems to be much better all around.
- dividuum 6y agoSomething like function no_globals() setmetatable(_G, { __newindex = function(t, k, v) return error("attempt to set global '"..tostring(k)) end, __index = function(t, k) return error("attempt to reference missing global '"..tostring(k)) end, }) end can help catch assignment and usage of undefined (e.g. by not using 'local') variables. The downside is that this then also applies to function definitions. So be prepared to write 'local function xyz()'.
- TulliusCicero 6y agoPeople keep pointing out workarounds to me. Thanks, but that's not really what I'm getting at. My complaint is that, if the language is supposed to be good for beginners, the workarounds should be unnecessary, since beginners are unlikely to a) be able to find proper workarounds, or b) even be aware that there's a problem that a workaround would be needed to solve. My son has no idea that invoking an un-initialized global variable immediately brings it into being with 'nil' is weird behavior for a language. If a less experienced programmer was the one helping him learn, they might not realize it either.
- samatman 6y agoAs I said in a sibling comment, Lua isn't designed to be good for beginners, it's simple rather than easy. But I want to stand up for this, in particular. Because in most languages, environments are magic: they're an exceptional thing that you work with in a special way, you're not expected to understand them, just use them. In Lua, an environment is... just a table! You can set an environment, read from it directly, assign it a metatable, and so on. So this is great for a beginner, once he gets past the hurdle. Just like `table.field` returns `nil` if there's no field field, `field` itself returns nil if there's no `_G.field`. Because _G.field and field itself are the same thing.
- TulliusCicero 6y ago> So this is great for a beginner, once he gets past the hurdle. Is it? Because that still seems very error prone to me. You're not gonna suddenly stop making typos just because you understand that there's an invisible global table around all the time. But hey, maybe it's super awesome later on, though I'm having trouble imagining scenarios where this behavior is all that useful over what most other languages do.
- djur 6y agoIt's less that Lua is great for beginners than that it's simple to get started with in the environments where it's embedded. It was originally a configuration language, and in a lot of ways it's still more optimized for "open up a text file and plug in some different values for the running program" than it is for implementing actual logic. (That's what I meant by "non-programmers" in the other comment.) And static typing is hard to make work when your codebase is made up of a lot of independent snippets of code which run in an environment completely determined by an outside program. You'd need something akin to TypeScript's external type definition files.
- TulliusCicero 6y agoOkay yeah, that's fair. It just seems like the advantage of "is easier to build larger programs in a coherent way" is more useful than "is easy for non-programmers to get started right at the beginning" for a lot of the contexts it's used in.