On Tue, Sep 11, 2018 at 10:45 AM Ian Lance Taylor <i...@golang.org> wrote:
> On Tue, Sep 11, 2018 at 10:04 AM, roger peppe <rogpe...@gmail.com> wrote:
> >>  func (x Foo(int)) Bar() {}
> >
> > As I understand it, this would be technically allowed, but would not
> > mean what you think.
>
> Yes.
>

Ouch! I chose a poor example. I think everyone understood my intent anyway,
but to be concrete, here's a better example:

type Foo(type T) struct{}

func (x Foo([]int)) Bar() {}



> I think this general topic is what C++ calls template specialization.
> The design draft explicitly does not support specialization.  This is
> listed in the "Omissions" section.  Specialization is a step toward
> template metaprogramming, which is a direction that I personally think
> Go should avoid.
>

So the answer is this draft does not permit these examples. That's the
answer I was expecting; thanks for confirming it. Two more comments:

First, please consider requiring the 'type' keyword in definitions of
methods on generic types:

func (x Foo(type T)) method() {}

This adds a small amount of verbiage, but makes the intent crystal clear.
It also allows for easy extension of the syntax if some far future version
of the language does allow adding methods to template instantiations.

Second, the actual text from the Omissions section is

No specialization. There is no way to write multiple versions of a generic
function that are designed to work with specific type arguments (other than
using type assertions or type switches).


which would appear to allow the example above but forbid this:

type Foo(type T) struct{}

func (x Foo(T)) Bar() {}

func (x Foo([]int)) Bar() {}


However, as Axel pointed out, the section "Parametrized types" says

Parameterized types can have methods. The receiver type of a method must
list the type parameters. They are listed without the type keyword or any
contract.

which on careful reading does appear to ban these examples. So, mea culpa
for not reading carefully enough.

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to