I thought I could just use obj?.let { MyObj(obj) }...
# announcements
d
I thought I could just use obj?.let { MyObj(obj) }, but I think this is giving me a function like a Supplier rather than the actual object.
f
deinspanjer: How about this?
Copy code
class MyObject(val arg: Any){
        
        companion object{
            fun create(arg : Any?) = arg?.let{MyObject(arg)}
        }
    }
g
i think you want arg?.let{}
let works just like map{} on any single object
d
Okay, I read the user guide on companion objects but I need to re-read cause I obviously don’t grok it yet.
f
yep
updated
if you’re used to the jvm it’s basically a static method
d
I tried changing it to a dumb if (this == null) null else wrap, and my problem still persists so this let isn’t the issue. 😞
After this wrapped object is handed off to the Nashorn engine, it tries to get a member of the wrapped object, but it is getting a
Function0<java.io.Serializable>
instead of a string. so I’ve done something nasty somewhere
f
have you tried debugging/printing it after creation but before passing it to Nashorn?
g
i am pretty sure that it should come out as an instance of MyObj
d
Trying. it is hard. I need to try to port some of this experimentation over into the JUnit test I started for the Kotlin code.
g
it would be interesting to see the type in the debugger before it's passed to nashhorn.. maybe you've wrapped the entire thing in an extra set of curly braces?
d
I thought exactly that, but I haven’t seen them yet if that is what I did.
g
the itellij debugger should show you interim inferred types. it could help to break them down into explicit intermediate assignments & step through with the debugger
d
You mean like this? 🙂
Copy code
final JsonElement jsonElement = data.toJson();
            final JsonObject asJsonObject = jsonElement.getAsJsonObject();
            final JSObject wrap = wrap(asJsonObject);
            final Object wrapMember = wrap.getMember("GatekeeperEndpointType");
            log.debug("Lots of stuff:\ndata ({}): {}\nasJsonObject ({}): {}\nwrap ({}): {}\n wrapMember ({}): {}"
                    data.getClass().getSimpleName(),
                    data.toString(),
                    asJsonObject.getClass().getSimpleName(),
                    asJsonObject.toString(),
                    wrap.getClassName(),
                    wrap.toString(),
g
something like that might work but i was more thinking set breakpoints in your code and use the intellij debugger
d
Gee, go for the easy stuff why don’tcha? 🙂
I just got a class cast exception that I think is showing me the culprit.
java.lang.ClassCastException: com.jsonlogic.JSObjectWrappersKt$wrap$1 cannot be cast to jdk.nashorn.api.scripting.JSObject
I was expecting that last guy to be a JSObject, but it looks like it is my function0..
Did I do something wrong with this delegate?
override fun getMember(name: String): Any? = data[name].wrap()
The intention is, if you call getMember on my wrapped instance, I get the member from the delegate object, but then I wrap that member so it can be properly visible to the JS engine
Aha, maybe here is where I’ve got those extra curly braces..
Copy code
fun JsonPrimitive?.wrap(): Any? = {
    if (this == null) null
    else if (this.isBoolean) this.bool
    else if (this.isNumber) this.number
    else this.string
}
f
is it even possible for
this
to be null?
d
I think so
because when calling wrap from the Java world, it is via a static method that takes the receiver as an argument
com.jsonlogic.JSObjectWrappersKt.wrap(something)
so I figured that Java could pass a null for something and hence I needed to guard against it
Does that sound right?
f
you’re right, cool
g
it's the = sign
f
Copy code
fun Integer?.wrap() : String? {
    return if (this == null) "nope" else "$this"
}
fun main(args: Array<String>) {
    val a = 5
    val b : Integer? = null
    println(a)
    println(b)
}
d
Yep! I got rid of the braces after the = and it works
f
Copy code
5
null
😄
d
Well, I shouldn’t say works, just on to the next bug
g
lol yep = either use = or {}, not both
d
If you are interested, here is the project I’m working on. I think it is in a somewhat useful state now.
Thank you both for your help!
👍 1