Kotlin top-levels functions vs object function

前端 未结 5 1480
余生分开走
余生分开走 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:46

    Tip for multi-module projects:

    Use the internal visibility modifier to scope a top-level function to its containing module so that it doesn't pollute the IDE auto-complete in unrelated modules.

    // module A
    internal fun doSomething() {
        // ...
    }
    
    // module B
    doSomething() // (!) Cannot access 'doSomething': it is internal in module A
                  // Does NOT show up in module B's auto-complete
    

提交回复
热议问题