It appears to be a flaw in the specs, or I did not understand how it works.
7.11.2 User-defined conditional logical operators says that in order to
overload conditional logical operators && and || (which cannot be directly
overloaded in C#) you need to overload the true and false operators and the
compiler will convert expressions like (x && x) to T.op_False(x) ? x :
T.op_BitwiseAnd (x, y). However this results in evaluating the wrong logical
expression since
1 && 2 == op_False(1) ? 1 : op_BitwiseAnd(1 , 2) == 0
Now, if I change the op_BitwiseAnd() operator to work as a op_LogicalAnd
then I'm breaking the bitwise And. Also having a bitwise operator acting
like a logical operator seems kind of a bad inconsistency in the language
specs.
Is there something I did not understand, or is there a fix planned for this?
--Gianluca 5 1435
Wow - a real C# language question.
You're misreading the spec and mixing scalar and logical operations.
&& and & are the logical and operators.
&& short-circuits, & does not.
For scalar types, applying & to two instances performs a bitwise operation.
This is not the case for other types.
For scalar types, you cannot apply && or similar operators, as it makes no
sense to short-circuit these operations.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press www.servergeek.com/blogs/mickey
Hi Mickey, if you try to overload the conditional logical operators,
following the specs, you will always get the wrong result either in the
logical ops or the bitwise ops. It still seems to me that there is something
wrong in the specs. The only way to make it work is apply logical And in the
bitwise And operator which appears to be logically wrong.
Look at this code:
DbNumber n1 = new DbNumber(1);
DbNumber n2 = new DbNumber(1);
if (n1 && n2)
{
// never gets here because the compiler generated this:
// op_True( op_False(n1) ? n1 : op_BitwiseAnd(n 1, n2))
// and it is wrong!
}
if (n1 || n2)
{
// works ok.
}
DbNumber n3 = n1 & n2; // works ok.
DbNumber n4 = n1 | n2; // works ok.
class DbNumber { // it could be struct, it makes no difference
private int m_value;
public DbNumber(int value) {
this.m_value = value;
}
// bitwise and
public static DbNumber operator &(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_v alue & n2.m_value);
}
// bitwise or
public static DbNumber operator |(DbNumber n1, DbNumber n2) {
return new DbNumber(n1.m_v alue | n2.m_value);
}
// logical operators
public static bool operator true(DbNumber n) {
return n.m_value!=0;
}
public static bool operator false(DbNumber n) {
return n.m_value==0;
}
}
"Mickey Williams" <my first name at servergeek.com> wrote in message
news:OR******** ******@TK2MSFTN GP10.phx.gbl... Wow - a real C# language question. You're misreading the spec and mixing scalar and logical operations.
&& and & are the logical and operators. && short-circuits, & does not. For scalar types, applying & to two instances performs a bitwise
operation. This is not the case for other types. For scalar types, you cannot apply && or similar operators, as it makes no sense to short-circuit these operations.
-- Mickey Williams Author, "Microsoft Visual C# .NET Core Reference", MS Press www.servergeek.com/blogs/mickey
> DbNumber n1 = new DbNumber(1); DbNumber n2 = new DbNumber(1);
should be
DbNumber n1 = new DbNumber(1); DbNumber n2 = new DbNumber(2);
"GP" <no****@nospam. com> wrote in message
news:GN******** *********@nwrdd c01.gnilink.net ... Hi Mickey, if you try to overload the conditional logical operators, following the specs, you will always get the wrong result either in the logical ops or the bitwise ops. It still seems to me that there is
something wrong in the specs. The only way to make it work is apply logical And in
the bitwise And operator which appears to be logically wrong.
Look at this code:
DbNumber n1 = new DbNumber(1); DbNumber n2 = new DbNumber(1);
if (n1 && n2) { // never gets here because the compiler generated this: // op_True( op_False(n1) ? n1 : op_BitwiseAnd(n 1, n2)) // and it is wrong! }
if (n1 || n2) { // works ok. }
DbNumber n3 = n1 & n2; // works ok. DbNumber n4 = n1 | n2; // works ok.
class DbNumber { // it could be struct, it makes no difference
private int m_value;
public DbNumber(int value) { this.m_value = value; }
// bitwise and public static DbNumber operator &(DbNumber n1, DbNumber n2) { return new DbNumber(n1.m_v alue & n2.m_value); }
// bitwise or public static DbNumber operator |(DbNumber n1, DbNumber n2) { return new DbNumber(n1.m_v alue | n2.m_value); }
// logical operators public static bool operator true(DbNumber n) { return n.m_value!=0; } public static bool operator false(DbNumber n) { return n.m_value==0; } }
"Mickey Williams" <my first name at servergeek.com> wrote in message news:OR******** ******@TK2MSFTN GP10.phx.gbl... Wow - a real C# language question. You're misreading the spec and mixing scalar and logical operations.
&& and & are the logical and operators. && short-circuits, & does not. For scalar types, applying & to two instances performs a bitwise operation. This is not the case for other types. For scalar types, you cannot apply && or similar operators, as it makes
no sense to short-circuit these operations.
-- Mickey Williams Author, "Microsoft Visual C# .NET Core Reference", MS Press www.servergeek.com/blogs/mickey
Hello, GP.
I think, bitwise and logical semantics are a bit mixed in spec.
Perhaps, it would be more clear to define "x && y" resolution as
"T.false( x ) : false ? T.true( y )". So overloaded '&' wouldn't take
part in short circuit logic.
This definition is much simpler and it strictcly
seperates intuitive semantics:
'&' and '|' are treated as "bitwise" operators, and
'true' and 'false' overloading is left for logical stuff.
But C# design was done in another away. The reason was, may be, to exploit
customization provided by operator overloading.
Imho, your current code is inconsistent with C# spec intuitive sence,
'cause "true( x & y ) != ( true( x ) & true( y ) )".
Best regards!
Hi Alexander,
x && y === > T.false(x) ? false : T.true(y)
x || y === > T.true(x) ? true : T.true(y)
Would be the right and logical way of resolving the logical And and logical
Or. I suspect that the reason why they did not allow for the overloading of
op_LogicalAnd (apparently it is possible in manged C++) is not to allow the
developer to break the conditional evaluation of expressions in the logical
comparison.
My code is inconsistent because the specs are inconsistent IMO. I overloaded
the bitwise And as a bitwise And (which I thought was a reasonable thing to
do). In order to make the x && y work with the "mixed" specs you need to
implement the bitwise And as a logical And and therefore break the bitwise
And op. I couldn't find any other way. A logical And/Or is as different from
a bitwise And/Or as a multiplication operator is different from a system
crash. :) A logical And between 1 && 2 returns true (1) while a bitwise And
will return 0 (it's obvious, I know). Therefore the assumption that x && y
can be translated to x & y is wrong, IMHO.
--
Best regards,
Gianluca Pivato
--
Ice Tea Group, LLC www.iceteagroup.com
"Alexander Monakhov" <mo******@ispra s.ru> wrote in message
news:OO******** ******@TK2MSFTN GP09.phx.gbl... Hello, GP.
I think, bitwise and logical semantics are a bit mixed in spec.
Perhaps, it would be more clear to define "x && y" resolution as "T.false( x ) : false ? T.true( y )". So overloaded '&' wouldn't take part in short circuit logic.
This definition is much simpler and it strictcly seperates intuitive semantics: '&' and '|' are treated as "bitwise" operators, and 'true' and 'false' overloading is left for logical stuff.
But C# design was done in another away. The reason was, may be, to exploit customization provided by operator overloading.
Imho, your current code is inconsistent with C# spec intuitive sence, 'cause "true( x & y ) != ( true( x ) & true( y ) )".
Best regards! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: nobody |
last post by:
This article is posted at the request of C.W. Yang who
asked me to detail my opinion of Lisp, and for the benefit
of people like him, who may find themselves intrigued by
this language.
The opinions expressed herein are my personal ones, coming
from several years of experience with Lisp. I did plenty of
AI programming back in the day, ...
|
by: Peter Hickman |
last post by:
Well after all this discussion it would appear that a 'Python like'
language has appeared => Prothon. http://www.prothon.org/index.html
Very alpha, sort of like Python (if you consider the indenting is what
makes Python unique) and sort of Ruby in its use of prefixes to define
scoping etc (although there is no reference to this trait being...
|
by: Tristan Miller |
last post by:
Greetings.
Do any popular browsers correctly support <q>, at least for Western
languages? I've noticed that Mozilla uses the standard English
double-quote character, ", regardless of the lang attribute of the HTML
document. Will any browsers render German-style quotes or French-style
guillemots for lang="de" and lang="fr", respectively?
...
|
by: Ilkka Huotari |
last post by:
Hi,
Here are some HTML and CSS specs that I have reformatted:
http://www.visiomode.com/docs/
I hope somebody else finds them useful too... The CHMs are quite good for
checking things quickly, locally, and the web versions may be nice too.
--
Ilkka
|
by: evolnet.regular |
last post by:
I've been utilising C for lots of small and a few medium-sized personal
projects over the course of the past decade, and I've realised lately
just how little progress it's made since then. I've increasingly been
using scripting languages (especially Python and Bourne shell) which
offer the same speed and yet are far more simple and safe to...
| |
by: Rene |
last post by:
The section 7.13.2 (Compound assignment) of the C# language specification
has the following paragraph:
The term "evaluated only once" means that in the evaluation of x op y, the
results of any constituent expressions of x are temporarily saved and then
reused when performing the assignment to x. For example, in the assignment
A() += C(),...
|
by: Frank Rizzo |
last post by:
I am about to jump in to developing a heavy duty winforms app with a lot
of 3rd party (DevExpress, Infragistics) controls in VS 2005. What are
the realistic specs for a laptop, so that my development is speedy and I
am not waiting around for the complex 3rd party controls to render
themselves in the design view. Let's assume for the moment...
|
by: VK |
last post by:
It is possibly more suitable to address this question to W3C mailing
list, but I'm trying here first.
Could anyone comment on <http://www.w3.org/TR/REC-xml/#sec-rmd>
The first statement says:
"If there are external markup declarations but there is no standalone
document declaration, the value "no" is assumed."
|
by: Master Programmer |
last post by:
I heard from a friend that C++ is mainly being replaced as language of
choice for most C++ programmers around the world. I guess this makes
sence as it means that when learning you dont have to fuck around
sencelessly with low level garbage like pointers. I was going to learn
C++ but if times are changing I think I will just learn C#, it...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |