Skip to content

type alias with type that have type parameters doesn't work in go1.18rc #51516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamluzsi opened this issue Mar 7, 2022 · 5 comments
Closed

Comments

@adamluzsi
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18rc1 linux/amd64

Does this issue reproduce with the latest release?

go1.18rc1 seems to be the latest at the moment. (Mon Mar 7 09:50:11 AM CET 2022)

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/adamluzsi/.cache/go-build"
GOENV="/home/adamluzsi/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/adamluzsi/pkg/mod"
GONOPROXY="*"
GONOSUMDB="*"
GOOS="linux"
GOPATH="/home/adamluzsi"
GOPRIVATE="*"
GOPROXY="direct"
GOROOT="/home/adamluzsi/dist/golang.org/1.18rc"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/adamluzsi/dist/golang.org/1.18rc/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18rc1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/adamluzsi/src/github.com/adamluzsi/frameless/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4186401866=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package bug

import "io"

type Iterator[T any] interface {
	// Closer is required to make it able to cancel iterators where resource being used behind the scene
	// for all other case where the underling io is handled on higher level, it should simply return nil
	io.Closer
	// Next will ensure that Decode return the next item when it is executed
	Next() bool
	// Err return the cause if for some reason by default the More return false all the time
	Err() error
	// Value returns the current value from the iterator.
	Value() T
}

// fails with:
//   ./bug.go:19:22: cannot use generic type Iterator[T any] without instantiation
type IteratorAlias = Iterator

What did you expect to see?

Based on the following document guidance: go.googlesource.com/proposal#generic-type-aliases
I hoped that I can make a type alias for a type that has type parameters with refactoring purposes.

What did you see instead?

./bug.go:19:22: cannot use generic type Iterator[T any] without instantiation

Let me know if I can help with the issue,
and thank you very much for your support!

@mengzhuo
Copy link
Contributor

mengzhuo commented Mar 7, 2022

Thanks for the bug report.
As the error message says, this is a dup of #25838, #50729, #50925.
Closing as a dup.

@mengzhuo mengzhuo closed this as completed Mar 7, 2022
@adamluzsi
Copy link
Author

I fully trust your judgment @mengzhuo that those are related to this and not a type parameters related issue.
I wish you a great day! : )

@gofish
Copy link

gofish commented Apr 23, 2022

@mengzhuo are you certain this is a dup? This is not an issue of parameterized aliases, nor or type alias cycles. A trivial type alias to any parameterized type is rejected, contrary to the example in the generics proposal itself: https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#generic-type-aliases

Asking mostly because that "umbrella" issue sounds larger in scope and because a similar issue appears to have cropped up in the generics prototype, which @griesemer fixed easily there: #39768

https://go.dev/play/p/yC68DoFSKrl

package main

type Vector[T any] []T

type VectorAlias = Vector

var v VectorAlias[int]
./prog.go:5:20: cannot use generic type Vector[T any] without instantiation

Otherwise please ignore me and I will follow #25838.

@gofish
Copy link

gofish commented Apr 23, 2022

This is not an issue of parameterized aliases

Unless of course permitting an alias to a parameterized type implies a parameterized alias which could be subject to cycle issues? I'm new to go1.18 generics and am learning, thanks!

@griesemer
Copy link
Contributor

Per the spec:

If the type definition specifies type parameters, the type name denotes a generic type. Generic types must be instantiated when they are used.

Iterator is a generic type, it must be instantiated when used, including when used on the RHS of alias declaration.

This is working as intended.

@golang golang locked and limited conversation to collaborators Apr 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants