Scope/Function names and labels: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 435:
def M(x): if x == 1 then F(x) else 2 end;</lang>
If F and M are not required to be top-level functions, then both F and M could be defined as inner functions of the same enclosing function.
 
=={{header|Kotlin}}==
Functions in Kotlin can be declared at top level, within a class/object/interface or can be nested within another function.
 
Top level functions (i.e. functions declared outside any class) support three levels of visibility:
 
1. public - visible everywhere (the default if no modifier is used).
 
2. internal - visible anywhere within the same module.
 
3. private - visible only within the current file.
 
Functions declared within a class support four levels of visibility:
 
1. public - visible everywhere its class is visible (the default if no modifier is used).
 
2. internal - visible anywhere within the same module that its class is visible.
 
3. protected - visible only inside its class or any sub-classes thereof.
 
4. private - visible only inside its class.
 
Functions declared within an object (i.e. a singleton class) support the same levels of visibility as a normal class except for 'protected'.
 
Functions declared within an interface are usually public but can be private if they have a body.
 
Functions declared within another function do not have any visibility modifiers but are in scope from their point of declaration to the end of the enclosing function.
 
 
In Kotlin any expression can be marked with a label (an identifier followed by an @ sign). Currently, they are used with the 'break', 'continue' or 'return' keywords - to break out of the labelled loop, to continue with the next iteration of the labelled loop or to return from a labelled lambda expression, respectively.
 
Such labels are in scope from their point of declaration to the end of the corresponding block.
 
Labels can also be used with the 'this' keyword to distinguish between different outer scopes (classes or function receivers) where there is more than one to choose from.
 
The following program illustrates some of these usages.
<lang scala>// version 1.1.2
 
// top level function visible anywhere within the current module
internal fun a() = println("calling a")
 
object B {
// object level function visible everywhere, by default
fun f() = println("calling f")
}
 
open class C {
// class level function visible everywhere, by default
fun g() = println("calling g")
 
// class level function only visible within C
private fun h() = println("calling h")
 
// class level function only visible within C and its subclasses
protected fun i() {
println("calling i")
println("calling h") // OK as h within same class
// nested function in scope until end of i
fun j() = println("calling j")
j()
}
}
 
class D : C(), E {
// class level function visible anywhere within the same module
fun k() {
println("calling k")
i() // OK as C.i is protected
m() // OK as E.m is public and has a body
}
}
 
interface E {
fun m() {
println("calling m")
}
}
fun main(args: Array<String>) {
a() // OK as a is internal
B.f() // OK as f is public
val c = C()
c.g() // OK as g is public but can't call h or i via c
val d = D()
d.k() // OK as k is public
// labelled lambda expression assigned to variable 'l'
val l = lambda@ { ->
outer@ for (i in 1..3) {
for (j in 1..3) {
if (i == 3) break@outer // jumps out of outer loop
if (j == 2) continue@outer // continues with next iteration of outer loop
println ("i = $i, j = $j")
}
if (i > 1) println ("i = $i") // never executed
}
val n = 1
if (n == 1) return@lambda // returns from lambda
println("n = $n") // never executed
}
l() // invokes lambda
println("Good-bye!") // will be executed
}</lang>
 
{{out}}
<pre>
calling a
calling f
calling g
calling k
calling i
calling h
calling j
calling m
i = 1, j = 1
i = 2, j = 1
Good-bye!
</pre>
 
=={{header|Oforth}}==
9,485

edits