https://kotlinlang.org logo
#announcements
Title
# announcements
d

deinspanjer

05/28/2017, 6:28 PM
Hi folks, I have a simple Java example and what I think is the corresponding Kotlin code, and the Kotlin one doesn’t work. Could someone help me figur this out?
This is the working Kotlin code:``` package com.jsonlogic import jdk.nashorn.api.scripting.AbstractJSObject import jdk.nashorn.api.scripting.JSObject import java.util.function.Function import javax.script.ScriptEngineManager fun main(args: Array<String>) { val m = ScriptEngineManager() val e = m.getEngineByName("nashorn") // The following JSObject wraps this list val l = mutableListOf<Any>() l.add("hello") l.add("world") l.add(true) l.add(1) val jsObj = object : AbstractJSObject() { override fun getMember(name: String?): Any? { if (name == "map") { // return a functional interface object - nashorn will treat it like // script function! return Function { callback: JSObject -> val res = l.map { // call callback on each object and add the result to new list callback.call(null, it) } // return fresh list as result of map (or this could be another wrapper) res } } else { // unknown property return null } } } e.put("obj", jsObj) // map each String to it's uppercase and print result of map e.eval("print(obj.map(function(x) '\"'+x.toString()+'\"'))"); } ```
4 Views