Infix methods, operators and thoughts

i know i shouldn’t put this on a go forum but i don’t know where and i don’t know how to make proposals as i’m just a noob programmer …

I understand that operator overloading would increase the complexity of the language, however would a feature like infix methods be a viable alternative?

for example if you want to add 2 vectors you can declare a method like:
@infix
+#m(matrix: a, matrix:b) matrix {…}

or if you want to multiply a vector to a scalor:
@infix
*#vs(vector: a, float: b) vector {…}

pros: avoids ambiguity of what you’re operating on, is infix like operators should be so the flow is natural
cons: looks like ■■■■

what are your thoughts and apologies for posting this here

Hi, @gorilla_sama, and welcome to the forum!

No apologies necessary; #technical-discussion seems like a fine place to discuss a new feature. I will tell you, though, that most of the frequent posters in this forum are not members of the Go language team, so even if we agree with you, we don’t have the power to add any new features to the language.

That being said, I Googled what “infix methods” are and I doubt Go will ever have them. I see one place they’re used is in Kotlin, and I found a description here:

2. What Is an Infix Notation?

Kotlin allows some functions to be called without using the period and brackets. These are called infix methods, and their use can result in code that looks much more like a natural language.

This is most commonly seen in the inline Map definition:

map(
  1 to "one",
  2 to "two",
  3 to "three"
)

“to” might look like a special keyword but in this example, this is a to() method leveraging the infix notation and returning a Pair<A, B>.

My gripe is with that last bit about how “to” looks like a special keyword. The languages I’m familiar with other than Go such as C# and Python, and perhaps Kotlin, seem to focus on making code look more declarative, like the code should explain what it’s doing and not how it’s doing it. Go is more like the antithesis to that and prefers raw loops and simple language constructs that show both what and how some code works.

1 Like

Thanks for the reply and the welcome.
yeah, i guess talking about it here is probably not gonna do anything to put the feature in go, but i guess i’m just using this as a brain storming session…

as for the how maybe a special character should be assigned for example a ‘^’ or ‘#’ at the beginning of these function calls which the user desires to be able to be written in infix form (yes, i changed the premise lol) so that when these expressions are seen in the wild it is known that it is a function which is being used in its infix form and that it takes exactly one argument in the back and one infront of it, maybe commas should seperate them for even more clarity.

for example

@infixable // allows you to use infix notation
fn add_mat(m1: matrix, m2: matrix) matrix { ... }
@infixable // compile error (infix only allows 2 args)
fn foo(int x) void { ... }
fn main() {
        let mT = add_mat(m1, m2)
        let m2T = m1, #add_mat, m2 // m2T == mT

        let m3T = m1, #add_mat, m2, #add_mat, m3 // and so on
        let m4T = add_mat(add_mat(m1, m2), m3) // compare with above
}