Allow Pseudo-Overriding of Null-Coalescing Expression via Member-Based-Predication #2026
Replies: 14 comments
-
This is nothing more than a comment for #2020. Please keep the discussion in one place. |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr that's not true. #2020 asks to allow overriding of the |
Beta Was this translation helpful? Give feedback.
-
I will point out that the performance costs of adding a virtual method to object are huge. There is no way that is going to happen. It would also require CLR changes. A number of quite respected language designers think it was a mistake to even make Equals, ToString and ToHashCode virtual method on object. I would suggest you rework your proposal so it doesn't rely on that. |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt interesting point. I did however intend to mention that the addition of these members to the actual base types is optional, but would provide consistency. To an extent, I agree with the idea of not having virtual methods on base types, but not having equals would be taking it too far, but that is a sidebar. What I was more going for is an effect similar to how any type instance can be destructed into a |
Beta Was this translation helpful? Give feedback.
-
Well the issue with Equals is that it confuses two separate concepts. Value equality, and reference equality. Value equality only makes sense for objects that define it. So I would make ReferenceEquals a non virtual method on object, and use an interface to indicate value equality. |
Beta Was this translation helpful? Give feedback.
-
Can't you just use an extension method to do this?
|
Beta Was this translation helpful? Give feedback.
-
It makes zero percent sense for the null collase operating to do anything besides null collasing, espically if it doesn't even involve null at all |
Beta Was this translation helpful? Give feedback.
-
@willard720 well the whole point is to extend it; the purpose of the operator is to be able to expressively sub out null-evaluated expressions that would indicate an invalid/unset state, and replace the result with some fallback. In my opinion it makes sense to allow types to have some say of what counts as pseudo-null or an invalid state for that type. |
Beta Was this translation helpful? Give feedback.
-
@MgSam I agree with you on not allowing direct overloading, as is why I made a separate proposal to #2020, and while I understand that |
Beta Was this translation helpful? Give feedback.
-
Since hte value is not actually null, htere's a perfectly good solution you can provide: just add a method (or an extension method) on your type. You can then just do:
|
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi you realize that your suggestion argues against the current null-coalescing operator as well; it could be implemented as an extension method by the end-user, but the syntax sugar was added anyways, so why not extend it to be more useful and still be related to its original purpose? Especially now that C# 8 is introducing opt-in nullability, this would be even more useful. |
Beta Was this translation helpful? Give feedback.
-
It could have. But, in general, people do not tend to like 'instance looking' members being allowable on null instances. If that were not the case, i would have been ok with an extension-method solution. For real instances that can represent invalid states, it's still fine to have an instance call on it. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi The extension method would have to accept a function, at which point it's clearer and easier to just check for the invalid case manually. Nonetheless, I do not support this proposal, as adding too much complexity for too little benefit. |
Beta Was this translation helpful? Give feedback.
-
@TheFanatr So, you mean for example, I think it would be more useful to have a different operator than Maybe something like
Also, we have the null collasing operator because it's so common, and is shorter than If this feature was added, I actually wouldn't be able to use it in any of my existing code, because the only time I could imagine using pseudo-null is in data type structs, but now we can just make them nullable and have real nullable usage. Also, having a pseudo-null class makes no sense. Can you give an example where using nullable value type won't get the job done? Even in my example, I can get that job done using just |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow Pseudo-Overriding of Null-Coalescing Expression via Member-Based Predication
This is an alternative proposal for the allowance of the null-coalescing
??
operator to be overridden via letting the target expression's type programmatically dictate what the behaviour of the expression should be, thus indirectly overloading the operation of the operator. This would see the "Null-Coalescing Operator" be renamed to something like "the Validation Operator" to better fit it's use.target expression
??
fallback expression
where
target expression
decides whether or not the expression should use itself or the fallback value.This is useful because it allows for much easier value validation, and could eliminate magic behaviours. Also, once C# 8 launches with opt-in nullability, this operator could still have a major use.
Explanation
The general preface of this proposal is that instead of changing the current behaviour of the expression for any one type by default, the predication of whether or not the value of the
target expression
should be coalesced to is decided by the implementation of the type of the expression via a specific member. This could be done in 2 similar ways.This member could be implemented on the
Object
andValueType
types and made overridable to derived types like theToString
method is. This way, theObject
class could hold a default implementation that just checks if the object isnull
, and theValueType
implementation could just always return false, keeping compatibility with most codebases that currently use the expression.Definition of Sample Class
Possibility One
There should be a member that is reflectively called if existing on the type that allows the type to dictate whether or not any instance of a type should coalesce to the fallback or not. Basically, there should be a
Coalesce
method implemented on the object that checks if the state is invalid.This would also allow for the modification of the value in case it is needed such as in cases where something on the object needs to be calculated or state-changed, but the object is immutable.
Possibility Two
The general idea of possibility two is that the validity-based coalescing expression should attempt to cast the
target expression
to abool
-typed Boolean value and if the result istrue
, the object is considered to be in a valid state and the expression is not coalesced to thefallback expression
. This is the opposite functionality to possibility one. The implementation of a coalescence predication member in this manner should appear as follows.Behaviour
With respect to the definition of the coalescence validity predication member from either possibility, the expression should operate like so, with said member being called to determine whether or not the
fallback expression
should be evaluated and presented as the value of the overall expression based on the convention of the possibility from which the member definition originates.Beta Was this translation helpful? Give feedback.
All reactions