sharpwind612 / luainterface

Automatically exported from code.google.com/p/luainterface
2 stars 4 forks source link

Cannot handle overloads with same number of argument but different type #19

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Run the example below

What is the expected output? What do you see instead?
Expected
1-2
1:2

Actual
1-2
1-2

What version of the product are you using? On what operating system?
LuaInterface: 2.0.0.16708
Lua51.dll
Windows XP3

Please provide any additional information below.

LuaInterface only matches method based on number of arguments, not datatype.

This might be difficult to handle as Lua does not have types the same way.
This is not an important feature right now, I only found it when looking at the 
MethodWrapper source to see if I could help with Issue 18.

Example code

void Main()
{
    LuaInterface.Lua e = new LuaInterface.Lua();
    Join j = new Join();
    e["Join"] = j;
    e.DoString("return Join:oload(1,2)").Dump();
    e.DoString("return Join:oload(\"1\",\"2\")").Dump();
}

public class Join {
    public string oload(int i1, int i2) {
        return i1 + "-" + i2;
    }

    public string oload(string i1, string i2) {
        return i1 + ":" + i2;
    }
}

Original issue reported on code.google.com by dmartens...@gmail.com on 2 Jul 2010 at 12:34

GoogleCodeExporter commented 8 years ago
You are right about the issue being the dynamic typing of Lua.  It doesn't see 
a problem with passing string arguments of your second method call to the int 
versions and vice versa, so it goes with the first one it finds that matches.  

A method of getting around this does exist in LuaInterface.  I've never used it 
personally in my own scripts (I'm new to the project :P), but there is a method 
named get_method_bysig that takes an object instance and a list of types and 
gets the proper method signature.  You can then use that signature to call the 
proper overload.  There is a good example of it in the TestLuaInterface.cs file 
of the current trunk.  Do a search for GetMethodBySignatureFromObj.  There is 
also an example of it getting a signature by Type rather than instance just 
below that one.

Let me know how that works for you or not so I know whether we can close this 
issue.

Thanks!

Original comment by eonstorm@gmail.com on 2 Jul 2010 at 12:59

GoogleCodeExporter commented 8 years ago
Yes that worked.

void Main()
{
    LuaInterface.Lua e = new LuaInterface.Lua();
    Join j = new Join();
    e["Join"] = j;
    e.DoString("f = luanet.get_method_bysig(Join, 'oload', 'System.Int32', 'System.Int32') return f(1,2)").Dump();
    e.DoString("f = luanet.get_method_bysig(Join, 'oload', 'System.String', 'System.String')return Join:oload(\"1\",\"2\")").Dump();
}

public class Join {
    public string oload(int i1, int i2) {
        return i1 + "-" + i2;
    }

    public string oload(string i1, string i2) {
        return i1 + ":" + i2;
    }
}

Produces the correct result.

Original comment by dmartens...@gmail.com on 2 Jul 2010 at 1:20

GoogleCodeExporter commented 8 years ago
Excellent.  I'm going to go ahead and close this then.

Original comment by eonstorm@gmail.com on 2 Jul 2010 at 1:35