Here's how you implement generics in Go. Offered without comment.




  • if v, ok := val.(Sortable); ok {
    return &value_impl_Sortable{v}
    }
    if v, ok := val.(int); ok {
    return &value_impl_int{v}
    }
    if v, ok := val.(uint); ok {
    return &value_impl_uint{v}
    }
    if v, ok := val.(int8); ok {
    return &value_impl_int8{v}
    }
    if v, ok := val.(uint8); ok {
    return &value_impl_uint8{v}
    }
    if v, ok := val.(int16); ok {
    return &value_impl_int16{v}
    }
    if v, ok := val.(uint16); ok {
    return &value_impl_uint16{v}
    }
    if v, ok := val.(int32); ok {
    return &value_impl_int32{v}
    }
    if v, ok := val.(uint32); ok {
    return &value_impl_uint32{v}
    }
    if v, ok := val.(int64); ok {
    return &value_impl_int64{v}
    }
    if v, ok := val.(uint64); ok {
    return &value_impl_uint64{v}
    }
    panic(fmt.Sprintln("Unknown datatype ", reflect.TypeOf(val), " in ", val))



  •  Looks like the way of the future to me. Lemme guess, Go doesn't have Objective-C protocols nor C++ templates? But it does have interfaces, according to the documentation. So why the panic()?



  • Oh my god! People actually use Go?



  • @TGV said:

     Looks like the way of the future to me. Lemme guess, Go doesn't have Objective-C protocols nor C++ templates? But it does have interfaces, according to the documentation. So why the panic()?

    Interfaces are great until you want to use primitives, which only implement an empty interface.



  •  @Ben L. said:

    @TGV said:

     Looks like the way of the future to me. Lemme guess, Go doesn't have Objective-C protocols nor C++ templates? But it does have interfaces, according to the documentation. So why the panic()?

    Interfaces are great until you want to use primitives, which only implement an empty interface.

    That's why the panic() would have been a good place to call &value_whatever for any other object...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.