4 ms·
I gave you an upvote because I think you don't deserve to go negative just for raising some of these concerns. Still, I'm going to examine some of them. With r
by striking 6y ago
I gave you an upvote because I think you don't deserve to go negative just for raising some of these concerns. Still, I'm going to examine some of them.
With regards to variables, the best way to hack around this issue is to 1) always use `local` when declaring a variable and 2) set `_G`'s metatable to raise an error if you try to create a new key, which prevents accidentally using globals like you've inadvertently discovered is the default behavior. This is this way because it is I guess simpler than teaching someone how declarations work, but it obviously leads to a pretty immediately bad situation for anyone who wants to do more programming than setting a global constant. It harkens back from when Lua was basically a TCL-like.
Copying a table is generally unnecessary, if you never mutate the table to be cloned. Just set the __index metatable property of a new table to a reference to the table to be cloned. Ta-da, you have inherited the previous table. Not giving you an immediate, easy way to do this forces you to come to terms with copying not always being the right solution. It's not great, and the language offers no pointers to those who want to get a thing done, but it isn't the worst choice.
Not providing increment operators is a design choice, one that simplifies the metatabling for operators. Only having one case for addition, and not having to worry about increment / incremental assignment, means people creating metatables that override operators have a good experience. This is one of those times where power users are given the advantage at the cost of the less experienced, and I share your implied concern in feeling the inconsistencies in who is favored by which language features.
- TulliusCicero 6y ago> With regards to variables, the best way to hack around this issue is to 1) always use `local` when declaring a variable and 2) set the `_G` metatable to never allow the creation of keys Keep in mind, I'm talking about beginners here. Beginner kids, even (it's a popular language for Roblox scripting!). Are either of those things, things that will just come naturally to newbies? Are they definitely gonna know about them when they pick up the language in random text or video tutorials? Are they going to remember to use local as often as they can, and will they understand when it's appropriate to use a global? The answer to all of these is no, obviously. For someone like me with a CS degree and several years of professional experience, your suggestions are perfectly reasonable, but in the wide, wacky world of beginners hacking things together, the right way should be easy and obvious. So obvious, even a beginner will catch on. > Copying a table is generally unnecessary, if you never mutate the table to be cloned. Just set the __index property of a new table to a reference to the table to be cloned. Ta-da, you have inherited the previous table. How is this more intuitive than just having a copy or clone function? You can find plenty of cloning functions on stackoverflow or wherever that people have written. Yes, there are workarounds, but why do you need a workaround for a pretty basic use case here? In python, you can just go "dict2 = dict(dict1)", or "dict2 = dict1.copy()", both of those are easy enough, and more importantly, they're intuitive: want a copy? Call the copy function! Why does Lua have to make it weird? > This is one of those times where power users are given the advantage at the cost of the less experienced, and I share your implied concern in feeling the inconsistencies in who is favored by which language features. Yes, it seems like the language design is a strange mix of "really easy to get started for beginners" and "footguns that you probably need to be experienced to avoid".
- djur 6y ago> Are either of those things, things that will just come naturally to newbies? Are they definitely gonna know about them when they pick up the language in random text or video tutorials? I think the argument is more that Roblox should be setting up its Lua environment that way. > In python, you can just go "dict2 = dict(dict1)", or "dict2 = dict1.copy()", both of those are easy enough, and more importantly, they're intuitive: want a copy? Call the copy function! A shallow copy, which can be very surprising. And since Lua has metatables and userdata, it's impossible to write a deep copy that works correctly in all circumstances.
- viraptor 6y agoI'm not super experienced with Lua - what's missing for a generic deep copy? Is there no real reflection system?
- djur 6y agoLua has a type of value called "userdata" that is just an opaque reference to a C resource. There's no way for Lua to know how do a deep copy of that resource, if it's possible at all. Tables can also have "metatables" that modify how they function. It's not obvious whether a deep copy should also create a deep copy of the metatable (and its metatable, and so on) or whether the copy should share the original metatable. There's good arguments either way.
- striking 6y agoIt's worth noting, I'm mostly not defending Lua here. I enjoyed my time with it, and then when I put it down I realized how unhappy I was with it. There's no language I really truly love, but the one I find myself recommending as a result of good tradeoffs between simplicity and usability is Python.
- djur 6y agoNot sure about that third point. Ruby desugars += and similar compound operators into separate operate and assign steps. There's no need for an implementation of += in addition to +. I'm sure Lua could do the same thing. My guess for why Lua doesn't provide a table copy function is because then they'd have to decide whether to provide shallow or deep copies, and deep copies have to do cycle detection and other such stuff. Then there's metatables, functions, and so on.
- striking 6y agoI think Lua accounted for the idea of desugaring, where PUC-Rio said that they didn't want the added complexity, but I don't think I could readily find that justification. This is just another one of those situations where Lua is not catering to anyone by randomly catering to one group or another. No one is happy.