开发者

Writing API called from Lua - 0 or 1 based?

开发者 https://www.devze.com 2023-01-30 07:58 出处:网络
If you were to write an API that is called from Lua (which is 1-based, e.g. table indices start at 1), would you apply the same rule to your API?

If you were to write an API that is called from Lua (which is 1-based, e.g. table indices start at 1), would you apply the same rule to your API?

For example, say your API had a function called GetFoo(x, y) which returned a Foo at the coordinate (x,y). Would you start your coordinate axes at (0,0) or (1,1) for the API, assuming that in the s开发者_JAVA技巧ystem itself (say written in C or C++ which are 0-based) these things start at (0,0) (so if you used the Lua convention you would always have to subtract 1 when retrieving numbers for these kinds of operations from the lua stack).


I haven't used Lua, but I would say for a coordinate system specifically (0,0) would be preferred.

For everything else, as long as you state it clearly in the documentation, by all means start indices at 1.


You could also just use the 0 index in your table/arrays. The only inconvenience is the standard libraries use the 1-based convention. So things like table.sort, string operations, etc ... will ignore the table[0] element.

0

精彩评论

暂无评论...
验证码 换一张
取 消