https://kotlinlang.org logo
Title
e

edwardwongtl

11/29/2017, 10:46 AM
Writing it by hand or generated by compiler is an other question though
d

diesieben07

11/29/2017, 10:48 AM
It doesn't matter if you write
interface GetText {
    fun getText(): String
}

class MyClass(@JvmField val _text) : GetText {
    override fun getText() = _text
}
or
interface GetText {
    val text: String
}
class MyClass(override val text: String) : GetText
The resulting bytecode is identical.
e

edwardwongtl

11/29/2017, 10:53 AM
Ya I know they will be the same, but the original question wasn't asked by me
d

diesieben07

11/29/2017, 10:53 AM
I know.
e

edwardwongtl

11/29/2017, 10:54 AM
Using
@JvmField
is just a fast way to get rid of that warning IMO
d

diesieben07

11/29/2017, 10:55 AM
It's the wrong way though... 😄
e

edwardwongtl

11/29/2017, 10:55 AM
I think what's wrong is the use of the
GetText
interface
d

diesieben07

11/29/2017, 10:55 AM
How so?
e

edwardwongtl

11/29/2017, 10:56 AM
That enum class already got a
val text
, the compiler will give you the
getText()
Why bother adding a interface for the
getText()
?
d

diesieben07

11/29/2017, 10:57 AM
One example: You have different enum types that both implement
GetText
.
e

edwardwongtl

11/29/2017, 10:57 AM
Unless he wants to do additional thing inside
getText()
d

diesieben07

11/29/2017, 10:57 AM
This is the whole point of interfaces.
e

edwardwongtl

11/29/2017, 10:58 AM
True..but in this case he just accidentally use a property name that matches the function
in other cases, this will works fine
d

diesieben07

11/29/2017, 10:59 AM
No, I assume he did not realize he can put properties in interfaces (probably Java background). So he tried to write a getter and assumed
val
is like a field in Java.
He has more than one enum class, he talked about that earlier.
e

edwardwongtl

11/29/2017, 11:01 AM
Oh I think that's the root cause then
m

michaelzinn

11/29/2017, 11:34 AM
Yes, I didn’t know that you can put vals in interfaces and yes, I want to use multiple enums that all implement the interfaces. The problem is solved (val in interface).