Swift bindings for .NET interfaces
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.
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)
}
}
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.