satoren / kaguya

C++ binding to Lua
Boost Software License 1.0
345 stars 70 forks source link

An object is not automatically casted into a function parameter #86

Closed Philanatidae closed 6 years ago

Philanatidae commented 6 years ago

I have (essentially) this code:

class A {
public:
    A():_myInt(10) {}

    bool equals(const A& other) {
        return _myInt == other._myInt;
    }
private:
    int _myInt;
};

...
kaguya::State state;
state["A"].setClass(kaguya::UserdataMetatable<A>()
    .setConstructors<A()>()
    .addFunction("equals", &A::equals));

state("a1 = A.new(); a2 = A.new(); print(a1:equals(a2))");

The a1:equals(a2) prints an error:

maybe...Argument mismatch:userdata       candidate is:
                A,A,

From experimenting, it appears that Kaguya puts in metadata with a string of the type. In this case, A. This way it can check against the type to convert to, A, and convert to it safely if they match. However, this does not appear to be working correctly, since A is clearly equal to A, yet it says that there is an argument mismatch. Is this a problem with my code, or Kaguya itself?

Philanatidae commented 6 years ago

I made a mistake, I was accidentally calling a1.equals(a2) instead of a1:equals(a2). Thank you for the wonderful library!