Skip to content

Conversation

metoule
Copy link
Contributor

@metoule metoule commented Sep 6, 2025

The expression we generate for the null-coalescing operator doesn't work for nullable types, because there's no comparison operator between a nullable type and an object:

const object nullConst = null;
int? MyVar = null;
return MyVar == nullConst ? 0 : MyVar; // <-- doesn't compile

This PR changes the generated expression for nullable types from expr == null ? exprRight : expr to expr.HasValue ? expr.Value : exprRight

Fix #368

@metoule metoule changed the title Fix 367: generate proper expression for null-coalescing operators on nullable types Fix #368: generate proper expression for null-coalescing operators on nullable types Sep 6, 2025
@metoule metoule marked this pull request as draft September 7, 2025 13:41
@metoule
Copy link
Contributor Author

metoule commented Sep 7, 2025

Back to draft, so that I can see how to handle the other binary operators.

@metoule
Copy link
Contributor Author

metoule commented Sep 8, 2025

After playing with sharplab, I discovered that for nullable types, the .NET compiler generates:

int? a;
int b = a ?? 10; // int b = a.GetValueOrDefault(10);
int? a;
bool b = a > 10; // bool b = (a.GetValueOrDefault() > 10) & a.HasValue

I'll update the PR to match this.

@metoule
Copy link
Contributor Author

metoule commented Oct 2, 2025

It's actually ready, the other binary operators are properly resolved (e.g. int? > int works as intended).

@metoule metoule merged commit 8825efb into dynamicexpresso:master Oct 6, 2025
2 checks passed
@metoule metoule deleted the fix_367 branch October 6, 2025 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nullable variables (int?) cause error on evaluation when value is null

2 participants