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...@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.
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
--
Somewhere on GitHub there is a discussion about unused type of arguments in the function. I think they agree that with benefits of hindsight you should be force to write func f(_ int) ... for any unused argument, that is need for function signature or some similar reason.
As I understand your code, because type inference works only on types of arguments, not on types of return values, you define a somepkg.ID to be just a string, that carries information about two additional types. This allows Go type inference algorithm infer type of return value from ID value and put it into place of return type of particular instantiation of generic function. Am I right?
You are almost surly more experience Go programmer than me, so anything that I write next may be just silly. Ian Lance Taylor give us one Go-proverb-like advice about generics in Go: "Write code, don't design types". Was your code created according to this rule?
My guts tell me that is not what I want to work with. It is some workaround of type interference algorithm, by putting too much information in one humble string-under-the-hood type.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/93a49e37-d8c8-452f-a5ef-985cf7fe5bb7n%40googlegroups.com.
"> type someDifferentInt[float64] int
I can't see why the parser can't see that this should (if anything) be interpreted as an array type declaration. To me, this seems like a regression. I'd suggest maybe filing an issue."
I think I know the reason. You can write
> type someArrayWithThreeInt [3]int
which is proper definition of new type that under the hood is just array of three int. Due to Go rules about names, white spaces and how Go parser work, this is equivalent (I think so) to
> type someArrayWithThreeInt[3] int
I don't know if I should filing the issue. There is already 5k+ issues and a lot of work for Go developers, I don't want to add another if there is not good enough reasons.
Since this can possible break some code, I heisted about that. Better gophers should look at that.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e42ce0ac-a2e9-4caf-8be2-83d75f2a2311n%40googlegroups.com.
Replacing (1) by
> type someOtherArray[int float64] [3]int
Go 1.17 gives
> syntax error: unexpected float64, expecting ]
In Go1.18beta this bloody nonsense compile!!!
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5ff44c39-2192-42f5-9b9a-a9d6f1626782n%40googlegroups.com.
On Fri, Feb 11, 2022 at 5:06 PM 'Axel Wagner' via golang-nuts
<golan...@googlegroups.com> wrote:
> FYI I went ahead and filed https://github.com/golang/go/issues/51145
I must be missing something because I still don't understand what the
problem is. The syntax `type A[B]C` can be reduced both in Go1.17 and
Go1.18 in only way - it's declaring an array type A, having B elements
of type C. Because B must be a constant expression, which `float64`,
unless redefined, is not, the error message produced is complete and
unambiguous.
Compiler error messages say what the error is, their purpose is not to
explain the specification. The intended reader of the error message is
supposed to understand the message in context of the language
specification, i.e. in this case be aware that the code declares an
array type, and expects an expression where a type `float64` was found
instead.
FTR: To have it explicit, it has nothing to do with type parameters,
they have syntax incompatible with this given example.
> type someAnotherType[uint float64] int
Go 1.17 gives
> syntax error: unexpected float64, expecting ]
In Go1.18beta this bloody nonsense compile!!!
I'm seriously lost here. Code below works in both Go 1.17 and Go 1.18beta2
> package main
>
> import "fmt"
>
> type someInterface[3] interface {
> SomeMethod()
> }
>
> func main() {
> var varSI someInterface
>
> fmt.Printf("varSI value: %v\n", varSI)
> fmt.Printf("varSI type: %T\n", varSI)
> }
and give in both cases result
> varSI value: [<nil> <nil> <nil>]
> varSI type: main.someInterface
I didn't find any way to assign some new value to "varSI", but this is already disturbing to me.
"So, no, this is not something that I made up to have an example or anything like that. I thought about a couple alternative ways to do it and they all don't work for one reason or another."
Thank you for the information. Do you check if performance of generic code is good? I always want to know if there is regression in performance.
"FWIW, here is code that assigns to varSI in two ways: https://go.dev/play/p/ypj6rQbpBBl"
From what I understand "type someInterface [3]interface{ SomeMethod() }" define interface array of three elements of "interface{ SomeMethod() }" and as such it should rather be name "someArrayOfInterfaces". Am I right?
Also, can someone give me an example, where it is desired to use such type?
Best,
Kamilniedziela, 13 lutego 2022 o 17:44:30 UTC+1 jake...@gmail.com napisał(a):On Friday, February 11, 2022 at 10:02:42 AM UTC-5 kziem...@gmail.com wrote:I'm seriously lost here. Code below works in both Go 1.17 and Go 1.18beta2
> package main
>
> import "fmt"
>
> type someInterface[3] interface {
> SomeMethod()
> }
>
> func main() {
> var varSI someInterface
>
> fmt.Printf("varSI value: %v\n", varSI)
> fmt.Printf("varSI type: %T\n", varSI)
> }
and give in both cases result
> varSI value: [<nil> <nil> <nil>]
> varSI type: main.someInterface
I didn't find any way to assign some new value to "varSI", but this is already disturbing to me.FWIW, here is code that assigns to varSI in two ways: https://go.dev/play/p/ypj6rQbpBBlNote that running this code in the playground will reformat ' type someInterface[3] interface' to the proper ' type someInterface [3]interface'. This is not a change in meaning, just a clarifying white space change made by `go fmt`.
--
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/0966efb7-7b73-4001-87f6-71a6362fe350n%40googlegroups.com.