Explain this Kotlin function structure

夙愿已清 提交于 2019-12-12 16:44:54

问题


I am working with this Kotlin function. I know that we have a function called mPasswordView!!.setOnEditorActionListener, that take parameter TextView.OnEditorActionListener, but what's that after it? we have curly brackets inside parameter?

mPasswordView!!.setOnEditorActionListener(TextView.OnEditorActionListener { textView, id, keyEvent ->
    if (id == R.id.login || id == EditorInfo.IME_NULL) {
        attemptLogin()
        return@OnEditorActionListener true
    }
    false
})

回答1:


The feature used in your example is a SAM constructor. The setOnEditorActionListener listener takes an OnEditorActionListener as its parameter. This interface only has a single method that you have to implement, which makes it a Single Abstract Method (SAM) interface.

The full syntax for using this method in Java would be:

mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        attemptLogin();
        return true;
    }
});

The one-to-one conversion to Kotlin would give you:

mPasswordView.setOnEditorActionListener(object: TextView.OnEditorActionListener{
    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        attemptLogin()
        return true
    }
})

Kotlin, however, allows you to use methods that take SAM interfaces as their parameter with a more concise syntax, by passing in a lambda instead. This is called a SAM conversion:

mPasswordView.setOnEditorActionListener { v, actionId, event ->
    attemptLogin()
    true
}

SAM conversions automatically determine which interface this lambda corresponds to, but you can specify it explicitly by using something called a SAM constructor, this is what's in your sample code. A SAM constructor returns an object that implements the given interface, and makes the lambda you've passed to it its single method's implementation.

mPasswordView.setOnEditorActionListener( TextView.OnEditorActionListener { v, actionId, event ->
    attemptLogin()
    true
})

This is redundant in this specific situation, because there is only one method called setOnEditorActionListener. But if there were multiple methods with this same name, that took different interfaces as parameters, you could use the SAM constructor to specify which overload of the method you want to call.

Official docs about SAM conversions




回答2:


So with other words the structure is the following (I will just leave this here is somebody needs help understanding it):

function_A ( parameters_A { override_parameters_B -> override_function_B } )    

where overriden_function_B is a constructor of the object, called parameters_A.



来源:https://stackoverflow.com/questions/43574172/explain-this-kotlin-function-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!