有人可以解释一下Kotlin的表达方式吗? [英] Can someone explain this Kotlin expression?

查看:84
本文介绍了有人可以解释一下Kotlin的表达方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Kotlin学习android开发.作为主要的Web开发人员,我对这两种技术都是陌生的.

I am learning android development using Kotlin. Being primarily a web developer, I am new to both technology.

在寻找API-29连接解决方​​案(API-29中不推荐使用NetworkInfogetActiveNetworkInfo)时,我遇到了该表达式以进行变量声明.

While looking for API-29 Connectivity Solution (NetworkInfo and getActiveNetworkInfo are deprecated in API-29), I ran across this expression for variable declaration.

 protected var callbackFunction: ((Boolean) -> Unit) = {}

 abstract fun startListening(callback: (Boolean) -> Unit)

我将如何解释?我是说变量callbackFunction是布尔类型还是单元类型.或者是其他东西.在该抽象函数中,回调类型是Boolean还是Unit或其他?

How would I interpret this? Do I say, variable callbackFunction is of type Boolean, Or of type Unit. Or something else. In that abstract function, is callback type of Boolean or Unit or something else?

关于花括号,我也有些困惑.这是否意味着变量是由一个空的匿名函数(类似于JavaScript)初始化的,还是完全不同的概念?

Also I am a bit confused, about the curly braces. Does it mean variable is initialized by an empty anonymous function (something like in JavaScript) or is it something entirely different concept?

推荐答案

Kotlin支持函数式编程,这意味着在Kotlin中将函数视为一等公民.就像其他任何变量一样,它们可以传递或从函数中返回.

Kotlin supports functional programming, which means that functions are treated as first class citizens in Kotlin. They can be passed around or returned from functions, just like any other variable.

所以

 protected var callbackFunction: ((Boolean) -> Unit) = {}

callbackFunction是实现功能interface的变量.您可以有效地说这是lambda.要定义lambda 类型,请在:之后使用Boolean作为参数,并在箭头->之后表示它返回Unit(就Java而言为void). =之后,我们给它加上花括号.

callbackFunction is a variable that implements a Function interface. You can effectively say that this is a lambda. To define the lambda type, after :, it takes a Boolean as a parameter and after arrow -> means that it returns Unit which is void in terms of Java. After the =, we give it a body with the curly braces.

这里是调用时的样子:

 class Test {
    var callbackFunction: ((Boolean) -> Unit) = {}
    fun higherOrderFunction(block: (Boolean) -> Unit) {
         block(true)
     }
   }

   fun main() {
      Test().higherOrderFunction { it ->
          println(it) //print true
      }  
   }

如果我们看一下字节码,就可以看到它在做什么:

If we look at the bytecode, we can see what it's doing under the hood:

  public final class Test {
  @NotNull
   private Function1 callbackFunction;  

  @NotNull
  public final Function1 getCallbackFunction() {
    return this.callbackFunction;
  }

   public final void setCallbackFunction(@NotNull Function1 var1) {
    Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
    this.callbackFunction = var1;
  }

   public Test() {
     this.callbackFunction = (Function1)null.INSTANCE;
   }
 }

检查callbackFunctionFunction1类型的方式.现在,让我们看一下Function1的声明.

Check how the callbackFunction is of Function1 type. Now, let see the declaration of Function1.

/** A function that takes 1 argument. */
public interface Function1<in P1, out R> : Function<R> {
  /** Invokes the function with the specified argument. */
  public operator fun invoke(p1: P1): R
}

您可以将callbackFunction变量传递给startListening()抽象方法,因为此方法接受一个以布尔作为参数并返回Unit的lambda.因此,callbackFunction与abstract方法的参数匹配,我们现在可以传递它.

You can pass callbackFunction variable to startListening() abstract method because this method accepts a lambda that takes a Boolean as a paramter and returns Unit. So, callbackFunction matches the parameter of the abstract method and we can now pass it.

我将如何解释?

How would I interpret this?

  • 您可以说,"callbackFunction是一个以Boolean为参数且不返回任何内容的lambda."
  • 在该抽象函数中,回调类型是Boolean还是Unit或其他?

    In that abstract function, is callback type of Boolean or Unit or something else?

    • 再次需要一个lambda,它需要一个布尔值作为参数并且什么也不返回.
    • 关于花括号.这是否意味着变量是由一个空的匿名函数初始化的?

      about the curly braces. Does it mean variable is initialized by an empty anonymous function?

      • 就像我们可以在abstract声明中使用花括号以使其具体实现一样,我们可以在此处执行相同的操作.
        • Just like we can give curly braces to an abstract declaration to give it concrete implementation, we can do the same here.
        • 这篇关于有人可以解释一下Kotlin的表达方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆