Special variables: Difference between revisions
Content deleted Content added
Added Kotlin |
|||
Line 895:
<pre>":, ARGS, CPU_CORES, C_NULL, DL_LOAD_PATH, DevNull, ENDIAN_BOM, ENV, I, Inf, Inf16, Inf32, InsertionSort, JULIA_HOME, LOAD_PATH, MS_ASYNC, MS_INVALIDATE, MS_SYNC, MergeSort, NaN, NaN16, NaN32, OS_NAME, QuickSort, RTLD_DEEPBIND, RTLD_FIRST, RTLD_GLOBAL, RTLD_LAZY, RTLD_LOCAL, RTLD_NODELETE, RTLD_NOLOAD, RTLD_NOW, RoundDown, RoundFromZero, RoundNearest, RoundToZero, RoundUp, STDERR, STDIN, STDOUT, VERSION, WORD_SIZE, catalan, cglobal, e, eu, eulergamma, golden, im, pi, γ, π, φ"</pre>
(Because of Julia's multiple dispatch that allows a single function to have multiple definitions for different argument types, combined with the ability of functions in modules or variables in the local scope to safely shadow names in the global namespace, having a large global namespace is seen as a convenience rather than a problem.)
=={{header|Kotlin}}==
There are two 'special variables' that I can think of in Kotlin:
* 'it' which implicitly refers to the parameter of a lambda expression where it only has one.
* 'field' which implicitly refers to the backing field of a property within its get/set accessors.
The following program illustrates their usage:
<lang scala>// version 1.0.6
class President(val name: String) {
var age: Int = 0
set(value) {
if (value in 0..125) field = value // assigning to backing field here
else throw IllegalArgumentException("$name's age must be between 0 and 125")
}
}
fun main(args: Array<String>) {
val pres = President("Donald")
pres.age = 69
val pres2 = President("Jimmy")
pres2.age = 91
val presidents = mutableListOf(pres, pres2)
presidents.forEach {
it.age++ // 'it' is implicit sole parameter of lambda expression
println("President ${it.name}'s age is currently ${it.age}")
}
println()
val pres3 = President("Theodore")
pres3.age = 158
}</lang>
{{out}}
<pre>
President Donald's age is currently 70
President Jimmy's age is currently 92
Exception in thread "main" java.lang.IllegalArgumentException: Theodore's age must be between 0 and 125
at President.setAge(test10.kt:5)
at Test10Kt.main(test10.kt:21)
</pre>
=={{header|Lasso}}==
|