douyin4561 2014-03-25 02:01
浏览 46
已采纳

常量不输入是什么意思?

The Go Programming Language Specification says that:

Constants may be typed or untyped

I am having a little doubt in my understanding. Consider this example in the spec:

const l = "hi"             // l == "hi"  (untyped string constant)
const m = string(k)        // m == "x"   (type string)

The spec says:

constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression

By this statement, why isn't l typed since it is clearly a constant declaration?

This behaviour is clearer with another example

type Foo string
func f(a Foo) {}

func main() {
  f("sarkozy")

  const t = "julie gayet"
  f(t)

  s := "hollande"
  //compile error
  // f(s)

  f(Foo(s)) // ok
}

Is the reason that f("sarkozy") compiles be due to this statement on Assignability in the spec?

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

My argument is the following:

  • "sarkozy" a an untyped literal.
  • Thus "sarkozy" being representable by Foo means I can type coerce like this Foo("sarkozy")
  • f(s) fails because s is not untyped.
  • 写回答

1条回答 默认 最新

  • douyun1852 2014-03-25 04:07
    关注

    Why isn't l typed since it is clearly a constant declaration?

    Yes, it is clearly a constant declaration, as your quote says:

    constant may be given a type explicitly by a constant declaration

    However, in your case there is no explicitly given type. You can only have an implicitly given type when "used in a variable declaration or an assignment or as an operand in an expression".

    Is the reason that f("sarkozy") compiles be due to this statement on Assignability in the spec?

    Yes, the reason that f("sarkozy") compiles is because the untyped constant of "sarkozy" has a type given implicitly when used as an operand in an expression such as in your case.

    "sarkozy" is implicitly given the type of Foo

    So why doesn't f(s) compile? (okay, that was not in the question, but the question remains)

    Your argument states that: "f(s) fails because s is not untyped."

    True that s is not untyped. s is a variable and not a constant, and variables cannot be untyped.

    The Go specs states for Variable Declarations:

    If the type is absent and the corresponding expression evaluates to an untyped constant, the type of the declared variable is as described in §Assignments.

    And that refers, from what I understand to the following:

    the constant is first converted to type bool, rune, int, float64, complex128 or string respectively, depending on whether the value is a boolean, rune, integer, floating-point, complex, or string constant.

    So, the following line:

    s := "hollande"
    

    will declare the variable (not constant) s of type string because the right-hand expression is an untyped string constant. The type is implicitly given during the declaration of the variable, not by analyzing what context it which it later on will be used.

    f(s) will then result in a compile error because you try to use a value of type string where a value of type Foo is expected.

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题