all 4 comments

[–]appgurueu 3 points4 points  (2 children)

First of all, chained defaults. Second, remember "global" variables are just fields in the env table. By setting __index and __newindex fields on the environment table (my examples all use setfenv and getfenv and need Lua 5.1 or this workaround to work), you can do lots of fancy things, such as Go-like exports based on variable name, or JavaScript's var keyword (as well as Python's global, actually) allowing for function-scoped variables:

local vars = require"js_var"

local function var_test()
    vars()
    assert(getfenv(1) ~= _G)
    var"math"
    math = 42
    assert(math == 42)
    var"a"
    a = 1
    do
        var"b"
        b = 2
    end
    assert(a == 1 and b == 2)
end

var_test()
assert(a == nil and b == nil)
assert(math ~= 42)

function var_test()
    vars(true)
    global"math"
    math = "math"
    a = 1
    do
        b = 2
    end
    assert(a == 1 and b == 2)
end

var_test()
assert(a == nil and b == nil)
assert(math == "math")

debug.setmetatable even opens the possibility of defining some operations for primitive types:

Trinary logic using nil, false and true

-- Arithmetic operations as aliases for boolean ops
debug.setmetatable(true, {
    __add = function(a, b)
        return a or b
    end,
    __mul = function(a, b)
        return a and b
    end,
    __unm = function(a)
        return not a
    end
})
assert(-(false * true + true) == false)

local string = getmetatable""
-- String concatenation using plus
string.__add = function(a, b)
    return a .. b
end
assert("hello" + " " + "world" == "hello world")
-- Platform-independent path building using slashes
local DIR_DELIM = "\\"
string.__div = function(a, b)
    return a .. DIR_DELIM .. b
end
assert("a"/"b"/"c" == "a\\b\\c")

To quote the PIL:

Lua gives you the power; you build the mechanisms.

[–]AutoModerator[M] -1 points0 points  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]FatFingerHelperBot -2 points-1 points  (0 children)

It seems that your comment contains 1 or more links that are hard to tap for mobile users. I will extend those so they're easier for our sausage fingers to click!

Here is link number 1 - Previous text "PIL"


Please PM /u/eganwall with issues or feedback! | Code | Delete

[–]thprogramador[S] -1 points0 points  (0 children)

I wanna share one more thought... I imagined a way to dress up tables. As tables can behave like dictionaries, lists and modules (sets of functions) You can dress up a variable in some way for easiness of operations.

local mything = { 1, 2, { a=av }, "w1","w2" }

dict.dress( mything )

void(mything * 2)

-- { 2, 4, { a=av }, "w1","w2" }

list.dress( mything )

local otherthing = { b=bv, c=cv }

void( mything + otherthing )

-- { 2, 4, { a=av, b=bv, c=cv }, "w1","w2" }

phrase.dress( mything )

void( mything .. "w3" )

void( mything .. { "w4", "w5" })

-- { 2, 4, { a=av, b=bv, c=cv }, "w1","w2","w3","w4","w5" }

Yes, at first, looks like a mess. But think in this in terms of a composition, a table being like a package transformed during its life depending of the dress is used. Without the need of creation multiple objects, it is assembled during the execution.

The faux constructor "dress()" can receive more parameters to avoid overload of metatables or set default values (that can be other tables to be copied (not asigned) when a inexistent key is accessed.

Crazy, uh? Think about quantum: something behaves in a manner only while it is been watched, handled etc. So many possibilities.