beyondnet icon indicating copy to clipboard operation
beyondnet copied to clipboard

Swift bindings for .NET interfaces

Open lemonmojo opened this issue 2 years ago • 2 comments

Consists of two parts:

  • A public Swift protocol with just the method declarations
  • An internal Swift protocol Impl that is used when a .NET API returns an instance of an interface

The user of an API that returns an interface instance doesn't need to know about the Impl class and so it's internal.

Since interface can inherit from other interfaces we need that too.

And of course we need to mark classes that adopt interfaces as such in the Swift bindings.

Every interface mapped to an Swift protocol will need to be derived from DNObject so that we get access to the handle property and initializer, etc.

lemonmojo avatar Apr 20 '23 17:04 lemonmojo

Example:

C# Interfaces:

public interface ILifeform {
  int getAge();
}

public interface IHuman: ILifeform {
  string getName();
}

Swift Protocols:

public protocol ILifeform: DNObject {
  func getAge() throws -> Int32
}

public protocol IHuman: ILifeform {
  func getName() throws -> System_String?
}

Swift Impls:

internal class ILifeform_Impl: DNObject, ILifeform {
  func getAge() throws -> Int32 {
    return ILifeform_GetAge(self.__handle)
  }
}

internal class IHuman_Impl: ILifeform_Impl {
  func getName() throws -> System_String? {
    return IHuman_GetName(self.__handle)
  }
}

lemonmojo avatar Apr 20 '23 18:04 lemonmojo

That won't work because we can't do multiple inheritance for the Impls (in case an interface conforms to multiple other interfaces).

What we can do instead is make the Impls completely empty (apart from the typeOf, typeName, destroy, ... stuff) and provide default protocol implementations as extensions.

lemonmojo avatar Apr 21 '23 07:04 lemonmojo