Automatic type assertion

298 views
Skip to first unread message

Pablo Rozas Larraondo

unread,
Jun 9, 2016, 10:13:34 AM6/9/16
to golang-nuts
Does anyone know the reason why Go doesn't offer an automatic type assertion of an interface into its underlying type? I know this could be achieved by using a type switch or safe type assertions (b, ok := a.(int)) but I'm wondering why this operation was not simplified or included as a function in the reflection package.

I'm sure this has been discussed somewhere but I cannot find any information related.

Thank you,
Pablo

Vasko Zdravevski

unread,
Jun 9, 2016, 10:34:21 AM6/9/16
to golang-nuts
Hi Pablo,

I think the reason is that the 'dynamic type' ('underlying type' refers to type declarations https://golang.org/ref/spec#Types) of the variable might not be available in the package you try to do that type assertion.

Hope this example helps: https://play.golang.org/p/PX4yIjTkd6

- Vasko.

Jan Mercl

unread,
Jun 9, 2016, 10:36:55 AM6/9/16
to Pablo Rozas Larraondo, golang-nuts

On Thu, Jun 9, 2016 at 4:13 PM Pablo Rozas Larraondo <p.rozas....@gmail.com> wrote:

> Does anyone know the reason why Go doesn't offer an automatic type assertion of an interface into its underlying type? I know this could be achieved by using a type switch or safe type assertions (b, ok := a.(int)) but I'm wondering why this operation was not simplified or included as a function in the reflection package.

Please elaborate a bit more on what do you mean by "automatic type assertion" and please give some small example of how you would design the language feature and/or the reflection package functionality. Thanks.

--

-j

Euller Pereira

unread,
Jun 9, 2016, 10:50:54 AM6/9/16
to Jan Mercl, Pablo Rozas Larraondo, golang-nuts
I think he wants something like this:

var i int
var c interface{}

c = GetSomeInterfaceValue

i = c

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Konstantin Khomoutov

unread,
Jun 9, 2016, 11:09:08 AM6/9/16
to Pablo Rozas Larraondo, golang-nuts
On Fri, 10 Jun 2016 00:13:17 +1000
Pablo Rozas Larraondo <p.rozas....@gmail.com> wrote:

> Does anyone know the reason why Go doesn't offer an automatic type
> assertion of an interface into its underlying type? I know this could
> be achieved by using a type switch or safe type assertions
> (b, ok := a.(int)) but I'm wondering why this operation was not
> simplified or included as a function in the reflection package.

Do you mean that with your prospective feature the code

var b int
b = a

would have been equivalent to

var b int
b = a.(int)

and would generate a runtime panic if the variable "a" is not of type
int?

Pablo

unread,
Jun 9, 2016, 4:39:46 PM6/9/16
to Jan Mercl, golang-nuts
Sorry I should have been more clear exposing the problem. What I meant by "automatic type assertion" was something like:

If a is a variable of type interface{}:
b := a.(a.(type)) as a way of getting a's value in its own type.

As I'm writing this, I'm realising of the problem behind this though. The type of b would be unkown and reflection ,or a type switch, would be required to get its type again. So, there's no benefit in such a function I guess.

Thank you for your responses, sorry for the confusion and please comment if you see it in a different way.

Pablo

Jose Luis Aracil

unread,
Jun 23, 2016, 4:56:58 PM6/23/16
to golang-nuts, 0xj...@gmail.com

I use my own "automatic" type assertion library:

https://github.com/jaracil/ei

Pablo Rozas-Larraondo

unread,
Jun 28, 2016, 8:27:24 PM6/28/16
to golang-nuts, 0xj...@gmail.com
Nice one! Thanks for sharing

Pablo
Reply all
Reply to author
Forward
0 new messages