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 ....
--
programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs 3 1972
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com... 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. --
programmer, author http://www.midnightbeach.com and father http://www.midnightbeach.com/hs
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com... 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.
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 ....
--
programmer, author http://www.midnightbeach.com and father http://www.midnightbeach.com/hs
"Daniel O'Connell [C# MVP]" wrote: 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.
--
programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Simon Ford |
last post by:
Hi All,
I'm having trouble understanding exactly how I can do some specific implicit
casting. There are two problems here; does anyone know what I should be
doing?
//----------
// (1)...
|
by: Boris |
last post by:
Hello,
I have written a program that compiles just fine with GCC 3.3. With
GCC 3.4.1 it results in this error:
g++ -O0 -g3 -Wall -c -oTestp.o ../Testp.c
.../Testp.c: In function `int main()':...
|
by: Generic Usenet Account |
last post by:
This is a two-part question.
(1) I have implemented a "Datastructure Registry" template class. I am
getting no compiler warnings with older compilers, but newer compilers
are generating the...
|
by: Jon Shemitz |
last post by:
How come I can write code like "if (L)" but NOT code like "if (L ==
true)" when L is a class with an implicit operator bool?
///////////
List L = new List();
public class List
{
private...
|
by: Girish |
last post by:
Im trying to understand implicit type conversions from object -> string and
vice versa.
I have two classes, one Driver and one called StringWrapper. These are just
test classes that try and...
|
by: Calum Grant |
last post by:
Here's a little utility I thought I'd share.
(For those familiar with ATL::CComQIPtr, the concept is similar).
====
#ifndef DYNPTR_H_INCLUDED
#define DYNPTR_H_INCLUDED
|
by: utab |
last post by:
Dear all,
I was trying to write a more complex program, and while searching for
sth in my reference C++ primer, by Lippman. I had a question in the
mind, see the code below
#include <string>...
|
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= |
last post by:
Coming originally from C++, I used to do the likes of the following,
using a pointer in a conditional:
void Func(int *p)
{
if (p)
{
*p++ = 7;
*p++ = 8;
|
by: tonvandenheuvel |
last post by:
Hi all,
please consider the following piece of code:
#include <iostream>
template<typename T>
class A
{
public:
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |