kotlin-script-examples icon indicating copy to clipboard operation
kotlin-script-examples copied to clipboard

request: sample to get return value of eval

Open tateisu opened this issue 3 years ago • 0 comments

can you add sample to get return value of eval() ?

something like this:

val result : ResultWithDiagnostics<EvaluationResult> = eval(...)

result.castOrError<ResultWithDiagnostics.Success<EvaluationResult>> {
    "script evaluation failed. $it"
}.value.returnValue.castOrError<ResultValue.Value> {
    "script evaluation result is $it"
}.value.castOrError<AppConfig> { // your expected class for value
    "script returns value, but it's not AppConfig. ${it?.javaClass?.simpleName}"
}.let { config = it }

inline fun <reified T:Any> Any?.castOrError(noinline messageCreator:(Any?)->String) =
    (this as? T) ?: error(messageCreator(this))

tateisu avatar Jan 30 '22 10:01 tateisu