question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Operator ?: seams to evaluate both sides of expression.

See original GitHub issue

Expression xx == null ? 0 : xx.SomeProperty failed with exception “No property or field SomeProperty exists in type Object (at index 22).” The problem is that the expression xx is null. If I change the expression to “xx == null ? 1 : 2” it is evaluated and the result is 1.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RupertAverycommented, Aug 1, 2016

The problem is that the parser has no idea what xx is.

The parser is not evaluating xx.SomeProperty, the error is a parse-time error. You are assigning a null value to xx, and without any type information the parser cannot know what xx is. null is treated as type Object and so in the process of parsing, it tries to find SomeProperty on type Object, which of course fails.

This is why C# forbids assigning a null value using the var expression.

So you need to pass in the type information explicitly using the SetVariable(string, object, Type) overload. Suppose you have SomeClass with SomeProperty:

interpreter.SetVariable("xx", null, typeof(SomeClass));

This will make your code work as expected.

Note however that assigning null to a typed variable does not work:

SomeClass xx = null;
interpreter.SetVariable("xx", xx);

This is because a typed variable set to null will actually be passed as a null object (without type information) into SetVariable, it acts as if it had no type at all, hence the need for an overload explicitly allowing you to set the type.

0reactions
1andy11commented, Aug 1, 2016

You are right. Using SetVariable with type parameter make it work. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does the logical operator || executes both sides before ...
The right hand side of either the '&&' and '||' logical operators will only be evaluated and returned if the left hand side...
Read more >
The mechanical evaluation of expressions
We shall consistently distinguish between "operators" and "functions" as follows. An operator is a sub- expression of a (larger) expression appearing in a....
Read more >
Chapter 5: Expressions -- Valvano
The basic problem in evaluating expressions is deciding which parts of an expression are to be associated with which operators.
Read more >
Chapter 15. Expressions
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to...
Read more >
Operators and Expressions in Python
Logical Operators. The logical operators not , or , and and modify and join together expressions evaluated in Boolean context to create more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found