New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

._add() meta-method not called for strings #170

Open
jayeheffernan opened this Issue Dec 2, 2018 · 0 comments

Comments

Projects
None yet
1 participant
@jayeheffernan

jayeheffernan commented Dec 2, 2018

I've run into an oddity in the way the .add() meta-method works. I have specified it on a class and I was expecting it to be called whenever an instance of that that class is followed by + some_other_value. The weird thing is it is not called when the value being added is a string. It is called for all other types that I could think of. The ._sub() meta-method for subtraction is called for all of the same types as .add() except that it is still called when the right operand is a string.

I've run into this issue in the Electric Imp version of Squirrel, but I've modified the code (slightly) to run in Squirrel 3.1 64 on Linux and I get the same results, so it seems to be a "feature" of the language rather than compiler or implementation specific. I've included code and logs below.

Is this a bug? Is there some rationale behind this exception?

// "polyfill" my logging method
server <- { log=@(str) print(str + "\n") };

class A {
    function _add(thing) {
        server.log("adding " + (typeof thing) + ": " + thing);
        return "added";
    }
    
    function _sub(thing) {
        server.log("subtracting " + (typeof thing) + ": " + thing);
        return "subtracted";
    }
}


results <- [
    A() + 123,
    A() + 123.4,
    A() + "abc", // this is the odd one out: no meta-method is called
    A() + true,
    A() + null,
    A() + {},
    A() + A,
    A() + [],
    A() + (@() null),
    
    A() - 123,
    A() - 123.4,
    A() - "abc",
    A() - true,
    A() - null,
    A() - {},
    A() - A,
    A() - [],
    A() - (@() null),
];

server.log("   --- results ---   ");
foreach (r in results) {
    server.log(r);
}
adding integer: 123
adding float: 123.4
adding bool: true
adding null: (null : 0x(nil))
adding table: (table : 0x0x564b1effc3f0)
adding class: (class : 0x0x564b1effafb0)
adding array: (array : 0x0x564b1effbed0)
adding function: (function : 0x0x564b1effc110)
subtracting integer: 123
subtracting float: 123.4
subtracting string: abc
subtracting bool: true
subtracting null: (null : 0x(nil))
subtracting table: (table : 0x0x564b1effc3f0)
subtracting class: (class : 0x0x564b1effafb0)
subtracting array: (array : 0x0x564b1effbed0)
subtracting function: (function : 0x0x564b1effba20)
   --- results ---   
added
added
(instance : 0x0x564b1effb260)abc
added
added
added
added
added
added
subtracted
subtracted
subtracted
subtracted
subtracted
subtracted
subtracted
subtracted
subtracted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment