FAQ
Hi fellow gophers,

Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
somebody must already thought about this. But can we do this:

type Number union {
     int
     float32
}

func (n Number) Max(m Number) Number {
     if n.(type) == int && m.(type) == int{
         if n.(int) > m.(int) {
             return n
         }
         return m
     }
     if float32(n) > float32(m) {
         return n
     }
     return m
}

func main() {
     var a int = 1
     var b float32 = 2
     fmt.Println(Number(a).Max(b))
}

Although it's not very clean, but I think it should have compile time
checks?
--
Jackie

--
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/groups/opt_out.

Search Discussions

  • peterGo at Jul 26, 2013 at 2:44 pm
    Jackie,

    It should look someting like this:

    package main

    import "fmt"

    type Number interface{}

    func Max(a, b Number) Number {
         var max Number
         // insert code
         return max
    }

    func main() {
         var a int = 1
         var b float32 = 2
         fmt.Println(Max(a, b))
    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.
  • peterGo at Jul 26, 2013 at 3:00 pm
    Jackie,

    As a practical matter, in Go, the solution is likely to be along the
    following lines:

    package main

    import (
         "fmt"
         "math"
    )

    func IMax(x, y int64) int64 {
         if x > y {
             return x
         }
         return y
    }

    func IMin(x, y int64) int64 {
         if x < y {
             return x
         }
         return y
    }

    func FMax(x, y float64) float64 {
         return math.Max(x, y)
    }

    func FMin(x, y float64) float64 {
         return math.Min(x, y)
    }

    func main() {
         var a int = 1
         var b float32 = 2.718
         fmt.Println(FMax(float64(a), float64(b)))
         var aa uint8 = 8
         var bb int32 = 32
         fmt.Println(IMax(int64(aa), int64(bb)))
    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.
  • Michael Jones at Jul 26, 2013 at 3:07 pm
    I proposed discriminated unions way back and there was no interest. My
    approach was to use a type switch to access them.

    On Fri, Jul 26, 2013 at 8:00 AM, peterGo wrote:

    Jackie,

    As a practical matter, in Go, the solution is likely to be along the
    following lines:

    package main

    import (
    "fmt"
    "math"
    )

    func IMax(x, y int64) int64 {
    if x > y {
    return x
    }
    return y
    }

    func IMin(x, y int64) int64 {
    if x < y {
    return x
    }
    return y
    }

    func FMax(x, y float64) float64 {
    return math.Max(x, y)
    }

    func FMin(x, y float64) float64 {
    return math.Min(x, y)

    }

    func main() {
    var a int = 1
    var b float32 = 2.718
    fmt.Println(FMax(float64(a), float64(b)))
    var aa uint8 = 8
    var bb int32 = 32
    fmt.Println(IMax(int64(aa), int64(bb)))

    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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/groups/opt_out.
  • Jackie Li at Jul 26, 2013 at 3:38 pm
    Thanks all,

    I know how to do it in current go. I'm not a full time programmer, but I've
    done a few small projects in go.

    I wrote this only because I saw several proposals about generics and just
    want to ask if anybody have thought about this and could this be a good
    idea of implementing generics.

    I remember reading about dropping union keyword long time ago. But I didn't
    find why.

    So do you guys think "union" can be used for generics?


    Thanks,
    Jackie

    On Fri, Jul 26, 2013 at 4:07 PM, Michael Jones wrote:

    I proposed discriminated unions way back and there was no interest. My
    approach was to use a type switch to access them.

    On Fri, Jul 26, 2013 at 8:00 AM, peterGo wrote:

    Jackie,

    As a practical matter, in Go, the solution is likely to be along the
    following lines:

    package main

    import (
    "fmt"
    "math"
    )

    func IMax(x, y int64) int64 {
    if x > y {
    return x
    }
    return y
    }

    func IMin(x, y int64) int64 {
    if x < y {
    return x
    }
    return y
    }

    func FMax(x, y float64) float64 {
    return math.Max(x, y)
    }

    func FMin(x, y float64) float64 {
    return math.Min(x, y)

    }

    func main() {
    var a int = 1
    var b float32 = 2.718
    fmt.Println(FMax(float64(a), float64(b)))
    var aa uint8 = 8
    var bb int32 = 32
    fmt.Println(IMax(int64(aa), int64(bb)))

    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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/groups/opt_out.



    --
    Jackie

    --
    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/groups/opt_out.
  • Jackie Li at Jul 26, 2013 at 3:41 pm
    Can you still find that post?

    Thanks

    On Fri, Jul 26, 2013 at 4:07 PM, Michael Jones wrote:

    I proposed discriminated unions way back and there was no interest. My
    approach was to use a type switch to access them.

    On Fri, Jul 26, 2013 at 8:00 AM, peterGo wrote:

    Jackie,

    As a practical matter, in Go, the solution is likely to be along the
    following lines:

    package main

    import (
    "fmt"
    "math"
    )

    func IMax(x, y int64) int64 {
    if x > y {
    return x
    }
    return y
    }

    func IMin(x, y int64) int64 {
    if x < y {
    return x
    }
    return y
    }

    func FMax(x, y float64) float64 {
    return math.Max(x, y)
    }

    func FMin(x, y float64) float64 {
    return math.Min(x, y)

    }

    func main() {
    var a int = 1
    var b float32 = 2.718
    fmt.Println(FMax(float64(a), float64(b)))
    var aa uint8 = 8
    var bb int32 = 32
    fmt.Println(IMax(int64(aa), int64(bb)))

    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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/groups/opt_out.



    --
    Jackie

    --
    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/groups/opt_out.
  • Jackie Li at Jul 26, 2013 at 4:10 pm
    ok, I think I've found the answer: http://golang.org/doc/faq#variant_types

    Thanks anyway!

    On Fri, Jul 26, 2013 at 4:40 PM, Jackie Li wrote:

    Can you still find that post?

    Thanks

    On Fri, Jul 26, 2013 at 4:07 PM, Michael Jones wrote:

    I proposed discriminated unions way back and there was no interest. My
    approach was to use a type switch to access them.

    On Fri, Jul 26, 2013 at 8:00 AM, peterGo wrote:

    Jackie,

    As a practical matter, in Go, the solution is likely to be along the
    following lines:

    package main

    import (
    "fmt"
    "math"
    )

    func IMax(x, y int64) int64 {
    if x > y {
    return x
    }
    return y
    }

    func IMin(x, y int64) int64 {
    if x < y {
    return x
    }
    return y
    }

    func FMax(x, y float64) float64 {
    return math.Max(x, y)
    }

    func FMin(x, y float64) float64 {
    return math.Min(x, y)

    }

    func main() {
    var a int = 1
    var b float32 = 2.718
    fmt.Println(FMax(float64(a), float64(b)))
    var aa uint8 = 8
    var bb int32 = 32
    fmt.Println(IMax(int64(aa), int64(bb)))

    }

    Peter
    On Friday, July 26, 2013 5:21:55 AM UTC-4, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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/groups/opt_out.



    --
    Jackie


    --
    Jackie

    --
    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/groups/opt_out.
  • Kevin Gillette at Jul 26, 2013 at 5:20 pm
    In the past, I've been a proponent of tagged union types, but agree it's
    not orthogonal enough to really work as a feature at this time. The ideal
    (considering many idioms) would have been:

    type Number union {
         int
         float32
    }

    func Max(n, m Number) Number {
         if n > m {
             return n
         }
         return m
    }

    Here, the compiler would abort if it encounters an operation that can't be
    performed on every possible member of the union type (so in this example,
    if Number could contain a slice, then compilation would abort on
    encountering the greater-than operation). The compiler would also insert a
    runtime check before the if statement and panic if the values of n and m
    don't have the same runtime types, while compilation would also abort if it
    can statically recognize and mismatch, such as with `Max(5, 3.8)`.

    Part of the trickiness of introducing unions is that it's not enough to
    introduce them alone -- an annotation indicating "same underlying type"
    really becomes necessary for frequent use, and would be messy to express.
    For example something like:

    `func Max(n : m Number) Number {}` might be used to require that two unions
    or interfaces have the same underlying type, verifiable at compile time
    where possible, and runtime (with panic) if necessary. But how would you
    express the same thing for:

    func Max(n Number, m ...Number) Number {}` (to get the maximum out of a set
    of one or more numbers)? There are two syntactical issues here: how to
    express that all values in a variadic have the same underlying type, and
    how to express that arguments of different technical type (Number and
    []Number) have the same underlying [element] type. There are ways around
    this (including not providing this facility at all), all of which introduce
    their own problems.
    On Friday, July 26, 2013 3:21:55 AM UTC-6, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.
  • Michael Jones at Jul 26, 2013 at 6:32 pm
    https://groups.google.com/d/topic/golang-nuts/VZ0rY12nzSM/discussion

    There are many ways to think of this, but my way is very much a combination
    of C do-it-yourself style with Pascal/Modula sensibilities wrapped in Go's
    existing metaphors.

    1. You have multiple data items whose use is temporally disjoint. (two
    variables, for example, that are mutually exclusive, say an integer count
    or a real mass).

    2. You have a "mode/type/status/gard/id" variable that records which of the
    alternatives is presently in use.

    3. You can set and get the mode.

    4. You can set and get the data corresponding to the present mode.

    5. You cannot access the (C "union") data other than as in #4.

    On Fri, Jul 26, 2013 at 10:19 AM, Kevin Gillette wrote:

    In the past, I've been a proponent of tagged union types, but agree it's
    not orthogonal enough to really work as a feature at this time. The ideal
    (considering many idioms) would have been:

    type Number union {
    int
    float32
    }

    func Max(n, m Number) Number {
    if n > m {
    return n
    }
    return m
    }

    Here, the compiler would abort if it encounters an operation that can't be
    performed on every possible member of the union type (so in this example,
    if Number could contain a slice, then compilation would abort on
    encountering the greater-than operation). The compiler would also insert a
    runtime check before the if statement and panic if the values of n and m
    don't have the same runtime types, while compilation would also abort if it
    can statically recognize and mismatch, such as with `Max(5, 3.8)`.

    Part of the trickiness of introducing unions is that it's not enough to
    introduce them alone -- an annotation indicating "same underlying type"
    really becomes necessary for frequent use, and would be messy to express.
    For example something like:

    `func Max(n : m Number) Number {}` might be used to require that two
    unions or interfaces have the same underlying type, verifiable at compile
    time where possible, and runtime (with panic) if necessary. But how would
    you express the same thing for:

    func Max(n Number, m ...Number) Number {}` (to get the maximum out of a
    set of one or more numbers)? There are two syntactical issues here: how to
    express that all values in a variadic have the same underlying type, and
    how to express that arguments of different technical type (Number and
    []Number) have the same underlying [element] type. There are ways around
    this (including not providing this facility at all), all of which introduce
    their own problems.
    On Friday, July 26, 2013 3:21:55 AM UTC-6, Jackie wrote:

    Hi fellow gophers,

    Sorry if I sound very ignorant, I'm not a full time developer. I'm sure
    somebody must already thought about this. But can we do this:

    type Number union {
    int
    float32
    }

    func (n Number) Max(m Number) Number {
    if n.(type) == int && m.(type) == int{
    if n.(int) > m.(int) {
    return n
    }
    return m
    }
    if float32(n) > float32(m) {
    return n
    }
    return m
    }

    func main() {
    var a int = 1
    var b float32 = 2
    fmt.Println(Number(a).Max(b))
    }

    Although it's not very clean, but I think it should have compile time
    checks?
    --
    Jackie
    --
    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/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 26, '13 at 9:22a
activeJul 26, '13 at 6:32p
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase