You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
I have noticed that binding Kotlin code results in bad code. For example, I am binding okio v2.0.0
There are a few bits of code that cannot be compiled:
It tries to bind classes with a - prefix. This looks to be a way to mark them as deprecated or internal or something. This is easy to remove by: <remove-node path="/api/package/class[starts-with(@name,'-')]" />
It tries to bind methods with a - prefix, again easy to remove with: <remove-node path="/api/package/class/method[starts-with(@name,'-')]" />
It tries to bind methods with $ in the parameter names: public static final int codePointAt(@NotNull byte[] $receiver, int index)
There are other areas where it works fine, so this looks to be just in the finally blocks. I am able to fix this by setting the managedName: <attr path="/api/package[@name='okio']/class/method[parameter[1][@name='$receiver']]/parameter[1]" name="managedName">receiver</attr>
Without this adjustment, the generator results in partially corrected code:
Declaration: public static unsafe int CodePointAt (byte[] _receiver_, int index)
There are also cases where this could be improved overall:
With Kotlin, you can have extension methods (the reason for the $receiver parameter name). This means that Xamarin could potentially automatically generate extension methods - or have the option to do so.
I am working on a tool that runs after the api.xml is created to create a transforms.xml file that tries to fix and resolve members. A very rough thing that seems to be working: xamarin/XamarinComponents#642
I found some things about the way companion objects function. They are effectively singletons. Instead of static methods, we have static singletons that have instance methods.
Another thing with extension methods is that Kotlin creates non-static classes that are built up in layers. For example, the extensions for arrays are in several files:
ArraysKt which is empty, but extends ArraysKt___ArraysKt (3 underscores)
ArraysKt___ArraysKt contains many methods and extends ArraysKt___ArraysJvmKt (3 underscores)
ArraysKt___ArraysJvmKt contains a few more methods and extends ArraysKt__ArraysKt (2 underscores)
ArraysKt__ArraysKt contains a couple methods and extends ArraysKt__ArraysJVMKt (2 underscores)
ArraysKt__ArraysJVMKt contains a couple methods
All of these methods are static, so I could make the class static. But, this would break the inheritance. I could also make the objects not extend anything, but then you would never be able to find any thing because you wouldn't know to look in a type with 3 underscores. I could also rename the class to something more useful, but this is not always easy if there are many classes.
In addition to these generated names, you can also mix normal methods and extension methods in a class. This means that the extension methods are not automatically applied and have to be explicitly imported.
Then, this has to be explicitly imported before being used:
importcom.example.Test.Companion.count
One possible issue is that the companion object is NOT static, so will have to be referenced via the Companion property in C#: Test.Companion.count(...)
Our Kotlin support has been substantially improved in 16.5, largely working from this list. I think everything from this list has been added or turned out to not be workable. (For example, Kotlin extension methods are not in static classes, so we have left them as-is.)
Activity
mattleibow commentedon Aug 14, 2019
Given:
I also notice a few bad methods ending in
$defaultwhich I think may have to do with default parameters:To test, I wrote this Kotlin:
Which results in this Java code:
mattleibow commentedon Aug 15, 2019
A few more weird things - trying to bind Kotlin 1.3:
The Java side has
-:I think things are changing a bit, not sure why this is not normal overloads. Even the Java in Intellij is invalid:

mattleibow commentedon Aug 15, 2019
I found some things about the way companion objects function. They are effectively singletons. Instead of static methods, we have static singletons that have instance methods.
Docs:
Java Code
Kotlin Code
Decompiled Kotlin Code
One thing to note is that the name "Companion" is not fixed and can be changed:
Kotlin Code
Decompiled Java Code
mattleibow commentedon Aug 15, 2019
For the case of extension methods in Kotlin:
Docs:
Kotlin Code
Decompiled Java Code
mattleibow commentedon Aug 19, 2019
Kotlin also has a set of
IFunction#types that basically are the same thing asFunc<T>in .NET.For example, if you have a collection, there is an extension method
mapwhich is used line theSelectin Linq:Kotlin:
The equivalent C#:
The Kotlin binding is missing support for creating extension methods, but it can be seen here:
If we had to use this:
mattleibow commentedon Aug 19, 2019
Another thing with extension methods is that Kotlin creates non-static classes that are built up in layers. For example, the extensions for arrays are in several files:
ArraysKtwhich is empty, but extendsArraysKt___ArraysKt(3 underscores)ArraysKt___ArraysKtcontains many methods and extendsArraysKt___ArraysJvmKt(3 underscores)ArraysKt___ArraysJvmKtcontains a few more methods and extendsArraysKt__ArraysKt(2 underscores)ArraysKt__ArraysKtcontains a couple methods and extendsArraysKt__ArraysJVMKt(2 underscores)ArraysKt__ArraysJVMKtcontains a couple methodsAll of these methods are static, so I could make the class static. But, this would break the inheritance. I could also make the objects not extend anything, but then you would never be able to find any thing because you wouldn't know to look in a type with 3 underscores. I could also rename the class to something more useful, but this is not always easy if there are many classes.
In addition to these generated names, you can also mix normal methods and extension methods in a class. This means that the extension methods are not automatically applied and have to be explicitly imported.
Docs: https://kotlinlang.org/docs/reference/extensions.html#companion-object-extensions
If the extension method is in a plain class:
Then, it can't be access outside the class - which is something we can ignore since bindings are not in the class.
If the extension method is in a companion object:
Then, this has to be explicitly imported before being used:
One possible issue is that the companion object is NOT static, so will have to be referenced via the Companion property in C#:
Test.Companion.count(...)[generator] Replace hyphens in parameter names (#498)
[generator] Replace hyphens in parameter names (#498)
jpobst commentedon Feb 3, 2020
Our Kotlin support has been substantially improved in 16.5, largely working from this list. I think everything from this list has been added or turned out to not be workable. (For example, Kotlin extension methods are not in static classes, so we have left them as-is.)