Jump to content
  • Advertisement
Sign in to follow this  
Daerax

[.net] IamANumber

This topic is 3698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

Share this post


Link to post
Share on other sites
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]

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!