https://kotlinlang.org logo
#touchlab-tools
Title
# touchlab-tools
j

Jan Skrasek

10/11/2023, 8:45 AM
Cannot convert value of type 'Int?' to expected argument type 'KotlinInt?'
Am I doing something wrong?
This is the signature:
Copy code
public init(searchSessionId: String, approximateTierMarketing: Int32, tierMarketing: KotlinInt?, screen: allshared.SearchScreen)
Copy code
- (instancetype)initWithSearchSessionId:(NSString *)searchSessionId approximateTierMarketing:(int32_t)approximateTierMarketing tierMarketing:(AllsharedInt * _Nullable)tierMarketing screen:(AllsharedSearchScreen *)screen __attribute__((swift_name("init(searchSessionId:approximateTierMarketing:tierMarketing:screen:)"))) __attribute__((objc_designated_initializer));
f

Filip Dolník

10/11/2023, 8:54 AM
Hi! How does the Kotlin code look like? Btw this was generated by SKIE or is just the output of the Kotlin compiler?
j

Jan Skrasek

10/11/2023, 8:54 AM
SKIE's output
Copy code
@Serializable
@ObjCName("GeneralCloseClickedEvent")
public data class CloseClickedEvent(
	public val searchSessionId: String,
	public val approximateTierMarketing: Int,
	public val tierMarketing: Int?,
	public val screen: Screen,
)
f

Filip Dolník

10/11/2023, 8:59 AM
And is the error a result of Kotlin compilation crashing or is it in your Swift code?
j

Jan Skrasek

10/11/2023, 9:00 AM
In swift code during build.
Few lines bellow I have another one
Cannot convert value of type 'Int' to expected argument type 'Int32'
Wrapping the expression to
Int32(expresison)
makes it ok.
f

Filip Dolník

10/11/2023, 9:04 AM
yeah, this is an expected behaviour and the conversion is done by the Kotlin compiler and not SKIE. The Int? is converted to KotlinInt because it is optional (and Obj-C ints cannot be optional). There are other situations where this happens (for example with generics) You can verify that by trying to call that init without SKIE (or just create a new function with a parameter of type Int?) Similarly Kotlin
Int
is exported as
Int32
because in Kotlin
Int
is 32 bit whereas
Int
in Swift is either 32 or 64 bit depending on the platform (nowadays basically all platforms are 64 bit) Unfortunately, you will need to make these conversions manually
j

Jan Skrasek

10/11/2023, 9:08 AM
I had the feeling it worked before applying skie, I'm sorry. Basically it means that our Kotlin code should prioritize using Longs to get implicit conversion from Swift's Int (I hope it will work that way then). Ad the nullability: so this is something Skie cannot workaround? Is it at least solvable if KMP interops with Swift directly? Thanks for all the info.
f

Filip Dolník

10/11/2023, 9:22 AM
I had the feeling it worked before applying skie, I’m sorry.
No problem, and just to be sure verify that. It’s not impossible that there is some bug (the type conversion algorithm is very complex)
Basically it means that our Kotlin code should prioritize using Longs to get implicit conversion from Swift’s Int (I hope it will work that way then).
I’m not sure that would help. Those would be converted to Int64 so it would deal with the overflow issue. But you would still need to convert Int to Int64 - unless Swift does the conversion automatically if you target only 64 bit platforms (which I’m not sure it does, but maybe) But more importantly, you would shift the problem to the Kotlin codebase because some very important library functions work with integers instead of longs (for example lists).
Ad the nullability: so this is something Skie cannot workaround?
To some extend yes, it’s one of the features that we have considered for a while now, but it’s possible only in certain cases. Unfortunately basically any feature in SKIE takes a significant amount of time so we have to carefully pick on what we want to work on. So I don’t know when if ever we get to that.
Is it at least solvable if KMP interops with Swift directly?
It’s difficult to answer that because there is no public proposal for the Swift Interop yet and there are multiple options how it can be implemented - all with different tradeoffs. It should be possible to get rid of the KotlinInt class. However, it’s not possible to fully solve the Int vs Int64 problem. That’s a fundamental difference between Kotlin and Swift and those will be a problem even with the new interop (and there are unfortunately quite a lot of them).
j

Jan Skrasek

10/11/2023, 9:41 AM
thank you color
2 Views