jhugman / uniffi-bindgen-react-native

A uniffi bindings generator for calling Rust from react-native
Other
0 stars 0 forks source link

Prefer interface types to concrete class names #45

Closed jhugman closed 1 month ago

jhugman commented 1 month ago

When defining a new class, uniffi-bindgen-react-native routinely generates an interface.

By convention, we're calling this FooInterface.

interface FooInterface {
    method(obj: Bar): Baz
}

class Foo implements FooInterface {
    method(obj: Bar): Baz
}

When a method on a FooInterface returns another object, or accepts an object as an argument— say Bar— prior to this commit, both the FooInterface and the concrete Foo type would use the concrete Bar.

interface FooInterface {
    method(obj: Bar): Baz
}

class Foo implements FooInterface {
    method(obj: Bar): Baz {
        // …
    }
}

This changes this, to use the interface name.

interface FooInterface {
    method(obj: BarInterface): BazInterface
}

class Foo implements FooInterface {
    method(obj: BarInterface): BazInterface {
        // …
    }
}

This makes it easy for creating mocks and dummy interfaces.

Question:

I notice that the type and the class namespaces seem to be disjoint, so this is valid:

interface Foo {
  method(bool: boolean): void;
}

class Foo implements Foo {
  method(bool: boolean): void {}
}

class Bar implements Foo {
  method(bool: boolean): void {}
}

const foo: Foo = new Bar();

Should I make these the same names? If keeping them different, what should they be?

If they are different, and we want to keep this commit, then other questions need to be thought about:

/cc @zzorba WDYT?

zzorba commented 1 month ago

So change looks good.

I think if there are situations where we NEED the object and the interface is insufficient, we should probably keep them as separate names (prefix I, suffix Type, something like that).

If we can always just refer to them through the interface, then I think the same name is fine (though by no means required, feels like the internet is of two minds as to whether doing the same name thing is cool or bad)

jhugman commented 1 month ago

I've kept the Interface split here because I couldn't implement the interface without ts wanting me to extend it.