Method overloads
Lua has no static types, so same-name multiple signatures cannot be chosen at compile time. ZLua provides: default dispatch, full-signature keys (automatic at Bind), [LuaAlias], and register_method short names. See: Demo.Run, app.lua.
Day-to-day demo:Run(10) is enough in Lua calling C#; read this page for ambiguity or hot paths. Authoritative details: Overload Spec.
How to choose
| Approach | Syntax | When |
|---|---|---|
| Default dispatch | demo:Run(10) | Arguments uniquely match |
| Full-signature key (automatic) | demo['Run(System.Int32)'](demo, 5) | Exact pick; no register_method needed |
[LuaAlias] | demo:run_i32(5) | You can edit C#; short name on hot paths |
register_method | After register: demo:run_i32(5) | Cannot edit C#; still want short name + colon |
Default dispatch
demo:Run(10) -- Run(int)
demo:Run("hello") -- Run(string)
Zero dispatch when there is only one public overload; with multiple, match by arguments.
Full-signature keys (auto-registered on name conflicts)
When the same method name has multiple overloads (e.g. Run(int) / Run(string)), besides the Run dispatch, Bind also hangs a direct key for each candidate:
MethodName(parameter Type.FullName, …) (no return type):
| Key | Meaning |
|---|---|
Run | Runtime dispatch |
Run(System.Int32) | Fixed Run(int) |
Run(System.String) | Fixed Run(string) |
-- 精确调用,不必 register_method
demo['Run(System.Int32)'](demo, 5)
demo['Run(System.String)'](demo, "hi")
Keys contain parentheses, so you cannot write demo:Run(System.Int32)(...); use bracket keys + dot + explicit self.
[LuaAlias]
[LuaAlias("run_i32")]
public void Run(int value) { }
public void Run(string value) { }
demo:run_i32(10) -- 短名 + 冒号,O(1);Run(int) 已换名,不再挂 "Run"
demo:Run("hi") -- 未换名的 Run(string)
An alias is a rename (replaces the default Lua key), not an append. XML (luaAliasXmlPaths / ZLuaAlias) and more examples: LuaAlias. C# extensions on the instance table: Extension methods.
register_method: short name + colon
Full-signature keys already allow exact calls, but the syntax is verbose. register_method hangs a direct closure onto an unused short name; then you can use colon:
local run_i32 = demo['Run(System.Int32)']
zlua.register_method("run_i32", run_i32)
-- 好处:短名进入 method 表,之后直接
demo:run_i32(5)
Two-argument form (per 05-LIB): zlua.register_method(aliasName, directClosure).
If aliasName already exists (including Run, full-signature keys, other aliases) → error; no overwrite.
You can also take a closure from a [LuaAlias] key and hang another custom name.
Common mistakes
| Symptom | Fix |
|---|---|
Wrong overload / ambiguous overload | Full-signature key, [LuaAlias], or register_method short name |
Want demo['(System.Int32)'] | Forbidden; must include the method name: Run(System.Int32) |
register_method says already taken | Pick an unused alias; do not overwrite Run / existing full-signature keys |
Learning path
| Previous | 0GC Marshal |
| Next | LuaAlias |
Related
- LuaAlias guide — Attribute + XML deep dive
- Method overload Spec §3.7, §5, §6
- Common zlua library