In my kmp iOS app, I'm trying to create a `Set<...
# multiplatform
s
In my kmp iOS app, I'm trying to create a
Set<Foo>
, where
Foo
is just a simple interface with a couple of functions defined in
commonMain
, and if I try to declare or instantiate this set in swift, I get the error "Type 'Foo' does not conform to protocol 'Hashable'". Is there a way to make my interface Hashable or is there another good way to have sets of my interface?
p
You can wrap it in a swift struct and implement the protocol there and delegate for the values
s
ok cool, I'll give that a shot. thanks!
actually I'm confused...am I wrapping the set in the struct? I have a swift classes that implement the interface, but how do I define the set?
p
Can you post the code you are talking about?
s
so in my common main, I have
Copy code
interface Foo {
    fun something()
}
and in my swift code I want to have a set of Foo, like so
Copy code
var fooSet: Set<Foo>
but this line gives me that error about Foo not being Hashable. I have implementations of Foo that I want to create the set from, in kotlin and swift, but how do I even declare the set?
p
From what I understand you can't. It doesn't confirm hashable
But you can go with SwiftFoo and implement the protocol there
s
that's really unfortunate. I don't need to implement the interface, I need the data structure to hold it. maybe I have to use a list or an array?
p
Yeah that will work
s
it's crazy to me that I can't have set's of my class in swift. Even if I define a class that has, say a function with a param of a
Set<Foo>
, I can't use it in swift? It's actually going to be pretty rough to try and change that data structure
maybe i can use expect/actual to create a delegate somehow
if I try to use KotlinMutableSet or KotlinArray, I get an error
'KotlinArray' is ambiguous for type lookup in this context
..any idea what that's about?
g
Maybe you can define hashCode/equals method in your interface? No default implementation, just the standard definition from
Any
functions. I presume that Any enforce those methods but you could pass an instance from Swift that doesn't matches those requirements.
Copy code
interface Foo {
    override operator fun equals(other: Any?): Boolean
    override fun hashCode(): Int
}
r
Try defining
extension Foo: Hashable
in Swift.
s
adding those functions didn't seem to work. if I try to define
extension Foo : Hashable
I get an error "Extension of protocol 'Foo' cannot have an inheritance clause".