TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Reply
 
LinkBack Thread Tools Display Modes
Old 15-12-2007, 04:08 AM   #11
Marc Gravell
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

> (I hasten to add that while I understand the principle of the thing, I
> don't understand Marc's code...


For the group's benefit - that doesn't mean that it is hard to use -
consider the following, for example, that works for *any* type that
declares a suitable add operator (even your own bespoke
"ComplexInt32"). If it can't find the operator it will throw an
InvalidOperationException at runtime - but it will run almost as
quickly as a specific overload implementation:

public static T Sum<T>(this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException();
T sum = Operator<T>.Zero; // not the same as default(T);
think "int?"
foreach (T value in source)
{
if (value != null)
{ // ignore nulls (think "int?"; recall 0 + null =
null)
sum = Operator.Add(sum, value);
}
}
return sum;
}
  Reply With Quote
Old 15-12-2007, 04:09 AM   #12
Peter Duniho
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

On Fri, 14 Dec 2007 13:07:11 -0800, Jon Skeet [C# MVP] <skeet@pobox.com>
wrote:

>> Oh well, it was worth a try. I'm a bit confused though: I thought
>> that for value types, a new instance of the class was generated for each
>> variation of the generic class.

>
> The JIT will spit out new native code for each value type, but the
> compiler keeps it in a single generic type.


Ahh...I thought it was the C# compiler doing that work. Okay then...I can
see how if the C# compiler always keeps a single generic type, why it then
has the limitations Ben describes. Thanks!

Pete
  Reply With Quote
Old 15-12-2007, 04:09 AM   #13
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

Chris Shepherd <chsh@nospam.chsh.ca> wrote:
> I've wondered this for a while now, and maybe the reasoning is blatantly
> obvious and I'm a fool or it's been asked and answered before, but is
> there a reason why the numeric types don't implement an interface that
> defined the operators for math as required?
>
> At least then we could use a generic constraint (amongst other things)
> for issues like this.


Aside from anything else, operators are static, so can't be part of an
interface implementation.

I think with a big change to a lot of stuff it would be possible, but
MS hasn't got round to it yet...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
  Reply With Quote
Old 15-12-2007, 05:09 AM   #14
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

Marc Gravell <marc.gravell@gmail.com> wrote:
> > (I hasten to add that while I understand the principle of the thing, I
> > don't understand Marc's code...

>
> For the group's benefit - that doesn't mean that it is hard to use


Sorry, yes, I should have made that clear. It's incredibly easy to use
- the implementation is just slightly confusing, due to being in
expression trees, which are fundamentally hard to get your head round
(well, so I find).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
  Reply With Quote
Old 15-12-2007, 05:11 AM   #15
Marc Gravell
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

> is there a reason why the numeric types don't implement an interface that
> defined the operators for math as required?


A number of such have been proposed (as discussion points on forums
like this), but they all suffer from the fact that our current
understanding of operators is much more flexible than a standard
interface would permit. Often there are operators against a range of
different types with different outputs, so the signatures of the
methods would be tricky - it isn't always T with "T Operator(T arg)".

The nature of maths is an issue too - the operations supported by
different number systems are not consistent; "uint", for instance,
doesn't define "negate" (additive inverse). Likewise the int-based
numbers don't define "reciprocol" (multiplicative inverse), nor do non-
square matrices. Division is better supported, but still has issues.
This means that either you'd have a ridiculous number of (probably
generic) interfaces (with 1, maybe 2 methods each), or you'd have a
few interfaces (with lots of simple T=>T methods) where you already
know that some methods are reasonably likely to throw "NotSupported"
or "InvalidOperation" (because they simply don't make sense for that
number system).
The first option (lots of interfaces) becomes very restrictive and
almost impossible to use; the second (untrustworthy interfaces)
doesn't buy an awful lot of *true* safety anyway.

Thus I propose that the duck-typing (to borrow Jon's usage) is
certainly no worse than anything else we can currently propose; but it
works, exists today, and is as quick as you'd like.

For completeness, I have also seen a previous reply on this subject
that the number of interfaces, members, virtcalls, etc (against the
primatives) would have a significant impact on things like CF. I don't
know about the truth of this claim, however.

Marc
  Reply With Quote
Old 15-12-2007, 07:22 AM   #16
My interest
Guest
 
Posts: n/a
Re: Generics, use +/-/* operator etc

Is there anyway to use Reflection to invoke operator + alike? I
tried, but failed to make it work.
  Reply With Quote
Reply

Thread Tools
Display Modes


Google
 


All times are GMT +5.5. The time now is 04:15 AM.


vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2008, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional