https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

Stefan Oltmann

07/02/2021, 2:03 PM
Does epect & actual work with interfaces? I want to define a Interface like so:
Copy code
expect interface Image

expect interface ImageLoader {

    fun loadFullImage(photo: Photo): Image

    fun loadThumbnailImage(photo: Photo): Image

}
And for Android it should load a "ImageBitmap"
Copy code
actual interface Image // ???

actual interface ImageLoader {

    actual fun loadFullImage(photo: Photo): ImageBitmap

    actual fun loadThumbnailImage(photo: Photo): ImageBitmap

}
And for iOS "Image" should be "UIImage" of course. Is that possible?
1
o

Osman Saral

07/02/2021, 2:14 PM
I'm not sure but you can use something like this.
Copy code
expect class Image

//for Android
actual typealias Image = Bitmap

//for iOS
actual typealias Image = UIImage
after that, you can use common Interface functions to return that typealias
s

Stefan Oltmann

07/02/2021, 2:15 PM
I will try that. Thank you! 🙂
Ok, "actual typealias" is indeed the mechanic I'm looking for. So thank you for pointing me to that. If I try to define "actual typealias MyImage = NSImage" for my macOS target it gives an error. "actual typealias MyImage = UIImage" seems to be fine. Conflicting overloads: public final actual fun <init>(data: NSData): MyImage /* = NSImage / defined in com.sampleapp.shared.MyImage, public final actual fun <init>(contentsOfFile: String): MyImage / = NSImage / defined in com.sampleapp.shared.MyImage, public final actual fun <init>(contentsOfURL: NSURL): MyImage / = NSImage / defined in com.sampleapp.shared.MyImage, public final actual fun <init>(byReferencingFile: String): MyImage / = NSImage / defined in com.sampleapp.shared.MyImage, public final actual fun <init>(byReferencingURL: NSURL): MyImage / = NSImage / defined in com.sampleapp.shared.MyImage, public final actual fun <init>(dataIgnoringOrientation: NSData): MyImage / = NSImage */ defined in com.sampleapp.shared.MyImage
Should I file a ticket or do I just use this wrong?
4 Views