Re: .NET generics & the .NET framework: not generic enough?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 07/18/04


Date: Sun, 18 Jul 2004 16:11:54 -0500


"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:eVgzZnNbEHA.1652@TK2MSFTNGP09.phx.gbl...
> Daniel,
>> primatives alone. I do wish there was a way to express that you want
>> int,
>> uint, byte, sbyte, short, ushort, long, ulong, string, decimal, double or
>> float *only* in a generic
> Why? What if I create a BetterInt32? (aka a family of types like SqlInt32
> in
> System.Data.SqlTypes) that due to CLS requirements BetterInt32 cannot be a
> generic class, so I need to do it the "old fashion way". Remember that
> Generics are not CLS compatible, however you are free to use generics
> internal to your project, even if the public interface is CLS compatible.
>
> Why should generic math be limited to Int32 and not my BetterInt32?
I didn't *say* generic math. I said data. I write quite a few classes that
have to deal with simple data and often that data has to be interconverted
or expressed in an exceedingly simple type systems(no lists, no currency, no
custom value types, etc). I use IConvertible now and do my best to convert
bad types into valid ones, but its not the most reliable and clear method
possible.

Infact one of those links makes the point" "If you have a method that
expects to take a currency parameter, you can communicate this more clearly
by having a currency type and using it in the method declaration". I would
say it would follow that if your method cannot take BetterInt32, it isn't a
good idea to *allow* that method to take a BetterInt32, wouldn't you? Hence
a need to constrain to primative types when only primative types work.
>
> Also as I mentioned elsewhere in this thread. int uses an IL opcode for
> addition, while decimal uses an overloaded operator, so you have an
> exception from the start, so enabling a constraint for just the
> "primitives"
> is probably more work then it needs to be or should be, as it would need
> to
> support both overloaded operators & opcodes...
>
> One of the reasons for struct & overloaded operators is to enable us as
> designers to define our own "simple data". For example Martin Fowler's
> Range & Quantity patterns, both of which I would consider "simple data",
> and
> I could see a need to enable them for generic math!
>
> http://www.martinfowler.com/ap2/
>
> http://martinfowler.com/ieeeSoftware/whenType.pdf
>
> Just a thought
> Jay
>
> "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
> message news:OAh%23OnJbEHA.1764@TK2MSFTNGP10.phx.gbl...
>>
>> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
>> news:%2349uNcIbEHA.2944@TK2MSFTNGP11.phx.gbl...
>> > Daniel,
>> > I gave the link more for an example of an existing suggestion, not as a
>> > suggestion I specifically endorse.
>> >
>>
>> Sorry, I didn't mean to be harsh. I'm just rather annoyed with the
> operator
>> thing. I think I've posted that same general reply a dozen times now.
>>
>> > I like IArithmetic<T>, as it seems to be the "easier" route. (the KISS
>> > principal). If only Int32 implemented it ;-) also IArithmetic<T> is in
>> > keeping with IComparable<T>.
>> Ya, that would help termendously by helping to constrain generics to
>> primatives alone. I do wish there was a way to express that you want
>> int,
>> uint, byte, sbyte, short, ushort, long, ulong, string, decimal, double or
>> float *only* in a generic. It'd help out *alot* in data situations where
> you
>> want to create classes that handle simple data. But, alas, that is a pipe
>> dream right now. IArithmetic wouldn't help with everything, but it'd make
>> numbers simpler.
>>
>> >
>> > I agree that an actual "operator +" constraint may need to get too
>> > fancy
>> > to
>> > be useful, especially when you consider it needs to work across
> languages.
>> > But then again Microsoft has a lot of talented people working for them,
>> > and
>> > if we (the users) keep throwing it about in the publics, hopefully
>> > something
>> > usable can be created...
>> >
>> Well, I for one am not a huge fan of operator overloading in general, but
> it
>> is frustrating not to be able to perform basic mathematics with generics.
> It
>> certainly takes away the ability to *easily* do something like the stream
> in
>> STL, where the size type could be defined in the template.
>>
>>
>>
>>
>
>



Relevant Pages

  • Re: Creating IndexOf in a custom collection
    ... I started out with a custom collection by extending ... based on specific properties of the contained class, my Generics based ... Dale Preston ... public int FindIndex ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: speed of generic code in Ada (vs Java)
    ... Is the situation similar with generics in Ada, GNAT, ... autoboxing that slows things down - java has both an int as a primitve ... which is a bit like comparing Ada arrays with Ada Containers. ...
    (comp.lang.ada)
  • Re: Why cant I overload a generic method based on class/struct?
    ... In the case of generics, defaultwould also return either a null, ... delegate(int lhs, int? ... int position = rhsFirst; ... To call this with char, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: .NET generics & the .NET framework: not generic enough?
    ... I do wish there was a way to express that you want int, ... What if I create a BetterInt32? ... Generics are not CLS compatible, however you are free to use generics ... One of the reasons for struct & overloaded operators is to enable us as ...
    (microsoft.public.dotnet.languages.csharp)