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

Multiple return values

Let's say that we want to calculate both the positive and negative square roots of an integer. We could approach this problem by writing two different functions:

   fun positiveRoot(k: Int): Double { 
     require(k >= 0) 
     return Math.sqrt(k.toDouble()) 
   } 
 
   fun negativeRoot(k: Int): Double { 
     require(k >= 0) 
     return -Math.sqrt(k.toDouble()) 
   } 

Each function returns one of the roots of k. Another approach might be to return an array so that we only have to invoke one function:

    fun roots(k: Int): Array<Double> { 
      require(k >= 0) 
      val root = Math.sqrt(k.toDouble()) 
      return arrayOf(root, -root) 
    } 

Now, this example returns both the positive and negative roots together in a list. However, we do not know from the return type whether the positive root or negative root is at position 0. We will have to hope that the documentation is correct; if not, inspect the source code. We could improve this further by using a class with two properties that name each of the roots and then return this instead. See the following code snippet as an example:

    class Roots(pos: Double, neg: Double) 
 
    fun roots2(k: Int): Roots { 
      require(k >= 0) 
      val root = Math.sqrt(k.toDouble()) 
      return Roots(root, -root) 
    }

Now, the return value is a class and the two properties make it clear which is the positive root and which is the negative root. This has the advantage of having named fields, so we could be sure which is the positive root and which is the negative root. An alternative to a custom class is using the Kotlin standard library Pair type. This type simply wraps two values, which are accessed through the first and second fields:

    fun roots3(k: Int): Pair<Double, Double> { 
      require(k >= 0) 
      val root = Math.sqrt(k.toDouble()) 
      return Pair(root, -root) 
    } 

This is most often used when it is clear what each value means. For example, a function that returned a currency code and an amount would not necessarily need to have a custom class, as it would be obvious which was which. Furthermore, if the function were a local function, you might feel that creating a custom class would be unnecessary boilerplate for something that will not be visible outside of the member function. As always, the most appropriate choice will depend on the situation.

There is a three-value version of Pair, which is appropriately named Triple.

We can improve this further by using destructuring declarations on the caller site. Destructuring declarations allow the values to be extracted into separate variables automatically:

     val (pos, neg) = roots3(16) 

You can see that the variables are contained in a parenthesis block; the first value will be assigned to the positive root, and the second value will be assigned to the negative root. This syntactic sugar works with any object that implements a special component interface. The built-in Pair type, and all data classes, automatically implement this interface. There will be more on this mechanism in Chapter 9, Data Classes.

主站蜘蛛池模板: 綦江县| 南安市| 得荣县| 齐河县| 凉城县| 赤壁市| 乌拉特后旗| 明光市| 于都县| 双辽市| 翼城县| 疏附县| 万源市| 石林| 伊通| 浠水县| 阿图什市| 临夏市| 垦利县| 隆化县| 中江县| 南京市| 三门峡市| 大石桥市| 定结县| 勃利县| 紫阳县| 乐陵市| 修文县| 虹口区| 宁陵县| 祁连县| 寿阳县| 辰溪县| 台中市| 梅河口市| 三亚市| 凌源市| 资溪县| 鄢陵县| 蕉岭县|