implicit operator bool - question 2

J

Jon Shemitz

Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined to fault my understanding
because I'm generally very impressed with the quality of the C# design
and implementation - but it sure LOOKS like "operator true" and
"operator" false are just some sort of design archaeology that never
got thought through ....
 
D

Daniel O'Connell [C# MVP]

Jon Shemitz said:
Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Fwiw, I'm having a hard time coming up with any C# 1.1 code that calls
true or false operators. I'm inclined to fault my understanding
because I'm generally very impressed with the quality of the C# design
and implementation - but it sure LOOKS like "operator true" and
"operator" false are just some sort of design archaeology that never
got thought through ....

I can't come up with areason for both, I'll take a closer look at the specs
for justification. However, asto code that will use the operators, you
basically need to use the ? operator:

object ? trueStatement : falseStatement; calls object.op_True if its
present.
 
D

Daniel O'Connell [C# MVP]

Jon Shemitz said:
Why would you ever want to define both "public static bool operator
true" and "public static bool operator false" when you can just define
"public static implicit operator bool"?

Is there syntax where the two are not equivalent? Or are there cases
where you might legitimately want both false and true to return the
same value?

Ok, I read up on the operators and there is a valid reason for there being 2
operators. Basically, it allows you to have values that are true, false,
both true and false, or niether true and false(IE null, I imagine this is
how the DB types work). There is an example using true and false operators
in the c# spec that implements a DBBool class, supporting null.

According to the spec, the && and || operators, when in the prescense of
overloaded & and |, use true and false. A call to && uses the false
operator, || the true. From the ecma C# spec(page 171, pdf page 187, section
14.11.2):

? The operation x && y is evaluated as T.false(x) ? x : T.&(x, y), where
T.false(x) is an
invocation of the operator false declared in T, and T.&(x, y) is an
invocation of the selected
operator &. In other words, x is first evaluated and operator false is
invoked on the result to
determine if x is definitely false. Then, if x is definitely false, the
result of the operation is the value
previously computed for x. Otherwise, y is evaluated, and the selected
operator & is invoked on the
value previously computed for x and the value computed for y to produce the
result of the operation.
? The operation x || y is evaluated as T.true(x) ? x : T.|(x, y), where
T.true(x) is an
invocation of the operator true declared in T, and T.|(x, y) is an
invocation of the selected
operator |. In other words, x is first evaluated and operator true is
invoked on the result to
determine if x is definitely true. Then, if x is definitely true, the result
of the operation is the value
previously computed for x. Otherwise, y is evaluated, and the selected
operator | is invoked on the
value previously computed for x and the value computed for y to produce the
result of the operation.

Situations where you'd want both true and false are pretty rare, I suspect,
however I guess it may have its value for 0(> 0 is true, < 0 false, 0 is
both, null niether) in some situations.
 
J

Jon Shemitz

Daniel O'Connell said:
Ok, I read up on the operators and there is a valid reason for there being 2
operators. Basically, it allows you to have values that are true, false,
both true and false, or niether true and false(IE null, I imagine this is
how the DB types work). There is an example using true and false operators
in the c# spec that implements a DBBool class, supporting null.

According to the spec, the && and || operators, when in the prescense of
overloaded & and |, use true and false. A call to && uses the false
operator, || the true. From the ecma C# spec(page 171, pdf page 187, section
14.11.2):

Thanks - I'll experiment with this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top