Skip to content

String interpolation with format fails with nullables #167

Description

@MatElcome

The following code throws an EvalException:

var compiledExpr = Z.Expressions.Eval.Compile("$\"{d:yyyyMMdd}\"", new Dictionary<string, Type> { ["d"] = typeof(DateTime?) });

I was expecting it to compile similar to:

System.Linq.Expressions.Expression<Func<DateTime?, string>> expr = d => $"{d:yyyyMMdd}";
var compiledExpr = expr.Compile();
var today = compiledExpr(DateTime.Now); // Returns "20260724"
var emptyString = compiledExpr(null); // Returns ""

The exception is only thrown if a format specifier is used, and only when the expression resolves to a Nullable<> type.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions