On Sun, Apr 06, 2025 at 02:24:05PM +0100, 'Scott Morgan' via lua-l wrote:
> On 06/04/2025 02:32, 'Martin Eden' via lua-l wrote:
> > What do you guys would ask from language Santa? Another features,
> > some of these, nothing?
>
> 1) nil safe table indexing, so you can grab values that may or may not be
> present in nestled table structures:
>
> a = { b = { c = { d = 123 } } }
> print(a.b.c.d) -- > 123
> print(a.b.x.d) -- > Error!
> print(a?b?c?d) -- > 123
> print(a?b?x?d) -- > nil
>
> There's various ways around it currently, but they all have drawbacks.
Will second this and it may be even implemented with only some frontend
changes.
> 2) I really miss C's += operator, which could include table concatenation:
>
> x = 10
> x += 5 --> x == 15
>
> tab = { 1, 2 }
> tab += 3 --> { 1, 2, 3 }
>
> Thinking about it; if you allowed .. to work on a table:
>
> tab = { 1, 2 } .. 3 --> { 1, 2, 3 }
> tab = { 1, 2 } .. { 3, 4 } --> { 1, 2, 3, 4 }
>
> This would have the advantage of making ropes easier to implement, to the
> extent you could get away with a single rope_to_string function, rather than
> a whole class in a lot of cases.
But I don't think it's a good idea: this hides the fact that concating
two tables is probably expensive. And may come with some uneasy choices,
1. After the operation, what state the original table ("{1, 2}" and
"{3, 4}" in your case) should be in? Should it be modified in place,
or this operation creates a new object?
- Modifying inputs in place sounds a bad idea to me, this is often
misleading and causes bugs, not to mention no assignment may be
involved in the case.
- Creating new objects obviously brings a large overhead, just like
string concating. This will stops anyone who wants an efficient
concating to make use of the feature.
2. table concatng is much more complex than the string one. IMHO it's
hard to desugar and we may end up adding an extra instruction to our
VM.
3. What should the operation do on tables with holes? i.e. what's the
result of { [1] = 1, [3] = 3 } .. { [2] = 1 }? How should it behave
on dictionary-like tables ({ foo = 1 } .. { bar = 2 }) or mixed ones?
IMHO the only reason to get Lua implementing table "concating" in C is
performance. If it's really an issue, I'll propose to introduce
table.merge(),
1. This function modifies passed table in place, just like
table.insert().
2. Problem 2 doesn't exist anymore if we implement the functionality
through a library subrountine.
3. table.merge() appends the later table's numeric part to the former
one's. The hashtable (dictionary) parts are simply merged, and the
later one overrides the former one if there're duplicated entires.
Anyway, you could always implement the syntax with
local concatable = { __concat = function(a, b)
local r = {};
for i, v in ipairs(a) do
r[i] = v;
end
for i, v in ipairs(b) do
r[i + #a] = v;
end
return r;
end };
local function
makeConcatable(t)
return setmetatable(t, concatable);
end
local a, b = makeConcatable{ 1, 2 }, makeConcatable{ 3, 4 };
for _, v in ipairs(a .. b) do
print(v);
end
although I think it's really not a good idea.
> Then you could have ..= as simple syntax sugar for both strings and tables,
> leaving += (and the other operators, I suppose) just for sugar on numbers:
>
> str = "Hello, "
> str ..= "World!" -- > "Hello, World!"
>
>
> 3) Bring back table.foreach!!
>
> Yeah, it's easy to implement, but it's such a PITA when you're sitting at
> some random Lua instance and just want to quickly look into some data. Just
> put it back in the standard lib.
>
> Maybe an enhanced version that can traverse a whole tree, without getting
> stuck in loops.
>
>
> I can dream :)
>
Thanks,
Yao Zi