This is actually a really good question. To the spec-mobile!

https://golang.org/ref/spec#Calls

Except for one special case, arguments must be single-valued expressions
> assignable to the parameter types of F and are evaluated before the
> function is called.


Okay, so the arguments must be assignable. That must then mean that a value
of type TI is not assignable to a type int.

https://golang.org/ref/spec#Assignability

A value x is *assignable* to a variable
> <https://golang.org/ref/spec#Variables> of type T ("x is assignable to T")
> in any of these cases:
>
>    - x's type is identical to T.
>
>
>    - x's type V and T have identical underlying types
>    <https://golang.org/ref/spec#Types> and at least one of V or T is not
>    a named type <https://golang.org/ref/spec#Types>.
>
>
>    - …
>
> The second bullet point is the important one: Both V (TI) and T (int) have
the same underlying type (namely int), but both are named types (with int
being a predeclared identifier).

Now, if you contrast that with the slice-case, here []int is *not* a named
type, but a type-literal. That means, for []int and TIS, this second bullet
point will evaluate to true, as one of the two is not a named type, so a
TIS is assignable to an []int.


Personally, I think this is somewhat confusing (though it is in line with
the spec).


On Sat, Jul 16, 2016 at 10:15 PM, T L <tapir....@gmail.com> wrote:

>
> package main
>>
>> func fi(i int) {}
>> func fis(is []int) {}
>>
>> type TI int
>> type TIS []int
>>
>> func main() {
>>     var ti TI
>>     fi(ti) // cannot use ti (type TI) as type int in argument to fi
>>
>>     var tis TIS
>>     fis(tis) // no problems!
>> }
>>
>> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

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

Reply via email to