xamarin-macios icon indicating copy to clipboard operation
xamarin-macios copied to clipboard

[docs] Add design doc about how we're improving our generator support in .NET 9.

Open rolfbjarne opened this issue 3 years ago • 5 comments

This is a proposal for how to improve our generator support for protocols using C#'s default interface member and static interface member features.

Comments, ideas, etc. are welcome!

For an initial review it might be easier to read the rendered document: https://github.com/rolfbjarne/xamarin-macios/blob/net8.0-dim/docs/objective-c-protocols.md

Fixes https://github.com/xamarin/xamarin-macios/issues/13294. Fixes https://github.com/xamarin/xamarin-macios/issues/14039.

rolfbjarne avatar Oct 07 '22 16:10 rolfbjarne

Don't merge until we're branching for net8.0(and in that case re-target the PR to net8.0).

rolfbjarne avatar Oct 07 '22 16:10 rolfbjarne

I think the "Coping with C# quirks" section covers the nonsense I ran into, and "inlining" versions that cast to base is the best solution.

I'm wondering however, would that work if you are a customer and have a derived class?

// class MyWindow : UIWindow
MyWindow * w = new MyWindow ();
w.SomethingOnAndInterface ();

chamons avatar Oct 10 '22 19:10 chamons

@chamons I'm not quite sure I understand your example.

If your custom class implements the interface, then no, it wouldn't work:

public interface IProtocol {
    public void DoSomething () {} 
}

public class MyWindow : UIWindow, IProtocol {
    public void Hello ()
    {
        this.DoSomething (); // this doesn't work
        ((IProtocol) this).DoSomething (); // this works
    }
}

public class MyWindow2 : UIWindow, IProtocol {
    public void Hello ()
    {
        this.DoSomething (); // this works now, it calls the method below
    }
    public void DoSomething ()
    {
        ((IProtocol) this).DoSomething ();
    }
}

rolfbjarne avatar Oct 10 '22 19:10 rolfbjarne

I was wondering out loud about this:

Our Binding

public interface IProtocol {
    public void DoSomething () {} 
}

public class UIWindow: IProtocol {
    public void DoSomething () {
        ((IProtocol) this).DoSomething ();
    }
}

User Code

public class MyWindow : UIWindow {
// Unrelated changed
}

public void Foo (MyWindow  window)
{
    window.DoSomething()
}

And I think that'd work.

chamons avatar Oct 10 '22 19:10 chamons

@chamons yes, that should work

rolfbjarne avatar Oct 10 '22 20:10 rolfbjarne

Superseded by https://github.com/xamarin/xamarin-macios/pull/20681.

rolfbjarne avatar Jun 06 '24 08:06 rolfbjarne