「Luaでディープコピーをする」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
imported>Administrator
(ページの作成:「Luaディープコピーがしたい。 再帰を使ったディープコピー。 <source lang="lua"> function deepcopy(orig) local orig_type = type(...」)
(相違点なし)

2018年1月30日 (火) 12:57時点における版

Luaディープコピーがしたい。

再帰を使ったディープコピー。

function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end

つーか、githubに再帰を使わないディープコピーが落ちていた。