All scopes, sorted from the most visible to the least:
Classes, objects and functions1 are declared in global scope. Imported file declarations are in global scope too. Global scope declarations are visible everywhere.
In class instance scope properties and functions are declared. These declarations are visible from a class instance and inside class functions.
/** this program prints:
changed
in class
in global
**/
fun main() {
= A(prop="changed")
a (a.logger()) // function called from an instance
print}
class A {
// here prop, this.func and logger are visible
= "prop"
prop fun func() { return "in class" }
fun logger(param = prop) {
(param)
print(this.func())
printreturn func()
}
}
fun func() { return "in global"}
Functions can have variable assignments and blocks. Functions change its reference arguments. All global scope declarations: other global functions, class constructors, object variables are visible inside a function body.
object Colors {
= "000000"
BLACK }
class A {}
fun change(argArr, argInstance) {
.add(2) // assuming arg is a list
arg.BLACK = "111111" // Colors.BLACK is changed
Colors.property = 3 // property will change outside of function scope
argInstance}
fun main() {
= []
arr = A()
a (arr,a )
change// here arr == [2],
// a.property == 3
}
Variables are visible from anywhere below the variable declaration.
fun scope() {
= 0
i while(i < 5) {
= 1
a = i + 1
i }
return a // here a is visible and equals 1
}
This behaviour differs from most of the languages, consequently it might change in future.
Blocks change already defined values. Variables defined inside of blocks are visible outside to the end of the function scope.
Block scope is nonexistent in some sense.
fun someFunction() {
= 5
a // a == 5
while (condition) {
= 3
a = 2
b // a == 3
}
// a == 3
// b == 2
}
Each file might contain only one function with a particular signature. Same holds for classes and objects. However, it is okay to have class and function with the same name (and no function params) - in this case class will be shadowed by that function.
class same {}
fun same() {return 1}
class difSignature {}
fun difSignature(param) {}
fun main() {
(same()) // 1
print(difSignature()) // class instance
print}
If imported file contains a declaration with the same name as existing,
class Point {
...
}
...
Current file’s declaration is prioritized.
import std.geometry2D as geom
class Point {}
fun main() {
= Point() // main.Point instance
p = geom.Point() // std.geometry2D.Point instance
pGeom }
[!warning]- Same in different imports
If two imports contain two similarly named declarations (say, both of them have
object Constants
) and current file does not have such declaration, using that declaration without file prefix is prohibited. That’s because it is ambiguous which declaration to use.
Function signature consists of:
Priority:
Local function always has priority over imported one (second step is not applicable, if there is a local function that can be called).
Top level function is prioritized over a class function2.
Then, find a function with the least number of unspecified default parameters3 (those which are not arguments in a call). For instance, if there are functions:
fun fn(a1, a2) {...}
,fun fn(a1, a2, a3 = 1) {...}
,and the call is fn(1,1)
, first function is
called.
Keep in mind, that inherited functions are simply added to the current subclass. If current class defines the same function, superclass function is shadowed. Following example might clear things:
fun main() {
= Subclass()
s (s.fn()) // "base"
print(s.shadow()) // "this is called"
log}
class Superclass {
fun fn() {return "base"}
fun shadow() {return "not called"}
}
class Subclass: Superclass {
fun fn(a = 1) {"not called, because fn without default parameters has more priority"}
fun shadow() {return "this is called"}
}
Functions can be declared in classes too.↩︎
This is due to the fact that class instance function can
be called with this.
prefix. Current class function does
not have a prefix to be called with.↩︎
Currently if there are two functions in different imports that both can be called, program will throw an error saying that it cannot pick needed function.↩︎