Scope modifiers: Difference between revisions

Added Kotlin
m (C# Changed one sentence)
(Added Kotlin)
Line 587:
 
A named function definition (<code>function foo() { ... }</code>) is “hoisted” to the top of the enclosing function; it is therefore possible to call a function before its definition would seem to be executed.
 
=={{header|Kotlin}}==
As Kotlin supports both procedural and object oriented programming, the usage of scope modifiers is more complicated than in some other languages and is described in the online language reference at https://kotlinlang.org/docs/reference/visibility-modifiers.html.
 
Note in particular the following differences between Java and Kotlin:
 
1. Kotlin does not have 'package private' visibility and, if no modifier is used, the default is 'public' (or 'protected' when overriding a protected member).
 
2. Kotlin has the 'internal' modifier which means that the entity is visible everywhere within the same 'module'. A module, for this purpose, is essentially a set of source code files which are compiled together.
 
3. In Kotlin private members of an inner class are not accessible by code within an outer class.
 
4. Kotlin does not have static members as such but instead has 'companion objects' whose members can be accessed using the class name, rather than a reference to a particular object of that class. The following is a simple example of their use:
<lang scala>// version 1.1.2
 
class SomeClass {
val id: Int
companion object {
private var lastId = 0
val objectsCreated get() = lastId
}
 
init {
id = ++lastId
}
}
 
fun main(args: Array<String>) {
val sc1 = SomeClass()
val sc2 = SomeClass()
println(sc1.id)
println(sc2.id)
println(SomeClass.objectsCreated)
}</lang>
 
{{out}}
<pre>
1
2
2
</pre>
 
=={{header|Liberty BASIC}}==
9,488

edits