FAQ
Hello.

I want to distinguish two different ID numbers in my code, which are
virtually int. If I used int as types of ID variables, I got confused.
I created two types, "IDa" and "IDb" as aliases of int.
However, the function which was defined to receive IDa receives int value.
Here is example: http://play.golang.org/p/ZuHtgV7Poh

In this code, I expected the go compiler to stop with errors. I found that
the compiler will generate error if I use a pointer to pass a value to a
function.
Is there any way without pointer to prohibit type cast when using alias
type?

Thanks.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Nov 10, 2015 at 2:09 pm

    On Tue, Nov 10, 2015 at 2:57 PM Yoshitaka Taguchi wrote:

    I want to distinguish two different ID numbers in my code, which are
    virtually int. If I used int as types of ID variables, I got confused.
    I created two types, "IDa" and "IDb" as aliases of int.

    Go has type aliases, but user code cannot do that. For example, byte is an
    alias of int8. But

             type T U

    creates a new type, not an alias.
    However, the function which was defined to receive IDa receives int value.
    Here is example: http://play.golang.org/p/ZuHtgV7Poh
    In this code, I expected the go compiler to stop with errors.
    What error do you expect? 123 is an untyped literal. Per the specification
    it is assignment compatible with any type as long as it can be converted to
    the target type without information loss.

    I found that the compiler will generate error if I use a pointer to pass
    a value to a function.
    Is there any way without pointer to prohibit type cast when using alias
    type?

    Go has no casts. You can convert the untyped literal to a typed value. Then
    the compiler rightfully complains: http://play.golang.org/p/6o2wHl7mUS
    --

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nate Finch at Nov 10, 2015 at 9:09 pm
    To translate what Jan said into plain English:

    The problem you're showing in that link is that numeric literals (actual
    numbers written in your source code) are implicitly converted to the
    correct type for the function they're passed into. In practice, this is
    usually not a problem, because you'd almost never write 123 as something to
    pass into a function in your code. You'd get that ID from some other
    function, or you'd define it as a constant of a particular type, and then
    the value would have a definite type, and since that type wouldn't match,
    it wouldn't compile: http://play.golang.org/p/ZjUR7YH9U4
    On Tuesday, November 10, 2015 at 9:09:23 AM UTC-5, Jan Mercl wrote:

    On Tue, Nov 10, 2015 at 2:57 PM Yoshitaka Taguchi <jap....@gmail.com
    <javascript:>> wrote:
    I want to distinguish two different ID numbers in my code, which are
    virtually int. If I used int as types of ID variables, I got confused.
    I created two types, "IDa" and "IDb" as aliases of int.

    Go has type aliases, but user code cannot do that. For example, byte is an
    alias of int8. But

    type T U

    creates a new type, not an alias.
    However, the function which was defined to receive IDa receives int
    value.
    Here is example: http://play.golang.org/p/ZuHtgV7Poh
    In this code, I expected the go compiler to stop with errors.
    What error do you expect? 123 is an untyped literal. Per the specification
    it is assignment compatible with any type as long as it can be converted to
    the target type without information loss.

    I found that the compiler will generate error if I use a pointer to pass
    a value to a function.
    Is there any way without pointer to prohibit type cast when using alias
    type?

    Go has no casts. You can convert the untyped literal to a typed value.
    Then the compiler rightfully complains:
    http://play.golang.org/p/6o2wHl7mUS
    --

    -j
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Yoshitaka Taguchi at Nov 11, 2015 at 2:19 pm
    Thanks a lot for nice answers!
    I understood what to do. I'll amend my code.

    Yoshitaka

    2015年11月11日水曜日 6時09分31秒 UTC+9 Nate Finch:
    To translate what Jan said into plain English:

    The problem you're showing in that link is that numeric literals (actual
    numbers written in your source code) are implicitly converted to the
    correct type for the function they're passed into. In practice, this is
    usually not a problem, because you'd almost never write 123 as something to
    pass into a function in your code. You'd get that ID from some other
    function, or you'd define it as a constant of a particular type, and then
    the value would have a definite type, and since that type wouldn't match,
    it wouldn't compile: http://play.golang.org/p/ZjUR7YH9U4
    On Tuesday, November 10, 2015 at 9:09:23 AM UTC-5, Jan Mercl wrote:

    On Tue, Nov 10, 2015 at 2:57 PM Yoshitaka Taguchi <jap....@gmail.com>
    wrote:
    I want to distinguish two different ID numbers in my code, which are
    virtually int. If I used int as types of ID variables, I got confused.
    I created two types, "IDa" and "IDb" as aliases of int.

    Go has type aliases, but user code cannot do that. For example, byte is
    an alias of int8. But

    type T U

    creates a new type, not an alias.
    However, the function which was defined to receive IDa receives int
    value.
    Here is example: http://play.golang.org/p/ZuHtgV7Poh
    In this code, I expected the go compiler to stop with errors.
    What error do you expect? 123 is an untyped literal. Per the
    specification it is assignment compatible with any type as long as it can
    be converted to the target type without information loss.

    I found that the compiler will generate error if I use a pointer to
    pass a value to a function.
    Is there any way without pointer to prohibit type cast when using alias
    type?

    Go has no casts. You can convert the untyped literal to a typed value.
    Then the compiler rightfully complains:
    http://play.golang.org/p/6o2wHl7mUS
    --

    -j
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 10, '15 at 1:57p
activeNov 11, '15 at 2:19p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase