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

rjhdby

12/29/2018, 10:44 AM
Hello. Is there best practics for returns platform Type from
expect
function? For example,
Drawable
for android and
UIImage
for ios?
g

gildor

12/29/2018, 12:03 PM
You need some abstraction for that that wraps both of those types. like
Image
which itself maybe expect declaration, implemented on each platform in a way that exposes platform type
i

itnoles

12/29/2018, 3:56 PM
typealias?
g

gildor

12/30/2018, 3:07 AM
Typealias may work, but you will have no common methods in this case, so it will be a type which may be references, but doesn't have any methods to use from common code. Imo real wrapper is more flexible, even of you don't have any common methods yet
a

Andrey Gromov

12/30/2018, 6:47 AM
Yes, this not for use inside common code, but for some type of declaration of intent. If we go deeper into this example, imagine what we want to declare some abstract API. Method that receive image ID and returns image resource. For android it will be
fun getImageForId(id:Int):Drawable
For iOS
fun getImageForId(id:NSString):UIImage
Of course I can declare those funcs separately, but it will be nice to declare abstract contract for
getImageForId
inside common code.
g

gildor

12/30/2018, 7:18 AM
Both typealias or real wrapper approach will work in this case. You can even abstract image id param