Kotlin, high-order function trouble -
i have following ho function on matrixstack
inline infix fun run(block: matrixstack.() -> any): matrixstack { push() block() pop() return } and somewhere else, have method trying return intermediate result calculated in block()
fun getsphereorbitpos(modelmatrix: matrixstack, orbitcenter: vec3, orbitaxis: vec3, orbitradius: float, orbitalpha: float): vec3 { modelmatrix run { translate(orbitcenter) rotate(orbitaxis, 360.0f * orbitalpha) var offsetdir = orbitaxis cross vec3(0.0f, 1.0f, 0.0f) if (offsetdir.length() < 0.001f) offsetdir = orbitaxis cross vec3(1.0f, 0.0f, 0.0f) offsetdir.normalize_() translate(offsetdir * orbitradius) // i'd return, top() = matrixstack.top() return (top() * vec4(0.0f, 0.0f, 0.0f, 1.0f)).tovec3() } } // return error if declare external variable
fun getsphereorbitpos(..): vec3 { var result = vec3() modelmatrix run { .. result = (top() * vec4(0.0f, 0.0f, 0.0f, 1.0f)).tovec3() // error } i following error:
expected value of type any
is there way can obtain want using matrixstack.run{ }? (because alternative manually call push(), block() , pop())
if use return inside run { } , infix function inlined, return getsphereorbitpos before pop() called, don't that.
and signature of block matrixstack.() -> any, block has return something. not using result of block inside run, change matrixstack.() -> any matrixstack.() -> unit , therefore don't expect return value block
another question is, there particular reason return this run always? if need return intermediate value , still call push() , pop(), can change function to
infix inline fun <t> run(block: matrixstack.() -> t): t { pop() val result = block() push() return result }
Comments
Post a Comment