This project has moved and is read-only. For the latest updates, please go here.

Operator 'Equal' is not defined for types System.Int32 and System.Decimal.

Feb 25, 2015 at 9:25 AM

I need to define the "==" operator for a decimal and an integer.

Currently I have a method, lets name it A(), that returns an Enum as an Int. I later use this integer to check if it is 0 or 1 in a function that is being parsed by Irony.

I have two number literals, one defined suchas:
var enNumber = new NumberLiteral("enNumber")
                DefaultIntTypes = new[] { TypeCode.Decimal },
                DecimalSeparator = '.'
And one defined suchas:
 var svNumber = new NumberLiteral("svNumber")
                DefaultIntTypes = new[] { TypeCode.Decimal },
                DecimalSeparator = ','
The number from A() is being returned as an Int, but is somehow being parsed as decimal (this is probably correct). However, when the comparison is being made:

(something like this):

if A() == 1

It breaks and gives me "Operator 'Equal' is not defined for types System.Int32 and System.Decimal.".

I belive a lot of operators are not implemented for decimal. Maybe a lot of parsing/casting should be implemented as well...

Any help is very much appreciated.

Feb 25, 2015 at 7:08 PM
Look at LanguageRuntime class, file LanguageRuntime_OpDispatch_Init.cs. All binary operations (including equal) are explicitly encoded there. You need to subclass LanguageRuntime, override Grammar.CreateRuntime method (return your custom runtime instance).
The best way to go I think is to provide type conversion method to automatically convert Int to decimal for type-mismatching Equal. I see that there's no typeconverter int->decimal; so override InitTypeConverters, call base method, and add int->decimal converter. That should solve the problem. For you '==' operator, the runtime will convert int to decimal and then compare 2 decimals.