mirror of
https://github.com/golang/go
synced 2024-11-02 11:50:30 +00:00
a70eb2c9f2
Get instantiatiated generic types working with interfaces, including typechecking assignments to interfaces and instantiating all the methods properly. To get it all working, this change includes: - Add support for substituting in interfaces in subster.typ() - Fill in the info for the methods for all instantiated generic types, so those methods will be available for later typechecking (by the old typechecker) when assigning an instantiated generic type to an interface. We also want those methods available so we have the list when we want to instantiate all methods of an instantiated type. We have both for instantiated types encountered during the initial noder phase, and for instantiated types created during stenciling of a function/method. - When we first create a fully-instantiated generic type (whether during initial noder2 pass or while instantiating a method/function), add it to a list so that all of its methods will also be instantiated. This is needed so that an instantiated type can be assigned to an interface. - Properly substitute type names in the names of instantiated methods. - New accessor methods for types.Type.RParam. - To deal with generic types which are empty structs (or just don't use their type params anywhere), we want to set HasTParam if a named type has any type params that are not fully instantiated, even if the type param is not used in the type. - In subst.typ() and elsewhere, always set sym.Def for a new forwarding type we are creating, so we always create a single unique type for each generic type instantiation. This handles recursion within a type, and also recursive relationships across many types or methods. We remove the seen[] hashtable, which was serving the same purpose, but for subst.typ() only. We now handle all kinds of recursive types. - We don't seem to need to force types.CheckSize() on created/substituted generic types anymore, so commented out for now. - Add an RParams accessor to types2.Signature, and also a new exported types2.AsSignature() function. Change-Id: If6c5dd98427b20bfe9de3379cc16f83df9c9b632 Reviewed-on: https://go-review.googlesource.com/c/go/+/298449 Run-TryBot: Dan Scales <danscales@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Dan Scales <danscales@google.com> Reviewed-by: Robert Griesemer <gri@golang.org>
101 lines
2.1 KiB
Go
101 lines
2.1 KiB
Go
// run -gcflags=-G=3
|
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// lice
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
// Overriding the predeclare "any", so it can be used as a type constraint or a type
|
|
// argument
|
|
type any interface{}
|
|
|
|
type _Function[a, b any] interface {
|
|
Apply(x a) b
|
|
}
|
|
|
|
type incr struct{ n int }
|
|
|
|
func (this incr) Apply(x int) int {
|
|
return x + this.n
|
|
}
|
|
|
|
type pos struct{}
|
|
|
|
func (this pos) Apply(x int) bool {
|
|
return x > 0
|
|
}
|
|
|
|
type compose[a, b, c any] struct {
|
|
f _Function[a, b]
|
|
g _Function[b, c]
|
|
}
|
|
|
|
func (this compose[a, b, c]) Apply(x a) c {
|
|
return this.g.Apply(this.f.Apply(x))
|
|
}
|
|
|
|
type _Eq[a any] interface {
|
|
Equal(a) bool
|
|
}
|
|
|
|
type Int int
|
|
|
|
func (this Int) Equal(that int) bool {
|
|
return int(this) == that
|
|
}
|
|
|
|
type _List[a any] interface {
|
|
Match(casenil _Function[_Nil[a], any], casecons _Function[_Cons[a], any]) any
|
|
}
|
|
|
|
type _Nil[a any] struct{
|
|
}
|
|
|
|
func (xs _Nil[a]) Match(casenil _Function[_Nil[a], any], casecons _Function[_Cons[a], any]) any {
|
|
return casenil.Apply(xs)
|
|
}
|
|
|
|
type _Cons[a any] struct {
|
|
Head a
|
|
Tail _List[a]
|
|
}
|
|
|
|
func (xs _Cons[a]) Match(casenil _Function[_Nil[a], any], casecons _Function[_Cons[a], any]) any {
|
|
return casecons.Apply(xs)
|
|
}
|
|
|
|
type mapNil[a, b any] struct{
|
|
}
|
|
|
|
func (m mapNil[a, b]) Apply(_ _Nil[a]) any {
|
|
return _Nil[b]{}
|
|
}
|
|
|
|
type mapCons[a, b any] struct {
|
|
f _Function[a, b]
|
|
}
|
|
|
|
func (m mapCons[a, b]) Apply(xs _Cons[a]) any {
|
|
return _Cons[b]{m.f.Apply(xs.Head), _Map[a, b](m.f, xs.Tail)}
|
|
}
|
|
|
|
func _Map[a, b any](f _Function[a, b], xs _List[a]) _List[b] {
|
|
return xs.Match(mapNil[a, b]{}, mapCons[a, b]{f}).(_List[b])
|
|
}
|
|
|
|
func main() {
|
|
var xs _List[int] = _Cons[int]{3, _Cons[int]{6, _Nil[int]{}}}
|
|
// TODO(danscales): Remove conversion calls in next two, needed for now.
|
|
var ys _List[int] = _Map[int, int](_Function[int, int](incr{-5}), xs)
|
|
var xz _List[bool] = _Map[int, bool](_Function[int, bool](pos{}), ys)
|
|
cs1 := xz.(_Cons[bool])
|
|
cs2 := cs1.Tail.(_Cons[bool])
|
|
_, ok := cs2.Tail.(_Nil[bool])
|
|
if cs1.Head != false || cs2.Head != true || !ok {
|
|
panic(fmt.Sprintf("got %v, %v, %v, expected false, true, true",
|
|
cs1.Head, cs2.Head, ok))
|
|
}
|
|
}
|