History variables: Difference between revisions
Added Kotlin
(Added Kotlin) |
|||
Line 801:
Finally, note that the distinction between relevant history and irrelevant history depend on the application, and perhaps on the user. Also, necessity suggests that a lot of the history detail will be irrelevant for many purposes.
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
class HistoryVariable<T>(val initialValue: T) {
private val history = mutableListOf<T>()
var currentValue: T
get() = history[history.size - 1]
set(value) {
history.add(value)
}
init {
currentValue = initialValue
}
fun showHistory() {
println("The variable's history, oldest values first, is:")
for (item in history) println(item)
}
}
fun main(args: Array<String>) {
val v = HistoryVariable(1)
v.currentValue = 2
v.currentValue = 3
v.showHistory()
println("\nCurrentvalue is ${v.currentValue}")
}</lang>
{{out}}
<pre>
The variable's history, oldest values first, is:
1
2
3
Currentvalue is 3
</pre>
=={{header|Oberon-2}}==
|