On Fri, Jul 17, 2020 at 12:36 PM Josh Hoak <jrh...@gmail.com> wrote:
>
> I was playing around with trying to use generics to de-interface-ify a fuzzer 
> implementation, and I ran into some stumbling blocks.
>
> is it possible to perform type switches? It seems the answer is currently no 
> (example, design doc reference). Maybe there's a better way to handle the 
> following?
>
> type Primitive interface {
>   type int, string
> }
>
> func Randomize(type F Primitive)(f F, r *rand.Rand) F {
>   switch v := (interface{})(f).(type) {
>   case string:
>     return fmt.Sprintf("%s--%d", v, r.Int())
>   case int:
>     return v + r.Int()
>   default:
>     panic(fmt.Sprintf("I don't know about type %T!\n", v))
>   }
> }
>
> func main() {
>   r := rand.New(rand.NewSource(1))
>   fmt.Println(Randomize(int)(1, r))
> }

Yes, there currently is no way to switch on the underlying type of the
type argument, as discussed in the part of the design draft that you
link to.

Ian

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVPQDOiVKx%2BDD1%3DREf2OK4HcRgOjXkEiyrsPjXsxQG93Q%40mail.gmail.com.

Reply via email to