I have a Kotlin based question, which I've posted ...
# announcements
g
I have a Kotlin based question, which I've posted to stackoverflow. Help is much appreciated: https://stackoverflow.com/questions/54520330/abstract-factory-and-inheritance-kotlin
h
If you want all factory instances to inherit from
Foo
, then you could just do
abstract class PlantFactory : Foo()
👍 1
r
@guppyfaced what does that encoding offer over plain kotlin sealed classes?. All that inline reified and indirection seems unnecessary to construct instances of a family of types.
g
About inline and reified - I understood that these would would not be immediately compiled, meaning that they wouldn't take up memory unless called and they would be destroyed after executing. @raulraja
r
That might be the case but it is still unnecessary. As I read that pattern it does nothing that sealed classes don't support natively with more precision. Seems like an encoding of a Java pattern that is unnecessary in Kotlin unless there is something there I'm not seeing
g
You might be right - I haven't used sealed classes with Kotlin. The answer in stackoverflow did recommend using sealed classes. How would a sealed class be used in this case? @raulraja
r
@guppyfaced I mean this does the same:
Copy code
sealed class Plant {
  object Orange : Plant()
  object Apple : Plant()
  companion object {
    fun orange(): Orange = Orange
    fun apple(): Apple = Apple
  }
}

fun main() {
  val apple: Plant = Plant.apple()
  val orange: Plant = Plant.orange()
  println(apple to orange)
  //(Apple@3fee733d, Orange@5acf9800)
}
and even the companion is boilerplate if you are fine with your constructors
This also does the same:
Copy code
sealed class Plant 
object Orange : Plant()
object Apple : Plant()

fun main() {
  val apple: Plant = Apple
  val orange: Plant = Orange
  println(apple to orange)
  //(Apple@3fee733d, Orange@5acf9800)
}
👍 1
g
Wow, that's really concise.
Give me a minute to try it out
Perfect! Thanks
👍 1