FAQ
Hi,

I was wondering, why does the compiler disallow the following
http://play.golang.org/p/6ZpeGpYLc4

(cannot use v (type MyType) as type string in function argument)

I know that if I changed my call to NeedString(string(v)) it would work,
but why not allow a derivative type where its primitive is expected?

Thanks

--

Search Discussions

  • Josh Holland at Sep 13, 2012 at 4:19 pm
    Hi,
    On 13 September 2012 17:09, tomwilde wrote:

    I know that if I changed my call to NeedString(string(v)) it would work,
    but why not allow a derivative type where its primitive is expected?

    Because string and MyType are distinct types, and Go does not perform type
    conversions between concrete types.

    Thanks,
    Josh

    --
  • Tomwilde at Sep 13, 2012 at 6:59 pm
    I understand that. My question is why not.

    Am Donnerstag, 13. September 2012 18:19:58 UTC+2 schrieb Josh Holland:
    Hi,

    On 13 September 2012 17:09, tomwilde <[email protected] <javascript:>>wrote:
    I know that if I changed my call to NeedString(string(v)) it would work,
    but why not allow a derivative type where its primitive is expected?

    Because string and MyType are distinct types, and Go does not perform
    type conversions between concrete types.

    Thanks,
    Josh
    --
  • Larry Clapp at Sep 13, 2012 at 7:21 pm
    Here's my guess: If they allowed it in function calls they'd probably have
    to allow it in method calls, which are really just function calls under the
    hood. So given this:

    type T1 string
    type T2 T1
    type T3 T2

    func (s T2) f() {}
    func (s T3) f() {}

    s := "foo"
    s.f()


    Which f() should be called?

    -- L
    On Thursday, September 13, 2012 2:59:26 PM UTC-4, tomwilde wrote:

    I understand that. My question is why not.

    Am Donnerstag, 13. September 2012 18:19:58 UTC+2 schrieb Josh Holland:
    Hi,
    On 13 September 2012 17:09, tomwilde wrote:

    I know that if I changed my call to NeedString(string(v)) it would work,
    but why not allow a derivative type where its primitive is expected?
    Because string and MyType are distinct types, and Go does not perform
    type conversions between concrete types.
    --
  • Russ Cox at Sep 13, 2012 at 7:54 pm

    On Thu, Sep 13, 2012 at 2:59 PM, tomwilde wrote:
    I understand that. My question is why not.
    Because it might be a mistake. Better for the programmer to be explicit.

    Russ

    --
  • Si guy at Sep 13, 2012 at 4:34 pm
    Type safety.

    MyType and string are not the same type, the reason that you can assign "foobar" to a MyType is because it is a literal and I think literals are untyped in go.

    --
  • Tomwilde at Sep 13, 2012 at 6:58 pm
    Hi,

    But basically the MyType's memory representation ought to be exactly the
    same as string's, right?

    The program would never encounter unexpected data hidden beneath a derived
    type, it could treat MyType as a string safely.

    Am Donnerstag, 13. September 2012 18:29:04 UTC+2 schrieb si guy:
    Type safety.

    MyType and string are not the same type, the reason that you can assign
    "foobar" to a MyType is because it is a literal and I think literals are
    untyped in go.
    --
  • Larry Clapp at Sep 13, 2012 at 7:42 pm
    Sort of, but not really. The method sets of "string" and "MyType" would
    differ. If you want to pass a MyType to something that expects a string,
    convert it. http://golang.org/ref/spec#Conversions There's probably
    little or no run-time cost to this, and this makes your intent explicit.

    Say you had

    type MyType string

    func f1(s string) {
    s.f2()
    }

    func (s string) f2() {}
    func (s MyType) f2() {}

    and you remembered that f1 called f2, and you called f1 with a MyType, you
    might easily assume that f1 would call func (s MyType) f2(). But you'd be
    wrong.

    -- L

    On Thursday, September 13, 2012 2:58:33 PM UTC-4, tomwilde wrote:

    Hi,

    But basically the MyType's memory representation ought to be exactly the
    same as string's, right?

    The program would never encounter unexpected data hidden beneath a derived
    type, it could treat MyType as a string safely.

    Am Donnerstag, 13. September 2012 18:29:04 UTC+2 schrieb si guy:
    Type safety.

    MyType and string are not the same type, the reason that you can assign
    "foobar" to a MyType is because it is a literal and I think literals are
    untyped in go.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 13, '12 at 4:09p
activeSep 13, '12 at 7:54p
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase