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

Christian Sousa

06/17/2020, 2:16 PM
Is there any way of doing something like this On common.kt
Copy code
expect fun foo(font: UIFont)
I mean, having some platform specific parameter on the expect? Or do I have to resort to use
Any
? Thanks!
r

russhwolf

06/17/2020, 2:20 PM
You could also expect/actual the parameter
c

Christian Sousa

06/17/2020, 2:23 PM
Like this @russhwolf?
Copy code
expect fun foo(expect font: UIFont)
r

russhwolf

06/17/2020, 2:26 PM
no, like
Copy code
expect fun foo(font: Font) 
expect class Font
c

Christian Sousa

06/17/2020, 2:45 PM
And is there any way of then on the iOS side for example do something like
Copy code
actual class Font = UIFont
?
Because I want to use a class that already exists on the iOS/Android side
r

russhwolf

06/17/2020, 2:45 PM
yeah that's exactly what you'd do
c

Christian Sousa

06/17/2020, 2:46 PM
It gives me the following:
Expecting a top level declaration
on the AndroidMain
r

russhwolf

06/17/2020, 2:47 PM
Are you not doing a top-level class?
c

Christian Sousa

06/17/2020, 2:49 PM
I’m declarating it inside my androidMain/actual.kt
r

russhwolf

06/17/2020, 2:49 PM
oh sorry. You want to do
actual typealias Font = ...
c

Christian Sousa

06/17/2020, 2:50 PM
Why the typealias? Just curious
r

russhwolf

06/17/2020, 2:50 PM
You're typaliasing
Font
to the platform-specific type
actual class
defines a new class
c

Christian Sousa

06/17/2020, 2:50 PM
Oh.. and should I do on the
expect class Font
as well?
r

russhwolf

06/17/2020, 2:50 PM
no that's still
expect class
c

Christian Sousa

06/17/2020, 2:51 PM
Yeah, got it!
r

russhwolf

06/17/2020, 2:51 PM
c

Christian Sousa

06/17/2020, 2:58 PM
By the way, and somewhat unrelated. But is there a way of overloading
expected
methods and using an OptionalExpectation? Something like:
Copy code
@OptionalExpectation
expect fun foo(bar: String)

@OptionalExpectation
expect fun foo(foo: String)
And then on the platform specific only implementing the one we want? For example the first one being for Android and the second one for iOS
r

russhwolf

06/17/2020, 2:59 PM
No,
@OptionalExpectation
only works for annotations
👍 1
Otherwise you might try to call that function from common and not know it's missing
c

Christian Sousa

06/17/2020, 3:00 PM
Yeah, thats what I thought. Thanks a lot for your help!
👍 1