Is there a way to merge them?

Is there a way to merge them?

local A = {}
local B = {}

function A.New()
	
end

function B.New1()
	
end

i’m sure it’s not hard at all but i don’t have idea how it’s works

if you want to merge 2 dictionaries, such as theese, you can loop trough one and overwrite the values of the other

local A = {}
local B = {}

function A.New()
	
end

function B.New1()
	
end

for index, value in B do
A[index] = value
end

In this case we overwrite table A
So if you have the same key in table B, that value will overwrite the other one

altough in a case where you dont want any overwriting and are using arrays instead, then you’d use table.insert instead

for _, value in B do
table.insert(A, value)
end

altough, this will not work for your case

I did this, but this method takes a lot of time. I use a lot of functions in my method and I want something that combines them in one line, for example using setmetatable.

Well, you only have to do it once. If you want to use metatable’s then i suppose you can do it like this:

local B = {B = "table b"}

local A = {A = "table a"}
A = setmetatable(A, {__index = B})

print(A)
print(A.A)
print(A.B)
2 Likes

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