建议开lua板块,借地问个简单的问题
有个表def = {foo = "int,int",name = "string",nie = "property int",lot = "int,string",}在文件“def.lua”里
1.如何判断表中没有的属性访问非法,如def.foo1 = “123”非法,def.name = “china”合法
2.如何动态判断函数的参数传递合法,如def.foo(1,2)合法 def.foo(1,"125")非法,def.foo(1,2,3)非法
请动态的判断,不要每个函数都用type来判断为number或string
3.当def.name = "china"时怎么调用隐式函数function setname()(已定义的)
请给相关建议,思想,源码都可
[解决办法]
第一个问题简单
Lua 5.0.3 Copyright (C) 1994-2006 Tecgraf, PUC-Rio
> t = { 11, 22, 33, you='one', me='two' }
> table.foreach(t,print)
1 11
2 22
3 33
me two
you one
>
> = t[2]
22
> = t.me
two
> = t.fred
nil
Indexing the table with a valid index returns the value stored at that index.Indexing the table with an undefined index returns nil
第三个问题要用到 _index,涉及到Metamethod和Metatable;
比如:
> print(s.length) -- no such key in the String table class
nil
> mt.__index = function (t,key)
>> if key == 'length' then return string.len(t.value) end
>> end
> print(s.length) -- new __index event calls the above function
6
[解决办法]
1. Metatable
2. 没看明白。def.foo明明是个字符串,你把它当成函数调用?
3. Metatable