String.split unexpected behavior when using `variable.split`

As with many other string methods such as string.upper, you can use split in this manner:

local x = "hello doge fans"
table.foreach(x:split(" "), print)
-- 1 hello
-- 2 doge
-- 3 fans

However, if you were to make a typo on this line and instead did x.split(" "), you would instead get the unexpected:

local x = "hello doge fans"
table.foreach(x.split(" "), print)
-- 1

This is unlike other string methods, which expectedly provide a helpful error:

> print(("hello").upper())
07:51:36.279 - print(("hello").upper()):1: bad argument #1 to 'upper' (string expected, got no value)
07:51:36.280 - Stack Begin
07:51:36.280 - Script 'print(("hello").upper())', Line 1
07:51:36.280 - Stack End

CC @Tiffblocks

1 Like

Not sure we can do anything here.

This is essentially calling string.split(" ") which is a valid function call because the separator argument for the split function has a default value.

5 Likes

That makes sense, a strange issue.

1 Like

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