kotlin - How can I fix this error with nested interfaces - Stack Overflow

admin2025-04-17  2

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.

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.

Share Improve this question asked Jan 30 at 21:03 zplotzplot 354 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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.

转载请注明原文地址:http://anycun.com/QandA/1744890600a89078.html