Proposal: An interface which implements true, false, &, |, operators in base interfaces should be allowed to use short-circuiting && and || operators #9519
Unanswered
Charlieface
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
EDIT: Oops, misread the proposal. I do see that it is intended to allow
|
Beta Was this translation helpful? Give feedback.
3 replies
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.
-
Summary of proposal
It should be allowed that an interface
IDerived : IBase1, IBase2
, which implementstrue
,false
,&
,|
, operators via any base interfacesIBase1
IBase2
, should be allowed to use short-circuiting operators&&
and||
, despite the fact the combining operators are defined in separate base interfaces.This allows the various operators to be defined in separate interfaces, something which is very common with other operators in
System.Numerics
. It's a blocking issue on implementingITruthOperators
, as noted in this issue on dotnet/runtime. It's very surprising that this doesn't work at the moment, as it doesn't follow the normal overload resolution for operators in the spec.Note that this is not allowing
|
or&
to short-circuit. It's merely widening the scope of overload resolution so that||
and&&
work in more cases.Explanation
Some of the user-defined operators you can define in C# include:
The C# spec (12.14.3) then specifically allows for short-circuiting operators
&&
and||
to be used.In the case of interfaces, these operators can be defined as
static abstract
, which means you can use that interface as a generic type argument, and the operators then work based on the actual reified type at runtime.However, it does not allow for the
false
operator to be in a different interface than the&
, or thetrue
to be in a different interface than the||
, even if one interface inherits the other, or the type parameter is declared inheriting both types.For example, the following code is taken from the proposal for dotnet/runtime
The following code generates a compiler error:
It would only be allowed if all operators were declared together, which is quite awkward for that linked proposal, because
IBitwiseOperators
is already defined in the CLR.In a closed bug report on this in dotnet/roslyn, @jaredpar and @AlekseyTs state this is intentional, because the above spec states:
and that is expected to be the interface itself, not any of its base types.
Proposal
I propose that the spec is relaxed as follows: (change in bold)
I can see no reason why this should be an issue, but there may be some corner cases involving nullable types or similar.
Beta Was this translation helpful? Give feedback.
All reactions