473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3001
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. as a developer, it is important, from a bottom line...
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 declarations in two places, it's obviously not generic....
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 MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
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 { public static void Main()
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 and then I would like to get an unmanaged pointer to...
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 comparison. Anybody who knows?? using System; ...
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 directory. Now one of the interface mechanisms was...
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 the "T" type of the instanced class? ie, ...
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#. Here's some simplified code (sorry for strange...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
tracyyun
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 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.