Undefined values: Difference between revisions

Added Kotlin
(Added FreeBASIC)
(Added Kotlin)
Line 602:
For example, suppose it is agreed that the "sum" of the elements of an empty array should be null. Then one can simply write:
<lang jq>def sum: reduce .[] as $x (null; . + $x);</lang>
 
=={{header|Kotlin}}==
Kotlin distinguishes between nullable and non-nullable types but, as this has already been covered in the Null Object task (http://rosettacode.org/wiki/Null_object#Kotlin), there is no point in repeating it here. It is any case debatable whether 'null' is an undefined value or not since, in Kotlin, it is technically the only value of the nullable Nothing? type.
 
However, the non-nullable Nothing type which has no instances and is a sub-type of all other types can be said to represent an undefined value as expressions of this type (such as a 'throw' expression) clearly have no defined value. 'Nothing' can be used in Kotlin to represent the return type of a function which never returns, because it always throws an exception or error. This can be useful when developing an application where the implementation of a function is being left until later.
 
Generally speaking, the compiler ensures that all variables and properties have a value before they are used. However, there is one exception to this - mutable properties of non-null, non-primitive types, marked with the 'lateinit' modifier don't need to be initialized in place or in the constructor where it would be inconvenient to do so. If such a property is used before it has been initialized, a special error is thrown. During the period prior to being initialized, a 'lateinit' property can therefore be said to be undefined.
 
Here are some simple examples illustrating these points:
<lang scala>// version 1.1.2
 
class SomeClass
 
class SomeOtherClass {
lateinit var sc: SomeClass
 
fun initialize() {
sc = SomeClass() // not initialized in place or in constructor
}
 
fun printSomething() {
println(sc) // 'sc' may not have been initialized at this point
}
 
fun someFunc(): String {
// for now calls a library function which throws an error and returns Nothing
TODO("someFunc not yet implemented")
}
}
 
fun main(args: Array<String>) {
val soc = SomeOtherClass()
try {
soc.printSomething()
}
catch (ex: Exception) {
println(ex)
}
 
try {
soc.someFunc()
}
catch (e: Error) {
println(e)
}
}</lang>
 
{{out}}
<pre>
kotlin.UninitializedPropertyAccessException: lateinit property sc has not been initialized
kotlin.NotImplementedError: An operation is not implemented: someFunc not yet implemented
</pre>
 
=={{header|Lingo}}==
9,476

edits