Comparing null with a non-null type

Aug 29, 2011 at 10:10 PM

I am trying to get an expression like "value != null" to evaluate to true. I am replacing value with the string "notNull". When I run this through the parser I am getting the following error:

myFile.myMethodl:
Irony.Interpreter.RuntimeException : Cannot convert value from type  to type System.String, type converter not defined.
  ----> System.Exception : Cannot convert value from type  to type System.String, type converter not defined.

I noticed in the error message that it doesn't mention what the first type is. I am guessing that is null.

What can I do to get this working?

Coordinator
Aug 30, 2011 at 7:13 PM

what version of interpreter are you using? 

Aug 30, 2011 at 8:21 PM

The one on the download page. July 5th 2011

Coordinator
Aug 31, 2011 at 6:54 PM

I have a quick simple fix, but not sure if it would work for you. Find method InitBaseTypeList in LanguageRuntime_Init.cs file. In type list, add "null" as the first element, like this:

    public virtual void InitBaseTypeList() {
      BaseTypeSequence.Clear(); 
      BaseTypeSequence.AddRange(new Type[] {null, //add this null
        typeof(string), typeof(Complex), typeof(Double), typeof(Single), typeof(Decimal), 
        typeof(BigInteger), 
        typeof(UInt64), typeof(Int64), typeof(UInt32), typeof(Int32), typeof(UInt16), typeof(Int16), typeof(byte), typeof(sbyte), typeof(bool) 
      });
    }

if it does not work, I have no fix for now. You see, this "old" interpreter was just a quick sample, not a real thing. It has many shortcomings, and I'm working on a new, real version. I would be reluctant to spend any time on fixing this old "sample" like code. If you need a real interpreter/evaluator - just wait a bit for a new one - please. Or start exploring it - initial draft is in latest source drop.

Sorry for troubles

Roman

Aug 31, 2011 at 9:02 PM

Roman,

We found a different solution that takes care of the problem. We made a change to LanguageRuntime.cs and LanguageRuntime_Init.cs. Our changes are in bold.

In LanguageRuntime.cs on line 143 the following change was made:
//If one of the types is null, use a string to compare the 2 values
t = null;
if (IsOneOf(t, type1, type2)) return typeof(string);
//First check for boolean op; some languages allow ints to be interpreted as bools in expressions
t = typeof(bool);
if (isBoolOp || IsOneOf(t, type1, type2)) return t;

In LanguageRuntime_Init.cs in the InitTypeCoverters method the following was added:
//null->string
TypeConverters.Add(null, typeof(string), value => null);

We would also like to point out that we have found some things that we feel are errors. We can have an expression with an integer == string and that will evaluate correctly. But when we have an expression like boolean == string that will throw an exception. It seems like these 2 cases should behave in the same manor.

Coordinator
Sep 1, 2011 at 4:20 AM

about == inconsistency - you're right, they should behave the same. I will double-check this in new interpreter

thanks!

Roman