Slightly confusing error with type parameters

90 views
Skip to first unread message

Brian Candler

unread,
Aug 9, 2022, 7:31:18 AM8/9/22
to golang-nuts
I don't know if there's much that can be done about this, but thought I'd mention it anyway.

Suppose you're trying to make a parameterised type, like
type Elem[T any] blah...

but you forget the "any", and just write
type Elem[T] blah...

The error message, and the automatic reformatting you get, is a bit confusing:

type Elem [T]blah...
undeclared name T for array length

I guess this is a fundamental ambiguity in the language.  However, if an array length expression comes directly after a type name, and the array length itself consists of a single undeclared name, I wonder if the error message could give more of a hint?

undeclared name T for array length, or missing constraint for type parameter (e.g. "T any")

When there are multiple type parameters, it's fine:

Reply all
Reply to author
Forward
0 new messages