Methods cannot take type arguments, so I find myself writing `func Foo(o 
Object) {...}` instead of `func (o Object) Foo()` in cases where Foo needs 
a type parameter.

I would like some type of pseudo-method in Go similar to Kotlin's extension 
methods. Made up syntax:

```go
package foo

func (o Object) #Foo[T any]() { /* ... */ } // Foo is a pseudo method of 
Object

func main() {
  obj := Object{}
  obj#Foo() // use # to differentiate from regular methods, or use .
}
```
or something more clever.

I expect a lot of the time people use methods for syntactic reasons, not so 
those methods can be used to implement an interface. Methods appear in 
godoc along with the type, get better tab completion than functions, etc. 
I'm not proposing these pseudo-methods be used in any way to implement 
interfaces.

This may have already been discussed. There have been rejected proposals 
for adding methods to types defined elsewhere 
(https://github.com/golang/go/issues/37742 
and https://github.com/golang/go/issues/21401). However, I can't find a 
proposal that proposes the Kotlin approach to "extension methods," which is 
largely syntax sugar that allows `fun koo(k Kobject): void` to be called 
like `k.koo()` by the programmer (rather than `koo(k)`) so long as `koo` is 
statically resolved where such `k.koo` calls appear. Is there such a 
proposal?

This feature would be useful for defining these pseudo-methods on types 
within a package or on types from other packages. Using a pseudo-receiver 
type that's defined in another package raises some questions about how to 
use the pseudo-method without surprising/confusing readers. Most 
prominently for writing method-like generic functions.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e8685cd7-4691-4b74-8c9a-b4a8992dbd20n%40googlegroups.com.

Reply via email to