Hello,

I should mention that I still don't read Go Language Specification 
(https://go.dev/ref/spec), due to my decision to first study Unicode and 
UTF-8, before reading it. It maybe strange thing to do, but I just how I 
need to deal with Unicode and UTF-8.

Regardless of that I quickly check few sections of it and I still baffled 
by what I find that compiler accepts. I write this to ask some gophers to 
help me find a way to think about this. I hope that you forgive me some 
stupid questions, I have so knowledge of Go, but still very little of 
"feeling" of Go.

I also have almost no knowledge about compilers. I just compiler front-end 
user.

In all code examples I use two following versions of Go.
go version go1.17.7 linux/amd64
go version go1.18beta2 linux/amd64

"Type parameters proposal" 
(https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md)
 
add new "[]" in new role. It allows to write
> func someFunction[T Constraint](...) { ... }
> type someType[T Constraint] ...
where "Constraint" is constraint on allowed type parameters and "T"" is a 
type parameter. (I hope that I didn't mess up names.)

As a byproduct of some obvious mistakes I find that
> type someInt[3] int
work fine in both Go 17 and 18beta2. It defines someInt as new type with 
underling type [3]int. This looks weird and pouting something like that in 
the working code is probably a sin, but this is not that big issue.

I'm now trying to play with compilers with this construction.
> type someDifferentInt[float64] int
In Go 1.17 I get this complain
> invalid array bound float64
> type float64 is not an expression
while in Go 1.18beta2 I get
> float64 (type) is not an expression
It probably need be this way, that with using "[T Constraint]" to define 
(?) type parameters, compiler can't says that it is "invalid array bound 
float64", but new error message is still less useful. I guess we need to 
live with that.

> type someOtherInt[float64 float64] int
This is awful thing to write, but I still perplexed by what compilers say.
Go 1.17:
> syntax error: unexpected float64, expecting ]
Go 1.18beta2:
> cannot use a type parameter as constraint

This example is to compare Go compilers with and without generics.
> type someAnotherInt[T any] int
Go 1.17:
> syntax error: unexpected any, expecting ]

Go 1.18beta2: it compile just fine. Can someone explain me, why compiler 
can't throw an error when it find unused type parameter? I like that in Go 
unused variable, or import, is compile time error and I would appreciate 
the same behavior with unused type parameters.

I need to go for a while, I will go back with more questions about what you 
can get when using "[]" in Go.

Best,
Kamil

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b085b991-e59e-4225-a4d6-36b2391e1dc2n%40googlegroups.com.

Reply via email to