In the code you can see nested interfaces.
The error in Intellij IDEA is:
Kotlin: None of the following candidates is applicable: :25
The error is in the line: return Integer(this.value + other.value), and the "+" sign appears underscored in red.
I would really appreciate any help to fix the problem.
Thank you.
interface T1
interface Semigroup<T1> {
fun builder(x: T1): SemigroupElement<T1>
interface SemigroupElement<T1> {
fun multiply(other: SemigroupElement<T1>): SemigroupElement<T1>
val value: T1
}
}
fun main() {
class Z : Semigroup<Int> {
override fun builder(x: Int): Semigroup.SemigroupElement<Int> = Integer(x)
inner class Integer<Int>(val x: Int) : Semigroup.SemigroupElement<Int> {
override val value = x
override fun multiply(other: Semigroup.SemigroupElement<Int>): Semigroup.SemigroupElement<Int> {
return Integer(this.value + other.value)
}
}
}
}
In the code you can see nested interfaces.
The error in Intellij IDEA is:
Kotlin: None of the following candidates is applicable: :25
The error is in the line: return Integer(this.value + other.value), and the "+" sign appears underscored in red.
I would really appreciate any help to fix the problem.
Thank you.
You declared the inner class Integer
to have a type parameter named Int
. Every occurrence of Int
in the inner class refers to this type parameter, not the built-in kotlin.Int
type. What is happening is a lot clearer if we just rename this type parameter to T
.
inner class Integer<T>(val x: T) : Semigroup.SemigroupElement<T> {
override val value = x
override fun multiply(other: Semigroup.SemigroupElement<T>): Semigroup.SemigroupElement<T> {
return Integer(this.value + other.value)
}
}
this.value
and other.value
are both of type T
and obviously cannot be added together.
You obviously meant to use kotlin.Int
, and Integer
should not have any type parameters.
inner class Integer(val x: Int) : Semigroup.SemigroupElement<Int>
// ^^
// "<Int>" is removed from here!
Side note: interface T1
is redundant. If you think that is necessary for declaring interface Semigroup<T1>
, you are mistaken.