官术网_书友最值得收藏!

Named parameters

Named parameters allow us to be explicit about naming arguments when they are passed to a function. This has the benefit that, for functions with many parameters, explicit naming makes the intent of each argument clear. This makes the call site more readable.

In the following example, we check to see whether the first string contains a substring of the second:

    val string = "a kindness of ravens" 
    string.regionMatches(14, "red ravens", 4, 6, true) 

This example will take the a kindness of ravens substring from 14 to 20, and will check that it is equal to the red ravens substring from 4 to 10. If that is confusing, then it is certainly easy to forget which parameter goes where. Therefore, if we could prefix the parameter with the name, it would be far more readable.

To use named parameters, we put the parameter name before the argument value. Here is the function call again, this time with named parameters:

string.regionMatches(thisOffset = 14, other = "red ravens",  otherOffset = 4, 
length = 6, ignoreCase = true)

This second example is more readable at the cost of being more verbose, but it is now clear what each of the parameters is meant for. The final Boolean parameter, which you might have guessed was for case sensitivity, is now obvious. Without named parameters, you must check the documentation or source code for the function that you are using to see whether there is documentation or comments on what each parameter is for.

Another benefit is that, for functions with multiple parameters of the same type, it makes errors less likely, as the values can be associated with the name. In the next example, you will see how the function accepts multiple Boolean parameters. And without named parameters, it is easy to swap arguments erroneously:

    fun deleteFiles(filePattern: String, recursive: Boolean,  ignoreCase: Boolean, deleteDirectories: Boolean): Unit

Compare the two different styles of calling this function:

    deleteFiles("*.jpg", true, true, false) 
    deleteFiles("*.jpg", recursive = true, ignoreCase = true,  deleteDirectories = false)

Did you notice that the first parameter is not named, even when the others are? When calling a function, not all parameters need to be named. The rule is simple—once a parameter has been named, all the following parameters must be named too.

Named parameters also allow the parameter order to be changed to suit the caller. For example, the following two examples are equivalent to each other:

    val string = "a kindness of ravens" 
    string.endsWith(suffix = "ravens", ignoreCase = true) 
    string.endsWith(ignoreCase = true, suffix = "ravens") 

The usefulness of this will be demonstrated in the next section on default parameters. Changing the order of parameters allows us to selectively choose which default parameters to override.

Named parameters can only be used on Kotlin-defined functions and not on Java-defined functions. This is because the Java code, when compiled into bytecode, does not always preserve the parameter names.
主站蜘蛛池模板: 三明市| 孝昌县| 竹溪县| 二手房| 汉寿县| 贡觉县| 图片| 武汉市| 比如县| 海淀区| 囊谦县| 玉门市| 秀山| 赞皇县| 疏勒县| 阿克苏市| 兴安盟| 常熟市| 西盟| 潮安县| 聊城市| 翼城县| 马山县| 云霄县| 凉山| 荔波县| 五莲县| 彝良县| 东阳市| 北辰区| 蓝山县| 石楼县| 措美县| 张家界市| 珠海市| 台东市| 石城县| 阿荣旗| 三都| 承德市| 平昌县|