I saw this in an example... brings up a question, ...
# javascript
g
I saw this in an example... brings up a question, how does one change an external interface declared already? The examples just seem to show inheritance.
Copy code
// Simple warn notifier
      var origWarn = console.warn;
      toastr.options = {
        closeButton: true,
        preventDuplicates: true,
        showDuration: 250,
        hideDuration: 150
      };
      console.warn = function (msg) {
        if (msg.indexOf('[undefined]') == -1) {
          toastr.warning(msg);
        }
        origWarn(msg);
      };
b
You can redeclare it and both will bind to same js (or rather ts) interfaces
g
hmm... I realized console was an "interface" in the std lib so I tried overriding the function, but I don't think it worked, maybe because of the public val... js("") worked but is ugly of course. Here is the std lib declaration, will try to re-declare as you suggest.
Copy code
/**
 * Exposes the [console API](<https://developer.mozilla.org/en/DOM/console>) to Kotlin.
 */
@Suppress("NOT_DOCUMENTED")
public external interface Console {
    public fun dir(o: Any): Unit
    public fun error(vararg o: Any?): Unit
    public fun info(vararg o: Any?): Unit
    public fun log(vararg o: Any?): Unit
    public fun warn(vararg o: Any?): Unit
}

/**
 * Exposes the [console API](<https://developer.mozilla.org/en/DOM/console>) to Kotlin.
 */
public external val console: Console
Yeah, I think that wouldn't work either... I need to redefine the actual external interface, this is the problem:
Copy code
console.warn = function (msg) {}
Kotlin doesnt seem to allow it.. thinks warn should be warn() and then doesn't get the reassignment.
I might be close to a solution
b
What about external interface with your stuff and then external val console: YourFancyExternalInterface
g
Trying everything, failing. I don't think there is a way to do this in Kotlin, redefine an external interface (that is obviously defined externally).
I need to be able to do this:
Copy code
override external val console: Notifier
but can't =(
b
Ah i see, you want to override external fun with kotlin code, right? You can do it with extersion functions
g
extersion function, is that like a crevortis function?
b
Copy code
fun Console.log() = ...
Extension*
g
hmm... sort of, but even then, I would extend from
Copy code
kotlin.js.console
which is a bunch of externals... so I'd be trying to extend a library of external declarations, one val and one interface. The only way to extend the interface would be to create a NEW class (since its an interface, i think) and inherit from it which I did... but it doesn't need extension it needs to be override, which I did... but I still run against the same problem, reassign the JS console functions. To clarify: The goal here is to be able to see Toastr alerts AND console warnings for ALL console warnings submitted BY ANYTHING, not just my code 😃
b
Ah, i see, then just add some js code into your resources and do it there. Or just execute it in main as raw js via js() function
Either way you'd be doing js overriding via js, don't think kotlin supports overriding js stuff
g
yeah, js("") is the solution... but then I ask myself, what advantage is kotlin providing me with if it's less flexible than the destination and forces me to put real production code in resources or a string... kinda crazy.
So I waste time.
Don't really understand why that works either, or how. I mean I know that if I try to reference kotlinJS variables in the js() string it fails.
b
Well it kas its tradeoffs. If you find yourself doing js() often, then drop kotlin, otherwise i think it's still worth it
g
ok. thanks
b
Js() only sees global values
I think you can get your kotlin vals visible as well if you annotate them with jsExport and jsName
Okay, js() it is, moving on from the abyss of the impossible...