https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

jdiaz

05/11/2020, 7:27 PM
Is it possible to implement an
expect
class in JVM with a property
path
and the extending class has a method
getPath()
? (I'm talking about
File
class). I'm trying something like
actual class MyFile extends <http://java.io|java.io>.File
and MyFile has a property
path
.
e

edenman

05/11/2020, 7:43 PM
i think that works if it’s a typealias instead of an extends
actual typealias MyFile = <http://java.io|java.io>.File
j

jdiaz

05/11/2020, 7:44 PM
That would work if the methods weren't a little bit different and I wasn't able to create actual extension methods to that typealias
e

edenman

05/11/2020, 7:54 PM
best i could come up with:
Copy code
actual class MyFile(pathname: String) : File(pathname) {
  actual val myPath: String
    get() = path
}
couldn’t get around the “accidental override” if the name is the same
j

jdiaz

05/11/2020, 7:57 PM
That's what I came up with, and one can't override getPath as it's final, I wouldn't like to call it mypath as it works everywhere and I wouldn't like to have a backing object as that means 2 objects every time it is instantiated but it seems is what I'll have to do
I would be fine if I found a synonym for path, lol
2 Views