I'm trying to create a <Google Chromecast Custom R...
# javascript
m
I'm trying to create a Google Chromecast Custom Receiver using KotlinJS. I downloaded the chromecast-caf-receiver TypeScript declaration files from DefinitelyTyped. and converted them to Kotlin using dukat. After some modifications to get the output from dukat to compile, I came up with the attached files. Question: I need to get an instance of
CastReceiverContext
by calling
cast.framework.CastReceiverContext.getInstance()
. In order to do that, I've done this:
Copy code
external object cast {
    var framework: Framework
}

external object Framework {
    var CastReceiverContext: CastReceiverContextStatic
}

external open class CastReceiverContextStatic() {
    fun getInstance(): CastReceiverContext
}

// access it using:
val context = cast.framework.CastReceiverContext.getInstance()
This works, but I feel like there is a better way to do this. Is there?
r
Not sure if better, but shorter:
Copy code
external val cast: dynamic
val context: CastReceiverContext = cast.framework.CastReceiverContext.getInstance()
m
That looks neater, thanks! I actually originally tried this:
Copy code
external val cast: dynamic
val context = cast.framework.CastReceiverContext.getInstance() as CastReceiverContext
but I was getting
ReferenceError: CastReceiverContext is not defined
. I didn't know that specifying the type while declaring a
dynamic
variable is different than casting it.
r
actually I don't know how is
CastReceiverContext
type defined in your project, because it's not defined in your sample
I assumed it is defined elsewhere
if you do a cast the compiler probably generates some kind of check and throws an error
if you just assign a
dynamic
variable you get an unchecked cast warning in IDE, and there is no check at runtime
m
ah, I see
r
at least thats how I think it is 🙂 haven't tested this :)
m
here is
CastReceiverContext
by the way