IssueRaiser

proposal: spec: operator functions

The only issue that I could _find_ about operator overloading currently #19770, although it's currently closed and doesn't have many details. # Goal Operator overloading should be used to create datatypes that represent things that already exist in Go. They should not represent anything else, and should ideally have no other function. **New operators should not be allowed to defined**, we should only be able to define currently existing operators. If you look at languages that let you define your own operators (Scala, looking at you!) the code becomes extremely messy and hard to read. It almost _requires_ an IDE because operator overloading is very heavily used. **Using an operator for anything _other_ than it's purpose is a bad idea**. We should not be making data structures with fancy looking `+` operators to add things to the structure. Overloading the `+` operator in Go should _only_ be used for defining addition/concatenation operators for non-builtin types. Also, as per Go spec, binary operators should _only_ be used for operating on two values of the same type. **Operators should only operate on and return a single type**. This keeps things consistent with how operators currently work in Go. We shouldn't allow any `type1 + string -> type1` stuff. **Operators should only be defined in the same package as the type they are defined on**. Same rule as methods. You can't define methods for structs outside your package, and you shouldn't be able to do this with operat

Posted by deanveloper on Sep 10, 2018, 9:53 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

ux (Other)

proposal: spec: operator functions

deanveloperSep 10, 2018, 9:53 PM

The only issue that I could _find_ about operator overloading currently #19770, although it's currently closed and doesn't have many details. # Goal Operator overloading should be used to create datatypes that represent things that already exist in Go. They should not represent anything else, and should ideally have no other function. **New operators should not be allowed to defined**, we should only be able to define currently existing operators. If you look at languages that let you define your own operators (Scala, looking at you!) the code becomes extremely messy and hard to read. It almost _requires_ an IDE because operator overloading is very heavily used. **Using an operator for anything _other_ than it's purpose is a bad idea**. We should not be making data structures with fancy looking `+` operators to add things to the structure. Overloading the `+` operator in Go should _only_ be used for defining addition/concatenation operators for non-builtin types. Also, as per Go spec, binary operators should _only_ be used for operating on two values of the same type. **Operators should only operate on and return a single type**. This keeps things consistent with how operators currently work in Go. We shouldn't allow any `type1 + string -> type1` stuff. **Operators should only be defined in the same package as the type they are defined on**. Same rule as methods. You can't define methods for structs outside your package, and you shouldn't be able to do this with operat

Comments (0)

No comments yet. Start the discussion.

Comment on this issue