Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW the original works as well, but because you're using a typedef (not an alias) you have to convert from your own type to the underlying:

    func (t *fishTank) fishCount() string {
        return fmt.Sprintf("How many fishies? %d!", (*container[fish])(t).size())
    }
that's because

    type fishTank container[fish]
creates a completely new and independent type with container[fish] as the underlying implementation.

An alternative is to just alias:

    type fishTank = container[fish]
however in that case you can't define methods on fishTank, because it's literally just a shorthand for container[fish].


OK, that `(*container[fish])(t).size())` idiom is um non-obvious. Somebody needs to write a nice simple bloggy step-by-step walk through all these gyrations.


It's not really an idiom. The normal conversion expression is

   T(v)
but that doesn't work if T is a pointer type, because it's parsed as

   *(T(v))
so the types don't match. So you need

    (*T)(v)
to ensure the type part includes the pointer specifier.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: