How does 'something{}' work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Know how does
local var = something{}

work(im assuming the whole code is)

local something = require(someModule)

local var = something{}

since i seen this piece of code on this post

local seasons = customenum { "Spring", "Summer", "Autumn", "Winter" };
print(seasons.Spring);
print(seasons.Spring.ToInt32());

but it doesnt seems to make sense, i think its this local function new(self, tbl, ...) in the source code at line 214 that make it work

something{} here is just a function that takes a table as a parameter, its the same as doing something({})

something {} is actually a way to pass a parameter to a function.

local function something(t: {}) -- takes one value, which is a table
	print(t[1])
end

something {1} -- prints "1"
something({1}) -- This is what the above line is equivalent to.

It can’t be used with multiple arguments though.

local function something(t: {}, t2: {}) -- takes 2 values this time
	print(t[1])
	print(t2[1])
end

something {1} -- Would print 1 then error (as t2 isn't passed)
something {1}, {2} -- Assuming above line isn't there, it'll error saying Expected identifier when parsing expression, got ','
something({1}, {2}) -- Assuming the above lines aren't there, this will print 1 and 2 correctly.

This also works with strings!

local function something(str: string)
	print(str)
end

something "Hello, World!" -- Hello, World!
something("Hello, World!") -- Hello, World!

Only works with 1 argument, no difference.


Personally I think this just makes the code look worse with strings, but better with tables. It’s a preference after all though. Saying it again, it does come with the limitation of being able to pass one argument only.

2 Likes

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