Kotlin - Scope Functions

  • The purpose is to execute a block of code within the context of an object.
  • When we call such a function on an object with a lambda expression provided, it forms a temporary scope.
  • In this scope, we can access the object without its name. Such functions are called scope functions.
  • The scope functions do not introduce any new technical capabilities, but they can make our code more concise and readable. but overuse of it sometimes makes it complex and hard to read.

There are five of them: let, run, with, apply, and also.

There are two main differences between each scope function:

  • The way to refer to the context object. (this, it) as val
  • The return value. (Object, lamda result)

要查看或添加评论,请登录

社区洞察

其他会员也浏览了