The coerceIn function

The coerceIn function verifies whether a given value is within the range. If the value is less than the starting point, the first element of the range will be returned. Similarly, if the value is higher than the ending point, the last element of the range will be returned. If the value is within the range, however, that value itself will be returned:

var i = 11.coerceIn(10..20)
println(i)

Here, coerceIn will return 11, because 11 is within the range. In the following example, coerceIn will return 10 because 9 is outside the range and less than the starting point:

i = 9.coerceIn(10..20)
println(i)

Similarly, in the following example, coerceIn will return 20 because 21 is outside the range and is greater than the last element:

i = 21.coerceIn(10..20)
println(i)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.135