Make concatenate operator ".." work on tables

This shouldn’t happen because there’s literally no good default way to turn a table into a string. Concat is too canonical; it says “this is the correct format in which a table is converted to a string and any other format is wrong”.

I’d be all for a stringify-like function, because you’d at least be able to supply arguments to change how the result is formatted.

5 Likes

But do you see any issue in having both? If you just concatenate, it would pick a default format, and you can always serialize it in a different way by manually turning it into a string using that function (with different parameters).

i.e.

local t = {a = 1, b = 2, c = 3}

local s1 = "Table: " .. t
-- Some default format, say: 'Table: {a = 1, b = 2, c = 3}'

local s2 = "Table: " .. table.tostring(t, "json")
-- 'Table: {"a":1,"b":2,"c":3}'

(Function name and parameters are pseudo, just to give an idea)

1 Like

I only have an issue with concat, for the reasons already mentioned. A function is fine because it’s not built into the language.

Also note when I say “format”, I mean things like how keys are printed and how loops are handled and whether the result follows any particular syntax. There’s a million and a half ways to handle these.

3 Likes

The most useful solution would be to add a ‘table explorer’ feature to the output window.

So you get
blah blah: table: asdfg
from some print statement

Then you click the table ID to open some explorer window which lists all the key-value pairs (and you can then do this recursively if any keys/values are tables).

Then it would also work for tables bigger than a few values, and there wouldnt be confusion about how to “stringify” it because it never is (only the contained data is, like now).

1 Like

The default behavior of “…” would be a somewhat big thing to change. Not in terms of being a practical problem, but in deviating from standard Lua when other solutions are possible.

Why not use print’s ability to autmoatically tostring and concat things?

> print("game pad:", game:GetService("UserInputService").GamepadEnabled) game pad: false

> print("Bool:", true, " Num:", 9.4, " Nil:", nil, " Workspace:", workspace) Bool: true Num: 9.4 Nil: nil Workspace: Workspace


I think a way to represent tables as a string for debugging purposes would be very useful, and including it as a default only extends that use. Replacing standard behavior such as concat’s isn’t needed for that purpose, though.

Here’s the approach I would take:

  1. Add a method to turn a table into a string. Perhaps table.tostring, or maybe a different name that makes it clear that the format is arbitrary.
  2. Either modify the print method, or add a new one. This method would concat all arguments together (like print), tostring-ing non-tables and table.tostring-ing tables.

My reasons for this are:

  • Doesn’t replace standard Lua behavior.
  • This avoids controversy.
  • The standard behavior serves to make whatever a piece of code does very clear. Automatically tostring-ing when concatenating could arguably change that.
  • Does not affect existing code. I’m sure someone has written something that depends on tostring returning table: ########, even if one may consider it badly made to do such a thing.
  • Personally, I have used the standard Lua behavior to check if two tables in completely different sections of a program (such that I can’t easily use ==) are the same. If this new behavior replaced the old behavior then it would become more difficult, especially if the tables were copies of each other or if they held the same data.
  • A new method could be made specifically for debug messages. The functionality of this method could be extended later in some way without affecting existing code that just wants to print to the output and not have any further purpose. For example, the ‘table explorer’ functionality could be added to this new method.

As a last thing, I’d like to note that I personally feel concat should apply just to strings for the default types. I, personally, do not like that it works with numbers. If I want something to be a string I use tostring or I use a method that does tostring automatically, like print.

2 Likes

Best would be having table.tostring indeed.
Then you can just wrap tostring to use table.tostring for tables.
print/warn use tostring() to convert non-strings to strings, for cases like this.

1 Like

I think my opinion will be very simply that … could be confusing.

What if I did tab1 … tab2, I assume I get a string but a new player might think it joins the two tables into one.

2 Likes

Concatenating two tables isn’t a legal lua operation in the first place, so I don’t think that will be a big source of confusion.

Maybe we should just add C# support for Roblox and make our lives much better

13 Likes

Is __concat a metamethod? would be useful here.

__tostring is.

Both are.
Not sure __concat works for concatting string…table though, I forgot the rules.

Well thought out.

If implemented as you suggest, the one additional change I would also seek to make “x … y” throw an intellisense warning when y is a table, because it is never what the programmer actually wants.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.