1

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

1 Answer 1

2

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{}.

Sign up to request clarification or add additional context in comments.

2 Comments

function takes_boolean can accept both bare true and false. So now you have 2 named types.
bool and Boolean are both named types. However, bareword true and false are untyped constant literals, falling under the last bullet point in golang.org/ref/spec#Assignability See also golang.org/ref/spec#Constants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.