1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-31 19:15:20 +00:00

Throw the exception instead of returning null.

This commit is contained in:
2017-09-21 23:05:48 -07:00
parent f385a48aa2
commit 3057f66e66
3 changed files with 31 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
package org.nwapw.abacus.context
import org.nwapw.abacus.exception.ContextException
import kotlin.reflect.KProperty
/**
@@ -16,14 +17,14 @@ import kotlin.reflect.KProperty
*/
class ChainSearchDelegate<out V>(private val valueGetter: EvaluationContext.() -> V?) {
operator fun getValue(selfRef: Any, property: KProperty<*>): V? {
var currentRef = selfRef as? EvaluationContext ?: return null
operator fun getValue(selfRef: Any, property: KProperty<*>): V {
var currentRef = selfRef as EvaluationContext
var returnedValue = currentRef.valueGetter()
while (returnedValue == null) {
currentRef = currentRef.parent ?: break
returnedValue = currentRef.valueGetter()
}
return returnedValue
return returnedValue ?: throw ContextException()
}
}

View File

@@ -43,13 +43,13 @@ open class EvaluationContext(val parent: EvaluationContext? = null,
/**
* The implementation inherited from this context's parent.
*/
val inheritedNumberImplementation: NumberImplementation?
by ChainSearchDelegate { numberImplementation}
val inheritedNumberImplementation: NumberImplementation
by ChainSearchDelegate { numberImplementation }
/**
* The reducer inherited from this context's parent.
*/
val inheritedReducer: Reducer<NumberInterface>?
val inheritedReducer: Reducer<NumberInterface>
by ChainSearchDelegate { reducer }
/**