What does the arrow (“->”) operator do in Kotlin?

前端 未结 3 1640
耶瑟儿~
耶瑟儿~ 2020-12-05 04:09

Probably a little bit broad question, but the official documentation doesn\'t even mentioning the arrow operator (or language construct, I don\'t know which phrase is more a

3条回答
  •  旧时难觅i
    2020-12-05 04:30

    The -> is a separator. It is special symbol used to separate code with different purposes. It can be used to:

    • Separate the parameters and body of a lambda expression

      val sum = { x: Int, y: Int -> x + y }
      
    • Separate the parameters and return type declaration in a function type

      (R, T) -> R
      
    • Separate the condition and body of a when expression branch

      when (x) {
          0, 1 -> print("x == 0 or x == 1")
          else -> print("otherwise")
      }  
      

    Here it is in the documentation.

提交回复
热议问题