472,353 Members | 1,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Unconstrained Generic comparison should be allowed

Unlike C++, in Csharp you're only allowed to compare a generic type T
with null, if the method it's passed in not implementing
IComparable<Tor , IEquatable<T(still don't know why we need these
two radically similar interfaces). In other words, Csharp enforces
people to not make mistake, whereas C++ follows the philosophy "if you
make a stupid mistake, pay for it, and in the future you won't do it
again." In my opinion, comparing a type that doesn't support
comparison is a stupid mistake. All these checks and enforcements
introduced by C# is likely to burden compiler tremendously.

Seriously, why are comparisons are not allowed if mentioned interfaces
are not implemented, even if type supports the comparison?
Nov 4 '08 #1
2 2896
On Mon, 03 Nov 2008 18:22:19 -0800, puzzlecracker <ir*********@gmail.com>
wrote:
Unlike C++, in Csharp you're only allowed to compare a generic type T
with null, if the method it's passed in not implementing
IComparable<Tor , IEquatable<T(still don't know why we need these
two radically similar interfaces).
What do you mean by "compare"? It's true that if you constrain a
parameter so that it has to implement those interfaces, you can call the
members of those interfaces. But in that context, there's nothing special
about the interfaces; they are just like any other constraint. The fact
that they happen to be useful for comparisons is really just

Conversely, there are other ways to compare objects, and just because an
object implements those interfaces, that doesn't mean you'd be able to use
those other ways to compare objects. In particular, you don't get
"operator<" and "operator>" overloads for free.
In other words, Csharp enforces
people to not make mistake, whereas C++ follows the philosophy "if you
make a stupid mistake, pay for it, and in the future you won't do it
again." In my opinion, comparing a type that doesn't support
comparison is a stupid mistake. All these checks and enforcements
introduced by C# is likely to burden compiler tremendously.
For better or worse, that is the trade-off the C# design group has
chosen. That is, C# is a much more strongly typed and strict language
than C++. And yes, it's more work for the compiler, but you don't get
something for nothing.

Note also that C++ doesn't have generics. It has templates which are
similar, but not exactly the same. In particular, a C# generic is a true
type unto itself. But a C++ template is really just a preprocessing
step. C++ doesn't do _any_ kind of error-checking on the template itself;
any error-checking winds up implicit as part of the use of the template.
Seriously, why are comparisons are not allowed if mentioned interfaces
are not implemented, even if type supports the comparison?
This part I don't really understand. By definition, the type doesn't
"support the comparison" unless it implements the relevant interface. And
if the type does implement the relevant interface, then comparisons _are_
allowed.

Generics don't have any way to represent a constraint involving operator
overloads, so in that case, comparisons in a generic aren't allowed even
if the type _does_ support the comparison. So however I look at your
question, it doesn't really make sense to me.

Pete
Nov 4 '08 #2
puzzlecracker wrote:

The simplest option here would be to use Comparer<T>.Default.Compare -
job done. C# generics do not support operators, but you can hack them
toegether if you want; I've done this in the past here (using .NET 3.5):

http://www.pobox.com/~skeet/csharp/m...operators.html

(includes the comparison operators)

Interestingly, C# 4.0 promises to support operators on the new "dynamic"
type, which would be a short-cut, but probably noticeably slower than
either of the above approaches. I can't tell yet, as it isn't in the CTP
build...

Marc
[C# MVP]
Nov 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1....
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub...
3
by: kim.nolsoee | last post by:
Hi I want to use the Dictionary Classs that will load my own class called KeyClass used as TKey. Here is the code: public class Dictionary...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin...
3
by: =?Utf-8?B?R0I=?= | last post by:
I have created a small program that illustrates the problem. I would know how to address the fields that I want to sort on in the greaterThan...
4
by: Stef Mientki | last post by:
hello, I had a program that worked perfectly well. In this program modules were dynamically added, just by putting the file in a predefined...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#....
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.