https://kotlinlang.org logo
#getting-started
Title
# getting-started
u

ursus

11/20/2021, 3:55 PM
I'm trying to have typed Ids, so I created a interface
Copy code
interface Id : Comparable<Id> {
    val value: String
    
    override fun compareTo(other: Id): Int = value.compareTo(other.value)
    fun toString(): String = value
}
but it complains
'toString' hides member of supertype 'Comparable' and needs 'override' modifier
which is nonsense, but if I remove the comparable, then it complains that im hiding
Any
is this possible?
a

Ayfri

11/20/2021, 3:57 PM
It is not nonsense but Kotlin wants you to override the
toString
method, because it is already present in Comparable if I remember correctly
u

ursus

11/20/2021, 3:59 PM
no its not
r

Rob Elliot

11/20/2021, 4:07 PM
You can’t override
toString
in an interface. Just have to leave it to the implementations of
Id
. Or make
Id
an
open class
rather than an interface.
a

Ayfri

11/20/2021, 4:07 PM
It's already implemented in Any class, which every Class extends in Kotlin
r

Rob Elliot

11/20/2021, 4:07 PM
Copy code
open class Id(
  val value: String
) : Comparable<Id> {
  final override fun compareTo(other: Id): Int = value.compareTo(other.value)
  final override fun toString(): String = value
}
u

ursus

11/20/2021, 4:14 PM
yea I figured its impossible, its just that the error messages are bs imo
p

Paul Griffith

11/22/2021, 4:16 PM
u

ursus

11/23/2021, 12:47 AM
Yea but dagger doesnt work with them yet
2 Views