-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional.lua
More file actions
34 lines (30 loc) · 847 Bytes
/
optional.lua
File metadata and controls
34 lines (30 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- This is to show one of the gotchas around null values in tables.
-- if a table contains a null, then that is the end of the array like properties.
-- {"a",null,"b"} when used with ipairs will only be able to access "a" as the second
-- element is null.
-- This shows how to return something, even a blank table, to preserve the table order
local function elem(show)
if show then
return div{"table was returned"}
else
return {}
end
end
local function nullElem(show)
if show then
return div{"optional table was returned"}
end
end
return function()
return div{
elem(true),
elem(false),
elem(true),
elem(false),
-- other option is to always put the optional content with in its own table
-- this preserves the table iteration order
{null},
{nullElem(false)},
{nullElem(true)}
}
end