Generics and formatting

743 views
Skip to first unread message

Brian Candler

unread,
Jan 1, 2021, 12:50:13 PM1/1/21
to golang-nuts
Just a minor issue, but playing with this example:
- click "Run" and it works
- click "Format" and it fails with the following error:

prog.go:40:24: expected ']', found ',' (and 6 more errors)

Ian Lance Taylor

unread,
Jan 1, 2021, 1:40:36 PM1/1/21
to Brian Candler, golang-nuts
Hmmm, perhaps we failed to update gofmt the last time we updated the
go2go playground. The gofmt on the tip of the dev.go2go branch does
seem to work correctly. Can you open an issue at
https://golang.org/issue? Thanks.

Ian

Brian Candler

unread,
Jan 2, 2021, 1:27:30 PM1/2/21
to golang-nuts

Ian Lance Taylor

unread,
Jan 2, 2021, 2:58:33 PM1/2/21
to Brian Candler, golang-nuts
On Sat, Jan 2, 2021 at 10:28 AM 'Brian Candler' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> Raised as https://github.com/golang/go/issues/43470

Thanks.

Ian
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/57571d69-21d9-4d08-810e-32eb2647a928n%40googlegroups.com.

Brian Candler

unread,
Jan 5, 2021, 11:10:46 AM1/5/21
to golang-nuts
I don't know if this is expected, but this (bad) program causes the go2go playground to give "panic: assertion failed"

- I think it's the compiler itself that's panicing, not the runtime (since it doesn't get as far as the next line).

----

Aside: my general feeling so far is that the error messages given from go programs which misapply generic syntax can be very unhelpful.  Here's one example:

Error given: "missing ',' in type argument list"
Actual problem: a constraint was used in a place where it's not allowed

Brian Candler

unread,
Jan 5, 2021, 1:31:31 PM1/5/21
to golang-nuts
Here's another unhelpful error: https://go2goplay.golang.org/p/GDxiXiIIti7

Error: "prog.go2:7:11: all type parameters must be named"

Actual problem: the type parameters were named, but no constraint was given.

func blah[T1, T2](x T1) T2 {  // wrong
func blah[T1, T2 any](x T1) T2 {  // fixed

Ian Lance Taylor

unread,
Jan 5, 2021, 4:07:54 PM1/5/21
to Brian Candler, golang-nuts
On Tue, Jan 5, 2021 at 8:11 AM Brian Candler <b.ca...@pobox.com> wrote:
>
> I don't know if this is expected, but this (bad) program causes the go2go playground to give "panic: assertion failed"
>
> https://go2goplay.golang.org/p/VH5SLJNxw3X
>
> - I think it's the compiler itself that's panicing, not the runtime (since it doesn't get as far as the next line).

This is a bug in the type checker, which shouldn't panic. Want to
open an issue?


> Aside: my general feeling so far is that the error messages given from go programs which misapply generic syntax can be very unhelpful. Here's one example:
> https://go2goplay.golang.org/p/m9YUDBTWwpA
>
> Error given: "missing ',' in type argument list"
> Actual problem: a constraint was used in a place where it's not allowed

Here too issues will help. Thanks.

Ian

Brian Candler

unread,
Jan 6, 2021, 5:19:09 PM1/6/21
to Ivan Majeru, golang-nuts
On 06/01/2021 21:52, Ivan Majeru wrote:
If I understand right the panic is because the `m := NewMap[string, int]` is a function not a function call and after that you try to assign to this function `m[int]`. This works https://go2goplay.golang.org/p/NOaPH-dJyWd .

Sure, I know it was wrong, but I was still able to crash the compiler.

Previously I had tried using the (wrong) map-like syntax like this:

    m["foo"] = 123

and this gave a helpful error message (*):

"foo" (untyped string constant) is not a type

which, if you know about generic type parameters makes sense.

So out of interest, I changed it to see what would happen if I *did* put a type there:

    m[int] = 123

... and that caused the panic.

(*) Helpful-ish.  As you say, m is a function, indeed a non-generic one without type parameters. Trying to qualify a such a function with a type doesn't make sense.  Perhaps a better error would be "cannot use type parameters on a non-generic function" or similar.

Ivan Majeru

unread,
Jan 6, 2021, 5:19:31 PM1/6/21
to Brian Candler, golang-nuts
If I understand right the panic is because the `m := NewMap[string, int]` is a function not a function call and after that you try to assign to this function `m[int]`. This works https://go2goplay.golang.org/p/NOaPH-dJyWd .
--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages