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 <https://go2goplay.golang.org/p/p3PRvsXAuXD>, design doc
reference
<https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md#identifying-the-matched-predeclared-type>).
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))
}

Thanks,
Josh

-- 
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/CAJoEjeXcjS1xBhsPM_CLA_zBwS-e0WdA%3D4ngQnRz6daReXeR7g%40mail.gmail.com.

Reply via email to