FAQ
It seems to me that Go could use a keyword (for example "*same*") for
denoting the type of the receiver in interface methods specifications. For
example

type IComparable interface { Compare(same) int }

would mean that the argument to Compare must have the same type as the
receiver. As things are now you have to write something like

type IComparable interface { Compare(interface{}) int }

which is both ugly and inefficient as you have to check the type at runtime
with a possible panic. To my eyes the "same" keyword should be trivial to
implement and would vastly improve the usefulness of interfaces for generic
programming.

--

Search Discussions

  • Rémy Oudompheng at Nov 10, 2012 at 9:13 am
    I don't understand how you would use such a thing without a possible panic.

    You seem to want to use interfaces to emulate a kind of generic
    programming they are not made for.

    Rémy.


    2012/11/10, home@valentimex.com <home@valentimex.com>:
    It seems to me that Go could use a keyword (for example "*same*") for
    denoting the type of the receiver in interface methods specifications. For
    example

    type IComparable interface { Compare(same) int }

    would mean that the argument to Compare must have the same type as the
    receiver. As things are now you have to write something like

    type IComparable interface { Compare(interface{}) int }

    which is both ugly and inefficient as you have to check the type at runtime

    with a possible panic. To my eyes the "same" keyword should be trivial to
    implement and would vastly improve the usefulness of interfaces for generic

    programming.

    --

    --
  • Kevin Gillette at Nov 10, 2012 at 10:35 am
    It doesn't really work for interfaces, though, because if you have:

    type X int
    func (a X) Compare(b X) int { ... }

    type Y string
    func (a Y) Compare(b Y) int { ... }

    Then you can't have:

    list := []IComparable{X(5), Y("a string")}

    Since X and Y don't have the same "same", in which case, you're no better
    off than having:

    listOfX := []X{5, 6}
    listOfY := []Y{"a string", "another string"}

    And Go can already do that.

    By the way, prefixing interface type names with 'I' is contrary to Go
    convention. A more conventional name in this case would be Comparer.
    On Saturday, November 10, 2012 1:46:45 AM UTC-7, ho...@valentimex.com wrote:

    It seems to me that Go could use a keyword (for example "*same*") for
    denoting the type of the receiver in interface methods specifications. For
    example

    type IComparable interface { Compare(same) int }

    would mean that the argument to Compare must have the same type as the
    receiver. As things are now you have to write something like

    type IComparable interface { Compare(interface{}) int }

    which is both ugly and inefficient as you have to check the type at
    runtime with a possible panic. To my eyes the "same" keyword should be
    trivial to implement and would vastly improve the usefulness of interfaces
    for generic programming.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 10, '12 at 8:46a
activeNov 10, '12 at 10:35a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase