See https://github.com/golang/go/issues/45380 for a proposal for a language
feature that would allow this. In the meantime, you can convert to any, and
type switch on that:

func Foo[T allowedTypes](arg T) {
    switch t := any(arg).(type) {
    case int64:
...


On Fri, 12 Aug 2022, 00:40 'Matt Rosencrantz' via golang-nuts, <
golang-nuts@googlegroups.com> wrote:

> I'd really love to be able to do:
>
> type allowedTypes{
>   int64|float64|string
> }
>
> func Foo[T allowedTypes](arg T) {
>     switch t := arg {
>        case int64:
>            // something
>        case string:
>           // something completely different
>    }
> }
>
> As a way of having the type checker disallow other types from being passed
> so I don't have to return an error for incorrect types.  I guess this would
> be the same as asking for typesets to be allowed in normal interfaces like:
>
> func Foo(arg allowedTypes) {...}
>
> Is there another way to achieve this? Is there a reason it is not possible?
> Thanks!
> Matt
>
>
>
> --
> 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/870bc9bd-2fcf-4598-9287-2da7adbc524an%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/870bc9bd-2fcf-4598-9287-2da7adbc524an%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAJhgachvjCoU%3DcKyvTpQnNL_mKNmBBiXh3gNtLPCgq9vC3Tv4w%40mail.gmail.com.

Reply via email to