Bug 58844 - [generator] Methods with out parameters can't use interface protocols
Summary: [generator] Methods with out parameters can't use interface protocols
Status: CONFIRMED
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools (show other bugs)
Version: XI 10.99 (xcode9)
Hardware: PC Mac OS
: Normal enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-08-17 11:53 UTC by Matthew Leibowitz
Modified: 2017-08-18 19:48 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: No
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 58844 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Matthew Leibowitz 2017-08-17 11:53:54 UTC
This is the Objective-C:

    @protocol TIPImageCodec <NSObject>
    @end

    @interface TIPImageCodecCatalogue : NSObject
        - (void)removeCodecForImageType:(NSString *)imageType removedCodec:(out id<TIPImageCodec> *)codec;
    @end

The equivalent C#:

    interface ITIPImageCodec : INativeObject { }
    
    [Protocol, Model]
    [BaseType(typeof(NSObject), Name = "TIPImageCodec")]
    interface TIPImageCodec : INativeObject
    {
    }
    
    [BaseType(typeof(NSObject), Name = "TIPImageCodecCatalogue")]
    interface TIPImageCodecCatalogue
    {
        [Export("removeCodecForImageType:removedCodec:")]
        void RemoveCodec(string imageType, [NullAllowed] out ITIPImageCodec codec);
    }

However, I get a compiler error with the generated `RemoveCodec`:

    The type 'TwitterImagePipeline.ITIPImageCodec' cannot be used as type parameter 'T' in the 
    generic type or method 'Runtime.GetNSObject<T>(IntPtr)'. There is no implicit reference 
    conversion from 'TwitterImagePipeline.ITIPImageCodec' to 'Foundation.NSObject'.

It works with the `TIPImageCodec` type.
Comment 1 Sebastien Pouliot 2017-08-18 02:02:06 UTC
That sounds incorrect

> @protocol TIPImageCodec <NSObject>

should *not* be bound like

    interface ITIPImageCodec : INativeObject { }

    [Protocol, Model]
    [BaseType(typeof(NSObject), Name = "TIPImageCodec")]
    interface TIPImageCodec : INativeObject
    {
    }

but as

    interface ITIPImageCodec { }

    [Protocol]
    interface TIPImageCodec
    {
    }

i.e.

- the `[Model]` should not be used unless you're sure it's a good match for a base type. It often is not (and confuse people / create bad API that enforce a base type when the interface is what's really needed). However that should work too (it's an API design issue);

- bindings should not implement `INativeObject`, that's for non-ObjC type (and manual bound p/invoke). When you deal with a protocol then you know it's ObjC (NSObject-based). That code should generate/compile fine;
Comment 2 Matthew Leibowitz 2017-08-18 14:49:50 UTC
That was a combination of old code and pseudocode. But that is not the issue here. The out parameter is.

I know it works with an NSObject-based type, but `TIPImageCodec` is something like a delegate type. The method that I am trying to bind is:

  - (void)removeCodecForImageType:(NSString *)imageType 
                     removedCodec:(out id<TIPImageCodec> *)codec;

As you can see, it takes a `id<TIPImageCodec>` which (I believe) should be able to accept a managed type:

  class MyCodec : NSObject, ITIPImageCodec { }

And I should be able to do this:

  var catalogue = TIPImageCodecCatalogue.SharedInstance;

  // add ITIPImageCodec
  var myCodec = new MyCodec();
  catalogue.SetCodec(myCodec, "com.microsoft.bmp");

  // remove ITIPImageCodec
  ITIPImageCodec codec;
  catalogue.RemoveCodec("com.microsoft.bmp", out codec);

To get this to work, I did this now:

  // API definition
  void RemoveCodec(string imageType, [NullAllowed] out IntPtr codec);

  // extras
  public void RemoveCodec(string imageType, out ITIPImageCodec codec)
  {
    IntPtr codecPtr;
    RemoveCodec(imageType, out codecPtr);
    codec = Runtime.GetINativeObject<ITIPImageCodec>(codecPtr, false);
  }
Comment 3 Sebastien Pouliot 2017-08-18 19:48:33 UTC
Yes, make sense (without the `INativeObject`) and it seems this is not a case we ever hit with Apple's API (grep returned nothing and trying it confirm the generated code won't compile).

Your workaround is fine, that's what the generator should be generating.