canadadry
canadadry

Reputation: 8443

Assignability of function parameters in golang

Runnable on playground

type Boolean bool

func takes_bool(b bool) {
    fmt.Printf("%t\n", b)
}

func takes_boolean(b Boolean) {
    fmt.Printf("%t\n", b)
}

When I invoke the following:

takes_bool(Boolean(false))
takes_bool(Boolean(true))

I get:

cannot use Boolean(false) (type Boolean) as type bool in function argument
cannot use Boolean(true) (type Boolean) as type bool in function argument

The rules on assignability seems to NOT disallow it i.e. at least one is not a named type & both have the same underlying type:

type Boolean bool

vs

bool

Upvotes: 1

Views: 1322

Answers (1)

Evan
Evan

Reputation: 6545

On a careful reading of http://golang.org/ref/spec#Types it seems bool is considered a named type (as are int and float and friends). The phrase "unnamed types" only refer to type literals like interface{} and struct{}.

Upvotes: 2

Related Questions