https://kotlinlang.org logo
a

addamsson

01/23/2019, 12:21 PM
g

gildor

01/23/2019, 12:42 PM
You cannot. This is jvm only feature
But you can use expect/actual declaration and extract this code to jvm and implement in some different way in other platforms
a

addamsson

01/23/2019, 12:46 PM
do you know about a library which does this?
Seems like that there is a proxy object in javascript as well
e

Ema

01/23/2019, 12:57 PM
@gildor do you know if kapt based approaches are even possible? Generating instead of reflecting code?
🕵️‍♂️ 1
d

Dico

01/23/2019, 1:02 PM
You can generate code with kotlin dynamically on the jvm, so yes. It's a very powerful feature.
e

Ema

01/23/2019, 1:26 PM
how about, generate code on KN?
g

gildor

01/23/2019, 1:52 PM
do you know about a library which does this?
Does what? proxy? It’s platform specific API so should be done on platfrom specific code.
@Ema Kapt based on Java APT, so it’s JVM only API But you can use it to generate some Kotlin code that can be used on any platform. It will be additional build step, where you generate some common code, than build it using MPP
👍 1
a

addamsson

01/23/2019, 2:46 PM
I think I'll try to reconcile javascript's proxy with java's proxy instead
and provide a common api for it
i don't really want to generate code
or have my users bother with it
thx for the ideas 👍
d

Davide Giuseppe Farella

01/23/2019, 6:28 PM
@addamsson I’m in the same limbo here 😄 Actually I’m using
Proxy
on Jvm and
TODO()
on KN & JS, since they aren’t my priority actually and hoping in a better solution than code-gen, please keep in touch here on StackOF, I’ve starred your post, just in case
😂 1
a

addamsson

01/23/2019, 6:29 PM
Copy code
TODO()
this literally made me laugh out loud 😄
👍
d

Davide Giuseppe Farella

01/23/2019, 6:30 PM
TODO()
is the developers’s 42 😄
✔️ 1
g

gildor

01/24/2019, 1:23 AM
just curious what is your use case for Proxy
d

Davide Giuseppe Farella

01/24/2019, 6:06 AM
That's mine https://github.com/4face-studi0/Ermes Tbh I'm also curious about Adam
g

gildor

01/24/2019, 6:08 AM
So Retrofit like use case. Yeah, I see
I don’t think that it will be possible to implement something like this for MPP before Compiler Plugins API will be available
or some another approach, like function delegation, but it also requires language support
For now I just use ktor-http-client for this (but also use interface to abstract it)
d

Davide Giuseppe Farella

01/24/2019, 6:16 AM
For now I'm just exploring kotlin multiplatform and I'm very happy to see at least the jvm part is working. Then, with time, I'll see if "something new" will come to kotlin-mp to help me on that, or maybe choose a complete different approach.
a

addamsson

01/24/2019, 1:47 PM
@Davide Giuseppe Farella I did some research
I tried using javascript's
Proxy
as an
external
class
but it quickly turned out that javascript's proxy is not a real proxy
when you careate a proxy for a
Foo
interface you get back a
Proxy
and not an object which implements
Foo
so I'm kinda stuck with this idea
while Java's proxy works as it is supposed to work javascript's proxy does not adhere to the proxy pattern
d

Davide Giuseppe Farella

01/24/2019, 1:51 PM
So basically
Copy code
interface Foo {
    fun bar(): Int
}
become
Copy code
class Proxy {
    fun bar() = 4 // for instance
}
?
a

addamsson

01/24/2019, 1:51 PM
no, the whole point would have been to supply an interface
and get a proxy instance back like
Copy code
val foo: Foo = createProxyFrom(Foo::class)
this is the proxy pattern
but javascript doesn't support it
d

Davide Giuseppe Farella

01/24/2019, 1:54 PM
Yes, I just didn’t understand what that Js lib does instead of that
I mean, at runtime, my
Foo
interface implementation become ( basically )
Copy code
class FooImpl: Foo {
    override fun bar() = 4
}
What about that Proxy lib?
a

addamsson

01/24/2019, 1:56 PM
this is not a lib but a built-in class in javascript
what it does is that you give it an object
it wraps the object
and you use the proxy from that point like it was that object
given javascript's dynamic nature it is not a problem that you get back a
Proxy
instead of a
Foo
but this is a problem in Kotlin since you can't cast a
Proxy
to a
Foo
I pasted a link to the docs above ☝️
d

Davide Giuseppe Farella

01/24/2019, 1:58 PM
Ok, so I get it right, basically I’ll have class FooImpl : Foo
I’m taking a quick look at that
Anyway maybe that could fit my usecase, I’ll need more valutations. Thanks for now
a

addamsson

01/24/2019, 4:57 PM
👍