dtdr84101 2019-07-04 13:40
浏览 97
已采纳

可以为函数类型创建方法吗?

I know methods can be created for specific types

Example

type MyFunc func() int

func (f MyFunc) eval() int {
    return f()
}

func main() {
   var randomFunction MyFunc = func() int {
      return 1;
  }

  fmt.Println(randomFunction.eval()) // prints 1
}

But randomFunction had to be declared as with MyFunc type. If the declaration looked like

randomFunction := func() int {
  return 1;
}

the method call wouldn't had worked.

My question is whether methods can be created for every function with func () int type, for example.

I know Go is not functional, nor does contain too many functional idioms, but the I'm looking for an approach similar to Haskell's show instance for functions:

instance Show (Int -> Int) where
   show op = show "Function with 1 Int param and Int return type"
  • 写回答

1条回答 默认 最新

  • drza10046 2019-07-04 13:45
    关注

    Methods are "bound" to a type. MyFunc.eval() is bound to type MyFunc.

    This:

    randomFunction := func() int {
      return 1
    }
    

    Is a short variable declaration which creates a variable with name randomFunction and uses a function literal to provide an initial value for it, whose type will be an anonymous function type: func() int. This type does not have the eval() function (anonymous types have zero methods).

    But you may use a type conversion to convert it to MyFunc, and the resulting value will have that method:

    randomFunction2 := func() int {
        return 1
    }
    fmt.Println(MyFunc(randomFunction2).eval()) // prints 1
    

    The above conversion does not change the type of randomFucntion2 of course.

    If you use the conversion in the short variable declaration, randomFunction3 will have a static type of MyFunc and "always" have the eval() method:

    randomFunction3 := MyFunc(func() int {
        return 1
    })
    fmt.Println(randomFunction3.eval()) // prints 1
    

    Also note that oftentimes you don't even have to manually convert your function values, as if it is an anonymous type, the conversion will happen automatically.

    For example if you have a function that takes a value of type MyFunc:

    func handle(f MyFunc) {
        fmt.Println(f.eval())
    }
    

    You may pass the above randomFunction2 to it:

    handle(randomFunction2) // This is OK
    

    The above works because the value of randomFunction2 is assignable to a (variable of) type MyFunc (they have the same base type). For details, see Custom type passed to function as a parameter.

    Try the examples on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错