lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




>a benefit, as there is literally not a single place in Lua where an
>assignment expression would save more than a single line of code.

One place where Lua has similar syntax to Pyhon is if-elseif statements. Assignment expressions let you avoid extra nesting here.

    if x := foo:match(p1) then
        -- A
    elseif x := foo:match(p2) then
        -- B
    elseif x := foo:match(p3) then
        -- C
    end

-- vs

    local x = foo:match(p1)
    if x then
        -- A
    else
        x = foo:match(p2)
        if x then
            -- B
        else
            x = foo:match(p3)
            if x then
                -- C
            end
        end
    end
end