Can I expect a top-level class while still impleme...
# multiplatform
m
Can I expect a top-level class while still implementing nested classes?
Copy code
// MyClass is platform-specific
expect class MyClass(
  param1: Param1,
  param2: Param2,
  param3: Param3
) {
  // Builder is the same for all platforms
  class Builder {
     var param1: Param1? = null
     // ...

     fun build(): MyClass {
       return MyClass(
                param1 = param1 ?: defaultParam1,
                // ...
              )
     }
ย  }
}
r
I don't think so. You might be able to mimic the same caller syntax by making
MyClass
an interface and doing a
expect fun MyClass(...): MyClass
that returns platform-specific implementations.
m
Thanks !
That's not exactly the same as
Builders
but given that my example had only 2 parameters, that'll work nicely ๐Ÿ™‚
r
you could still have a
Builder
inside the interface
๐Ÿ‘ 1
m
๐Ÿ‘ Yup, that'll work
r
cool I won't try to type it out in Slack then ๐Ÿ˜„
๐Ÿ˜† 1
(if you dropped the builder you wouldn't need the workaround and could just go back to an
expect class
)
๐Ÿ‘€ 1
m
We have
Builders
everywhere now ๐Ÿ˜… so it's more consistent to leave it there too
Wait actually no, in this specific case, since it's 2 parameters only the Factory function should be good ๐Ÿ‘
But maybe in the future we might need
Builders
for multiplatform classes