lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 14.07.2016 um 10:13 schröbte Egor Skriptunoff:
On Thu, Jul 14, 2016 at 11:01 AM, Daurnimator <quae@daurnimator.com> wrote:

Maybe, {1, 2, 3} for tables, @{1, 2, 3} for arrays (with array metatable
applied)?

In that case, why not use the already valid: S(1,2,3)

Three reasons:
1) Standardization of array type in Lua (all the developers will use the
same implementation of arrays)
2) Avoidance of performance penalty due to passing vararg to a function

What performance penalty? The times where Lua created an `arg` table for the vararg lists are over (which is funny, because the `arg` table is exactly what you'd want in this case) ...

And at least in bytecode table literals and function calls look surprisingly similar:

	2	[5]	NEWTABLE 	4 3 0
	3	[5]	MOVE     	5 0
	4	[5]	MOVE     	6 1
	5	[5]	MOVE     	7 2
	6	[5]	SETLIST  	4 3 1	; 1
	7	[5]	MOVE     	3 4

vs.

	8	[6]	GETTABUP 	4 0 -1	; _ENV "f"
	9	[6]	MOVE     	5 0
	10	[6]	MOVE     	6 1
	11	[6]	MOVE     	7 2
	12	[6]	CALL     	4 4 2
	13	[6]	MOVE     	3 4


3) Avoiding the limitation on number of function arguments

4) I can't really see Lua using one-letter functions. More idiomatic would be a `table.create` function (similar to `coroutine.create`) which is even longer than the `table.pack` we already have ... 5) It doesn't allow mixed arrays/tables, which you might want if you pass positional and keyword arguments to a function. Incidentally, that's where nil values in the array might come up as well ... `f(S( 1, 2, 3 ))` is just ugly. `f@{ 1, 2, 3 }` isn't that much better, though ...

Btw., the problem is really *empty* arrays. Lua could infer that `{ 1, 2, 3 }` is supposed to be an array and do its thing (whatever that may be), but `{}` would be ambiguous. So we only have to come up with syntax for an empty array like `{,}`, or my current favorite `{ n=0 }`. ;-)


Philipp