Null-conditional with-expressions #7649
Unanswered
mrwensveen
asked this question in
Language Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to be able to use the
with
expression on nullable record types, as if calling an instance method on that type. For example, suppose I have a class Book and a record Person:I might want to set the Author's FirstName, but only when it's not null:
Or I might want to copy (clone) the author from another
Book
instance:Under the hood, both use a generated
<Clone>$()
method, so technically null-conditional calls could be done as (for the second example):This is specifically prohibited because of the generated method's name.
My proposal is to allow null-conditional with-expressions:
setting Author to null when other.Author is null, or applying the with expression (clone-and-set) when it isn't null.
Beta Was this translation helpful? Give feedback.
All reactions