Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

why do extension functions ignore the where clause?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
not_a_commie - 04 Apr 2008 16:09 GMT
I have here some example code (below) that I believe should work fine.
However, I get the error

Error    1    The call is ambiguous between the following methods or
properties:
'TestExtensions.ExtensionsOne.AddFive<TestExtensions.One>(TestExtensions.One)'
and
'TestExtensions.ExtensionsTwo.AddFive<TestExtensions.One>(TestExtensions.One)'
C:\workspace\TestProjects\TestExtensions\TestExtensions\Program.cs
44    4    TestExtensions

It's as though the resolution of the extension functions ignores the
where clause on the functions. As a workaround I can call the function
statically. I just think this should be resolvable.

namespace TestExtensions
{
    public interface IOne
    {
        int Int { get; set; }
    }

    public class One: IOne
    {
        public int Int { get; set; }
    }

    public interface ITwo
    {
        int Int { get; set; }
    }

    public class Two : IOne
    {
        public int Int { get; set; }
    }

    public static class ExtensionsOne
    {
        public static void AddFive<T>(this T obj) where T : IOne
        {
            obj.Int += 5;
        }
    }

    public static class ExtensionsTwo
    {
        public static void AddFive<T>(this T obj) where T : ITwo
        {
            obj.Int += 5;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var one = new One();
            one.AddFive();
        }
    }
}
not_a_commie - 04 Apr 2008 16:12 GMT
I should add that using classes instead of interfaces in the where
clause makes no improvement to the problem.
Jon Skeet [C# MVP] - 04 Apr 2008 16:20 GMT
> I have here some example code (below) that I believe should work fine.

Why do you believe it should work? I can't see anything in the overload
resolution section of the spec (7.4.3.3 is the relevant bit here, I
believe) which suggests that generic constraints should make one
conversion better than another.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.