summaryrefslogtreecommitdiff
path: root/test/type.lua
blob: 3079391d12e2309d7b5d6b0b3810b1f4cd6e6a79 (plain)
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
35
36
37
$debug

function check (object, class)
 local v = next(object,nil);
 while v ~= nil do
   if class[v] == nil then
    print("unknown field: " .. v) 
   elseif type(object[v]) ~= class[v].type then
    print("wrong type for field " .. v)
   end
   v = next(object,v);
 end
 v = next(class,nil);
 while v ~= nil do
   if object[v] == nil then
     if class[v].default ~= nil then
      object[v] = class[v].default
     else
      print("field "..v.." not initialized")
     end
   end
   v = next(class,v);
 end
end

typeblock = {x = {default = 0, type = "number"},
               y = {default = 0, type = "number"},
               name = {type = "string"}
              }

function block(t) check(t,typeblock) end

@block{ x = 7, name = "3"}
@block{ x = "7", name = "3"}
@block{ x = 7, name = 3}
@block{ x = 7}
@block{ x = 7, name = "3", bogus=3.14}