-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Custom binary operators not supported #21
Comments
Yes, you are right. I confirm the bug. I will try to fix it in the following days. |
I just realized the issue is more generic. Overloadable operator methods have special names when retrieved via Reflection, such as op_Addition for +, etc. That's why ExressionParser.FindMethods fails to find these methods. So, it's necessary to support the overloadable operators as mentioned here: |
I have just fixed the bug. It was caused by a code that try to convert all operator parameters to a specific primitive type. For example if you write "1 == 0.5" internally the parser convert the parameters so that they use the same type (in this case Int32). Of course this code have no sense for custom operators. So I simply removed the exception if the above code cannot understand the base type. I'm not sure if my explanation is clear ... anyway now the test passed without problem! Thank you for bug report, write me if you find other related or not related problems. |
Hello, Thanks for your prompt response. However, I'm afraid the issue still occurs when using overloaded operators on classes. For example, if I change TypeWithOverloadedBinaryOperators to a class, and even if I reference the type that contains the overloaded operators, I get a ParseExeption. Consider the following minimal changes in the corresponding unit test: [TestMethod]
public void Can_use_overloaded_operators()
{
var target = new Interpreter();
target.Reference(typeof(TypeWithOverloadedBinaryOperators)); //this has no effect
var x = new TypeWithOverloadedBinaryOperators(3);
target.SetVariable("x", x);
string y = "5";
Assert.IsFalse(x == y); //this works and calls the overloaded operator
Assert.IsFalse(target.Eval<bool>("x == y", new Parameter("y", y))); //this fails
//the rest is same as before
}
class TypeWithOverloadedBinaryOperators //converted from struct to class
{
//same as before
} The exception details are:
Perhaps a different approach is needed, e.g. like the one used for invoking extension methods? |
Ok, now I hope it is fixed. There was some other similar checks for reference type only. I'm not 100% sure to have covered all scenarios because I basically just removed some check. Anyway I have just released version 0.11.4.0 . Write me for any other problem. |
A ParserException occurs when attempting to evaluate an expression that uses custom binary operators on objects. Add the following code to OperatorsTest.cs to reproduce.
causes the following:
The text was updated successfully, but these errors were encountered: