A question about how to inherit from external classes in Kotlin/js

I posted this question to SO and didn’t get any traction. So I’m hoping someone here can help me wrap my head around it.

Using Kotlin/js, here’s something that works:

fun main() { val canvas= document.getElementById("mainCanvas") as HTMLCanvasElement val ctx=canvas.getContext("2d") as CanvasRenderingContext2D val path=Path2D() path.rect(10.0,10.0,10.0,10.0) ctx.stroke(path) } 

Here’s something that doesn’t work:

fun main() { val canvas= document.getElementById("mainCanvas") as HTMLCanvasElement val ctx=canvas.getContext("2d") as CanvasRenderingContext2D val path=ExtraPath() path.rect(10.0,10.0,10.0,10.0) ctx.stroke(path) } class ExtraPath() : Path2D() { fun extraFunction(){} } 

When it runs in the client, js complains that Path2D constructor: ‘new’ is required.

So what’s the correct way to extend one of these js wrapper classes?

What I’ve tried: various combinations of annotations. Reading through the generated js file to see where Path2D is instantiated (it happens by calling Path2D.call(this) from inside the generated ExtraPath function, which is perfectly legal as best as I can gather). Reading through the wrapper class to see how they do extensions. And they all look pretty much like this, the parent class is declared external and then it’s put after the “extends” colon like normal kotlin. Read the docs, they have an example exactly like this with foo() and bar() cutouts. The only difference is that their bar() was declared external by them in the example instead of by the imported wrapper class. Beyond that, I just don’t know enough about the process to get clues from anywhere else.

Edit:

This is the best I can do. I’m kind of joking with the interface. I would just put the “parent” inside one of the child fields directly. But it was obvious 12 hours ago that that was an option. I just wanted to figure out why this doesn’t work the way it shows on the docs page and behave like…kotlin..so that people could write “js in kotlin”.

fun main() { val canvas= document.getElementById("mainCanvas") as HTMLCanvasElement val ctx=canvas.getContext("2d") as CanvasRenderingContext2D val path=ExtraPath() path.p.rect(10.0,10.0,10.0,10.0) ctx.stroke(path.p) } interface Ancillary{ val p:Path2D } class ExtraPath():Ancillary{ override val p=Path2D() fun extraFunction(){} } 

Don’t get me wrong. Nothing makes me happier than the thought of being able to work exclusively in kotlin. But with the docs this slim, 20 different hello world github repos, and tutorials that never ship with unbreaking dependencies, I think this is going to be the second time this month I just decide to give up on a task and just write it in js. And I don’t even know js, but I can finish a small task in 10 hours by pigeon typing ugly syntax and googling. I’ve wasted twice that looking for non-jvm kotlin docs that don’t exist.

Ok therapy session over. Kotlin, I love you. I just really really love you for jvm.

submitted by /u/arnoldfrend
[link] [comments]