Do you control the map? Can you annotate the gette...
# javascript
h
Do you control the map? Can you annotate the getter with @JsName ?
s
Copy code
actual var hello = "HELLO WORLD"
@JsName("testMap")
actual var testMap: Map<String, String> = mapOf(Pair("key", "JS"))
This is my map currently
h
And I suppose the problem arises because you're accessing this with "pure" JS (i.e. not created from kotlin)? Well, I don't have any other suggestions, actually. You could subclass the map and override the getter, but that's a bit overkill for something that should be simple...
Maybe you should create a custom getter (only the function - not a complete subclassing of the map)?
s
Exactly what I thought. Issue is you would need to subclass nearly every part, which doesnt make sense. But if it was in pure kotlin it wouldnt have this issue. JS support isnt great. Even the minified code isnt accessable via JS
Copy code
}(this, function (_, Kotlin) {
  'use strict';
  var Pair = Kotlin.kotlin.Pair;
  var mapOf = Kotlin.kotlin.collections.mapOf_x2b85n$;
  var hello;
  var testMap;
  hello = 'HELLO WORLD';
  testMap = mapOf(new Pair('key', 'JS'));
}));
These vars cannot be accessed
h
But you don't need to subclass. You just make a separate (proxy) function.
s
I would need to proxy the map with a wrapper in JS?
Well… I managed something, but my god…. I created a utility fun in .kt
Copy code
@JsName("mapGetter")
fun mapGetter(map: Map<*, *>, key: Any): Any? {
    return map[key]
}
And can access the fields like this.
common.mapGetter(common.testMap, "char") = "X"
common.mapGetter(common.testMapInt, 1) = 10
Going to try it as an extension
No joy.
i
Hi! This is a known problem of Kotlin/JS. Feel free to vote or comment here - https://youtrack.jetbrains.com/issue/KT-34995
👍 1
s
Thanks Ivan, do you think the last comment on the list will work for now until this gets improved?
"This is by design, to avoid name clashes on JavaScript when overriding methods. A "reproducible" suffix is generated based on the number and type of its parameterss and appended to the method name. This guarantees a unique JS method per Kotlin method.
I'm not an expert but I don't think this there is anything to "fix" here. You can create your own "listOf" kind aliases in Kotlin and use @JsName annotation to force the name to be the same in the generated JS code." I'm a bit lost as to why the methods where not just named slightly different while this was getting addressed like "map.getFor("key")".