Sign in to follow this  
Daerax

[.net] IamANumber

Recommended Posts

In Haskell you can constrain the types that a polymorphic function can take. For example limiting a type to anything that derives from the number type class: ZipListsAndSum :: (Num a) => [a] -> [a] -> a. I was looking and it seems there is no INumeric type (hehe) interface that guarantees that a type will implement certain numeric operations. Of the interfaces that would be appropriate the number types on .Net only implement IComparable and IEquatable, which do not cover all the operations I would expect from a number type. Any tips to overcome this?

Share this post


Link to post
Share on other sites
This is indeed a common frustration for people dealing with generics. Why Microsoft has not added more interfaces to .NET after the introduction of generics is beyond me. However, if you are using C# 3.0 and the new .NET 3.5 framework (which you should if you love new toys), there is a neat method that can help you take advantage of some built in functionality developed for LINQ and expression trees.

Instead of trying to explain it to you here, I'll just link you to the article and the download page for the provided utility library to handle it all for you. You can just extract the relevant parts and include them in your project.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
Not to derail, but I don't see how they could add interfaces for value types such as int and single. There's probably a very good reason that they didn't do that, however disappointing it may be. [crying]


Well the value types (which are actually a subtype of the general object type as well) like int and float on .NET do implement interfaces. The problem is with static methods which operators are and interfaces cannot have.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
This is indeed a common frustration for people dealing with generics. Why Microsoft has not added more interfaces to .NET after the introduction of generics is beyond me. However, if you are using C# 3.0 and the new .NET 3.5 framework (which you should if you love new toys), there is a neat method that can help you take advantage of some built in functionality developed for LINQ and expression trees.

Instead of trying to explain it to you here, I'll just link you to the article and the download page for the provided utility library to handle it all for you. You can just extract the relevant parts and include them in your project.

Cheers that has proven interesting reading.

Unfortunately while I do have C# 3.0 and .net 3.5 I must stick to .Net 2.0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
Not to derail, but I don't see how they could add interfaces for value types such as int and single. There's probably a very good reason that they didn't do that, however disappointing it may be. [crying]


Value types are allowed to implement interfaces. Even the built in types like int are just aliases for Int32, which are structs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this