Kotlin / dukat

Converter of <any kind of declarations> to Kotlin external declarations
552 stars 42 forks source link

Generic params in lambdas are passed as is and causing compilation failure #276

Open Schahen opened 4 years ago

Schahen commented 4 years ago

This code

interface SomeOtherInterface {
  ping: <T>(arg: T) => T;
}

is translated to:

external interface SomeOtherInterface {
    var ping: (arg: T) -> T
}

Which won't compiled because we can not have standalone generic params in members. Actually, I am aware of this problem in general and we convert generated interfaces accordingly, for instance:

interface SomeOtherInterface {
   cached: {
    <T extends (...args: any[]) => any>(func: T) => any): T;
  };  
}

is converted to:

external interface `T$0`<T> {
    @nativeInvoke
    operator fun <T : (args: Array<Any>) -> Any> invoke(func: T, resolver: (args: Array<Any>) -> Any = definedExternally): T
}

external interface SomeInterface {
    var cached: `T$0`<T>
}

It's just that we need to generalise this practice

Schahen commented 4 years ago

more complete example

interface SomeOtherInterface {
   ping: <T>(arg: T) => T;
   pong<D>(arg: D): D;
   cached: {
    <T extends (...args: any[]) => any>(func: T) => any): T;
  };  
}
uliluckas commented 3 years ago

A good package for tracking success is fp-ts which has tons of these issues.

wfhartford commented 3 years ago

I'm running into this issue running dukat against @types/google-protobuf. The following two functions from the Message class produce kotlin code which does not compile:

  serializeBinaryExtensions(
    proto: Message,
    writer: BinaryWriter,
    extensions: { [key: number]: ExtensionFieldBinaryInfo<Message> },
    getExtensionFn: <T>(fieldInfo: ExtensionFieldInfo<T>) => T): void
  readBinaryExtension(
    proto: Message,
    reader: BinaryReader,
    extensions: {[key: number]: ExtensionFieldBinaryInfo<Message>},
    setExtensionFn: <T>(fieldInfo: ExtensionFieldInfo<T>,
    val: T) => void): void

If someone could point me in the right direction, I would be happy to try to fix this issue.