On Thursday, September 29, 2016 at 11:48:34 PM UTC+8, andrey mirtchovski
wrote:
>
> > I just expect type asserting on a non-nil interface value shouldn't
> panic.
>
> of course it should panic if the interface holds an unexpected value
> and you're not checking for correctness with the comma-ok pattern.
>
> Go is a statically typed language and by asserting the wrong type on a
> variable you have an created an obviously incorrectly typed program.
> If the compiler was able to deduce that the interface was holding an
> incorrect value at compile time it would never have allowed such a
> program to be compiled.
>
> the following two examples both look ridiculous to a Go programmer:
>
> var i int = 42
> var b = bool(i)
> // b is now false
>
> vs
>
> var i = interface{}(42)
> var b = i.(bool)
> // b is now false
>
> One shouldn't be able to turn 42 into a "false" in Go. maybe in other
> languages, but not Go.
>
ok, maybe type assertion is really different from map element access, range
over slice/map and read from channel.
The later three can be viewed as special form of function callings, but
type assertion can't.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.