Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: type switch ranges #5743

Closed
eaigner opened this issue Jun 20, 2013 · 3 comments
Closed

spec: type switch ranges #5743

eaigner opened this issue Jun 20, 2013 · 3 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language

Comments

@eaigner
Copy link
Contributor

eaigner commented Jun 20, 2013

Consider the following example:

You have a function that accepts an argument list of type `interface{}`

    fmt(format string, args...interface{})

If you want to find out the type of an arg, we just loop over `args` and process each
`arg` in a type switch.

    swtich t := arg.(type) {
      case int:
        // handle int
      case int8:
        // handle int8 etc...
    }

In most cases you just want to know if a value is of a certain type class like `u/int`
to `u/int64`.
In that case you have to explicitly write a `case` statement for every type.

I don't know if that was explicitly avoided when defining the Go spec, but here is a
proposal for type ranges

    switch t := arg.(type) {
      case intish:
        // t is cast to an int64 here
      case floatish:
        // t is cast to a float64 here
      ...
    }
@remyoudompheng
Copy link
Contributor

Comment 1:

Did you consider using reflect.ValueOf(arg).Int() ?

Labels changed: added languagechange.

@eaigner
Copy link
Contributor Author

eaigner commented Jun 21, 2013

Comment 2:

Yes, but should reflection be necessary in this case?

@robpike
Copy link
Contributor

robpike commented Jun 21, 2013

Comment 3:

Use reflect. It's easy.
switch reflect.ValueOf(arg).Kind() {
case reflect.Int:
...
}

Status changed to WontFix.

@eaigner eaigner added wontfix LanguageChange Suggested changes to the Go language labels Jun 21, 2013
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language
Projects
None yet
Development

No branches or pull requests

4 participants