Feature Request Kotlin - call super infix functions as infix

Call of overridden super class infix method with infix syntax

class Foo{
    open infux fun showEmotion(emotion:  String){
        println(emotion)
    }
}

class Bar : Foo {
    override fun showEmotion(emotion : String){
           super showEmotion emotion
    }
}

This is instead of

override fun showEmotion(emotion : String){
    super.showEmotion(emotion)
}
1 Like

Why, what is the big advantage of that. Is this change important enough to offset the minus 100 point rule? I don’t really think so.

2 Likes

I agree doesn’t pass the rule. Felt it provided continuity with infix method syntax.

1 Like

super isn’t the same as a reference to this.
The exact error is:

‘super’ is not an expression, it can only be used on the left-hand side of a dot (‘.’)

Super is a bit different from this in that it’s not an expression.

(super).anyFunction() // Doesn't work... even if the function is not infix

Since infix functions work with left-hand and right-hand expressions, using super on either side is the same as having (super) in parentheses as an expression.

Here’s a runnable (and editable :pencil2: ) example. Try uncommenting some of the lines to see what happens:

open class Base {
    open infix fun method(int: Int) = println("left=$this, right=$int")
    val instance = this
}

class Foo : Base() {
    init {
        //sampleStart
        (1 + 1) example (3 + 3)
        1 + 1 example 3 + 3
        
        this.method(4 + 1)
        this method 4 + 1
        (this) method (4 + 1)
        
        super.method(4 + 1)
        //super method 4 + 1     // <-- Try uncommenting this line
        //(super).method(4 + 1)  // <-- Try uncommenting this line
        (this as Base) method 4 + 1
        
        instance method 4 + 1
        super.instance method 4 + 1
        //(super).instance       // <-- Try uncommenting this line
        //sampleEnd
    }
}

infix fun Int.example(other: Int) = println("Example: left=$this, right=$other")

fun main() {
    Foo()
}
2 Likes

Yeah, I agree super is not an expression. My view is to enable consistency with infix method syntax
this infixMethod arguments works
then why wouldn’t
super infixMethodName argument work

In my example the super showEmotion can’t be used in the overridden method with infix syntax observed thus it can only be called as super.showEmotion(/*arguments*/)

I get your point though.

3 Likes

Yeah it could be nice for constancy–it’s a good thing to point out at least :+1:

I more so posted my stuff for future readers. Since the cost/benefit ratio may be low, I figure either: this request might sit idle (if it’s the only use-case and not considered a bug). Or in the future something changes with the compiler or language features that changes use-cases or defines the correct behavior.

4 Likes