FAQ
Hi,

I tried applying this approach in my application but could not make it to
work. Therefore, I reduced the problem to the following example:

type IntMethod func(interface{}, int, int) int

type IntMethodHolder struct {
operator IntMethod
}

func (self Addition) add(left, right int) int {
return left + right
}


The statement in which I create an IntMethodHolder with a reference to the
"add" method of Addition fails to compile.

For the full example see : http://play.golang.org/p/TtpNoazdzj

So the question is: Is the type definition of IntMethod incorrect or is it
(storing a method reference) just not possible?



^ Ernest

--

Search Discussions

  • Jan Mercl at Dec 7, 2012 at 9:31 am

    On Fri, Dec 7, 2012 at 10:07 AM, Ernest Micklei wrote:
    So the question is: Is the type definition of IntMethod incorrect or is it
    (storing a method reference) just not possible?
    http://play.golang.org/p/86fRF62Jap

    -j

    --
  • Ernest Micklei at Dec 7, 2012 at 10:09 am
    Ok, for this example to work I understand that the IntMethod definition had
    to been changed.

    The original definition uses the "interface{}" as a generic type hoping
    that it would match Addition (and other types)
    That IntMethod definition was accepted by the compiler so what would be
    correct call to use it inside IntMethodHolder ?

    ^ ernest

    Op vrijdag 7 december 2012 10:31:34 UTC+1 schreef Jan Mercl het volgende:
    On Fri, Dec 7, 2012 at 10:07 AM, Ernest Micklei
    <ernest....@gmail.com <javascript:>> wrote:
    So the question is: Is the type definition of IntMethod incorrect or is it
    (storing a method reference) just not possible?
    http://play.golang.org/p/86fRF62Jap

    -j
    --
  • Jan Mercl at Dec 7, 2012 at 10:10 am

    On Fri, Dec 7, 2012 at 11:08 AM, Ernest Micklei wrote:
    Ok, for this example to work I understand that the IntMethod definition had
    to been changed.

    The original definition uses the "interface{}" as a generic type hoping that
    it would match Addition (and other types)
    What in the specs is such hope based on?

    -j

    --
  • Jesse McNelis at Dec 7, 2012 at 10:13 am

    On Fri, Dec 7, 2012 at 8:07 PM, Ernest Micklei wrote:

    type IntMethod func(interface{}, int, int) int

    type IntMethodHolder struct {
    operator IntMethod
    }

    func (self Addition) add(left, right int) int {
    return left + right
    }

    So the question is: Is the type definition of IntMethod incorrect or is
    it (storing a method reference) just not possible?
    You've misunderstand how interfaces work. interface{} isn't a 'generic'
    type. It's an interface.
    You can convert any type to an interface{}, but interface{} is it's own
    distinct type.





    --
    =====================
    http://jessta.id.au

    --
  • Ernest Micklei at Dec 7, 2012 at 10:30 am
    Thank you both for your help

    ^ E

    Op vrijdag 7 december 2012 11:13:06 UTC+1 schreef Jesse McNelis het
    volgende:
    On Fri, Dec 7, 2012 at 8:07 PM, Ernest Micklei <ernest....@gmail.com<javascript:>
    wrote:
    type IntMethod func(interface{}, int, int) int

    type IntMethodHolder struct {
    operator IntMethod
    }

    func (self Addition) add(left, right int) int {
    return left + right
    }

    So the question is: Is the type definition of IntMethod incorrect or is
    it (storing a method reference) just not possible?
    You've misunderstand how interfaces work. interface{} isn't a 'generic'
    type. It's an interface.
    You can convert any type to an interface{}, but interface{} is it's own
    distinct type.





    --
    =====================
    http://jessta.id.au

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 7, '12 at 9:07a
activeDec 7, '12 at 10:30a
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase