[.net] IamANumber

Started by
4 comments, last by Mike.Popoloski 15 years, 9 months ago
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?
Advertisement
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.
Mike Popoloski | Journal | SlimDX
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]
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.
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.
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.
Mike Popoloski | Journal | SlimDX

This topic is closed to new replies.

Advertisement