Kotlin top-levels functions vs object function

前端 未结 5 1479
余生分开走
余生分开走 2021-01-19 01:20

I have static function which is limited to some context eg only for docs. There are 2 alternative ways to define it as top-level function or function in an object.

1

5条回答
  •  旧巷少年郎
    2021-01-19 01:45

    object Clazz will be compiled as singleton, top-level function will be compiled as static in JVM.

    if there are no reasons for your method to be in instance, static(top-level, companion object) way would be a little bit high-performance.(ref: https://stackoverflow.com/a/11993118/5354658)

提交回复
热议问题