Proposal: Default-or-Empty Coalesce operator "???" #183
Replies: 8 comments 3 replies
-
As @ufcpp says, this idea has bee suggested before, using the
Then, if
If it's a struct:
Whilst |
Beta Was this translation helpful? Give feedback.
-
What I do for strings specifically is two extension methods: var bar = foo.NullIfEmpty() ?? "baz";
var bar = foo.NullIfWhitespace() ?? "baz"; // By far the more used |
Beta Was this translation helpful? Give feedback.
-
@jnm2, agreed, I have nearly identical extension methods in my arsenal. :) @DavidArno, the proposals have similarity, but #147 would be incapable of solving this problem for Also, since #147 proposes no changes to existing native types, its use cases could be easily solved by overloading '||' without having to change anything in the language. I'll post an example there for that discussion. In that case, simply adding a public class String {
public static String operator |(String s1, String s2) => IsNullOrEmpty(s1) ? s2 : s1;
public static bool operator true(String s) => !IsNullOrEmpty(s);
public static bool operator false(String s) => IsNullOrEmpty(s);
}
...
string foo = null;
string bar = foo || string.Empty || "bar"; // "bar"
string baz = foo || string.Empty; // "" Similar changes to |
Beta Was this translation helpful? Give feedback.
-
public extension StringOperators of string
{
public static string operator |(string x, string y) => string.IsNullOrEmpty(x) ? y : x;
} |
Beta Was this translation helpful? Give feedback.
-
IsNullOrEmpty is not as common as IsNullOrWhitespace for me. We should at least attempt to determine which of the two is more deserving of special operator treatment, if either. |
Beta Was this translation helpful? Give feedback.
-
@jnm2, I don't have any strong preference, though it seems to me that a string being "empty" is a special state that is analogous to null. In other words, the only reason we even have to distinguish them is because the .NET team chose to implement I was curious about the relative popularity, here's what I found across all public GitHub repositories: IsNullOrEmpty - 2,491,375 results GitHub's search doesn't allow nailing down which of these is more popular in connection with the conditional operator, which would perhaps be a better gauge of which would benefit more from a shortcut syntax, but at first glance, testing for emptiness seems to be the far more popular option. |
Beta Was this translation helpful? Give feedback.
-
Next up: the |
Beta Was this translation helpful? Give feedback.
-
Default-or-Empty Coalesce Operator "???"
Summary
Add a new operator
???
to coalesce either a null or an "empty" value to some other value.Motivation
The null-coalescing operator
??
is of limited use with strings, because in many cases the logic should treat either a null or an empty string the same way. WhileString.IsNullOrEmpty()
provides a useful method to test these states together, the syntax leaves something to be desired. For example:Also, the
??
operator offers no utility for value types that, while not nullable, have adefault(T)
that should be replaced with a more suitable value when found.DateTime
is an excellent example:Detailed design
For string objects, the proposed
???
operator returns the left-hand operand if the operand is not null and is not empty; otherwise it returns the right hand operand. E.g.:For value type instances,
???
operator returns the left-hand operand if the operand is not equal todefault(T)
; otherwise it returns the right hand operand.For
Nullable<T>
instances,???
operator returns the left-hand operand if the operand is not null and is not equal todefault(T)
; otherwise it returns the right hand operand.Drawbacks
The proposal includes rules for how this operator would apply to other types, but admittedly its primary purpose is to solve a problem specific to strings. In an ideal world, language operators should be generally useful, and there may not be a strong enough case for such an operator for non-string types.
Alternatives
One alternative would be to overload the
true
,false
, and|
operators forSystem.String
, allowing a syntax that is very similar to EMCAScript:Similar improvements could be made to
DateTime
andGuid
.Unresolved questions
For non-string reference types, there is no clear answer as to whether the operator should be available, and if so, how it should behave. It could simply be equivalent to
??
. Or, for exmaple, a new interfaceIEmptyable
could be created with a single read-only propertyEmpty
, which could then be used for the comparison.Design meetings
This is an individual proposal, I'm not aware of any similar discussions to date.
Beta Was this translation helpful? Give feedback.
All reactions