Sylvain Patenaude
12/04/2019, 5:53 PMexpect
/ actual
pattern?
EDIT: Looks like it's doable, but the method I want to split is not an extension method after all. So my question is rather:
Can we have only some methods of a class that are prefixed with expect
or does it have to be the full class at that point?
For instance, I would have a class where pretty much all methods would be 'common' except for a few that would need to be implemented natively.russhwolf
12/04/2019, 6:17 PMDmitri Sh
12/04/2019, 6:19 PMankushg
12/04/2019, 6:23 PMrusshwolf
12/04/2019, 6:23 PMSylvain Patenaude
12/04/2019, 6:26 PMrusshwolf
12/04/2019, 6:27 PMSylvain Patenaude
12/04/2019, 6:31 PMcompanion
methods in Kotlin terms) in an interface
? Basically my pattern would look like this:
expect object Helper : HelperCommon { // HelperCommon would either be a class or interface with static methods only
expect fun function1() // specialized natively
expect fun function2() // specialized natively
...
}
Now from another class, I would like to be able to call the functions in HelperCommon
through Helper
like this:
val someValue = Helper.functionCommon1(...) // functionCommon1 would be static/companion in HelperCommon
russhwolf
12/04/2019, 8:01 PMexpect companion object
in a non-expect class/interface. But you could have a non-expect object Helper
and have its methods delegate to internal expect
functions which then have separate implementations per-platform.Sylvain Patenaude
12/04/2019, 8:04 PMrusshwolf
12/04/2019, 8:05 PMHelper
Sylvain Patenaude
12/04/2019, 8:06 PMrusshwolf
12/04/2019, 8:07 PMSylvain Patenaude
12/04/2019, 8:08 PMinternal
would do this? I thought it was just a visibility modifier for the callers in same module.russhwolf
12/04/2019, 8:08 PMSylvain Patenaude
12/04/2019, 8:09 PMHelperNative
and HelperCommon
for instance, but if I can avoid it that would be better.russhwolf
12/04/2019, 8:10 PMobject Helper {
fun commonFunction() {
// .. common impl
}
fun specializedFunction() = specializedFunctionInternal()
}
expect fun specializedFunctionInternal() // Will have separate impl
specializedFunctionInternal
have internal
visibility so people don’t call it directly, but you don’t have tospecializedFunctionInternal()
in a HelperNative
object if you want but it’s not necessarySylvain Patenaude
12/04/2019, 8:13 PMrusshwolf
12/04/2019, 8:13 PMinternal
and have Helper
live in its own moduleSylvain Patenaude
12/04/2019, 8:14 PMspecializedFunctionInternal()
function private
? Can you have a private expect fun function()
?russhwolf
12/04/2019, 8:15 PMpublic
or internal
Sylvain Patenaude
12/04/2019, 8:16 PMprivate virtual
functions in C++. Thank you very much for your help @russhwolf.Dmitri Sh
12/04/2019, 10:31 PMankushg
12/06/2019, 10:59 PMexpect class SpecialClass
and define an extension fun SpecialClass.doSomething()
in common code.
This way, if your implementation of doSomething
is identical across platforms, you can still define it once in Common code, even though you aren't technically allowed to have it inside your expect class
declaration.
Any yup that was me 😊 Glad it was helpful!Dmitri Sh
12/07/2019, 7:38 PM