-
Notifications
You must be signed in to change notification settings - Fork 0
Invocation
Jonathan edited this page Jan 12, 2018
·
2 revisions
Self-assign recursive call is a type of invocation where, the receiver is assigned to a variable x, then the function is called on variable x, and the result of the invocation is assigned again to variable x. This is only possible when the return type of function is the same as the receiver type. This happens until the infinite, meaning that, if x become null
, the invocation does not occur anymore, but the loop keeps running. The only way to stop the loop, is using predicate, such as _?
.
Example:
class Entry {
val next: Entry
}
fun allEntries(entry: Entry): List<Entry> {
return [entry, entry.next.. | _?]
}
As predicates cannot be used in anyplace, this type of invocation is only valid in expressions where predicates are allowed. Inside of lists, for example, the evaluation will be lazy, populating the list on-demand.