New to writing Kotlin wrappers, what is the correc...
# javascript
s
New to writing Kotlin wrappers, what is the correct way to wrap an object that consumes another object (i.e.
Foo({...})
)?
s
You want describe a JS class
Foo
that takes an object
{...}
of a particular shape as a constructor parameter. Did I understand you correctly?
s
Yeah that's the signature of the Javascript library
I just want to write the interop code (call existing Javascript from Kotlin)
s
That would be
Copy code
external interface FooParameter {
    // ...
}

external class Foo(p: FooParameter) {
    // ...
}
s
@Svyatoslav Kuzmich [JB] Thank you!
@Svyatoslav Kuzmich [JB] Is there any good way to describe an object
{}
with optional parameters? Suppose
new Foo({})
accepts an object
{A: 'foo', B: 'bar'}
, where A or B may be undefined. In Kotlin we'd use default parameters on the constructor, what's the analogue for the wrapper?
s
s
Thanks, I'll use that
s
In case of interfaces one can do
Copy code
external interface I {
    var A: String? get() = definedExternally; set(value) = definedExternally
    var B: String? get() = definedExternally; set(value) = definedExternally
}
s
Does this mean you need to create an implementation of
I
each time you want to pass it to
Foo
?
s
Yes, you would need a class or object that implements
I
. But you can also wrap the whole thing with a regular function that uses default parameters.
t
@Sam Garfinkel Could you configure
Foo
after creation?
Via properties for example
138 Views