dousilie9522 2017-01-06 08:16
浏览 127
已采纳

自定义类型传递给函数作为参数

When I define a custom type, it seems that the type of the underlying type makes a difference about whether I can pass it to a function as is or I need to convert it.

Question is: Why does RuneFunc and StringMap work, but not Integer?

https://play.golang.org/p/buKNkrg5y-

package main


type RuneFunc func(rune) rune
type Integer int
type StringMap map[string]string

func main() {
    //m := make(StringMap)
    //mf(m)


    var i Integer = 5
    nf(i)


    //var f func(rune) rune
    //ff(f) 

}

func mf(i map[string]string) {

}
func ff(i func(rune)rune) {

}
func nf(i int) {

}

Here, when I run this function called nf with Integer it complains although int is the underlying type. But when I call mf or ff they run successfully.

  • 写回答

2条回答 默认 最新

  • dongruan6001 2017-01-06 08:29
    关注

    Integer and int

    int and your new type Integer are 2 different, distinct types. Where Integer is expected, you have to pass a value of type Integer.

    If you have an Integer value, you may use a simple type conversion to make it a value of type int, because the underlying type of Integer is int:

    var i Integer = 5
    nf(int(i))
    

    What may be confusing and interesting at the same time is that you are allowed to pass an untyped constant without conversion:

    nf(5)
    

    Try these on the Go Playground.

    The reason for this is in the Spec: Assignability:

    A value x is assignable to a variable of type T ("x is assignable to T") in any of these cases:

    [...]

    • x is an untyped constant representable by a value of type T.

    5 is an untyped constant which is representable by a value of type int because the untyped constant 5 has a default type int, so it is representable by a value of type Integer (which has the same default type).

    If you check the other assignability rules (not included in above quotation), none of them match the case where you attempt to pass a value of Integer for the parameter of type int, so that's not allowed.

    See related question: Golang: Creating a Constant Type and Restricting the Type's Values

    RuneFunc and func(rune) rune

    The difference between this case and the previous one (Integer and int) is that int is a named type and func(rune) rune is not.

    And there's an assignability rule which allows this:

    So in this case:

    var f RuneFunc
    ff(f)
    

    f is a named type, but the parameter type of ff() is func(rune) rune which is unnamed, so the assignment is allowed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配