mirror of https://github.com/golang/go.git
go/types: first cut at type-checking type parameters with operators
- implemented for comparisons for now - can type-check generic min - also: rebased on top of Go tip Change-Id: Id35582a59c4cddcb2b4ae9c7d7154ef8e41580ab
This commit is contained in:
parent
dc4f9fb373
commit
cfbeecaabb
|
|
@ -62,8 +62,13 @@ func isUntyped(typ Type) bool {
|
|||
}
|
||||
|
||||
func isOrdered(typ Type) bool {
|
||||
t, ok := typ.Underlying().(*Basic)
|
||||
return ok && t.info&IsOrdered != 0
|
||||
switch t := typ.Underlying().(type) {
|
||||
case *Basic:
|
||||
return t.info&IsOrdered != 0
|
||||
case *TypeParam:
|
||||
return t.contr.ifaceAt(t.index).is(isOrdered)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isConstType(typ Type) bool {
|
||||
|
|
|
|||
|
|
@ -116,3 +116,16 @@ var _ T2(int32)
|
|||
var _ T2 /* ERROR not satisfied */ (int64)
|
||||
var _ T2(int)
|
||||
var _ T2(struct{x int})
|
||||
|
||||
// Use of contracts in parameterized functions
|
||||
|
||||
contract SignedInteger(T) {
|
||||
T int8, int16, int32, int64, int
|
||||
}
|
||||
|
||||
func min(type T SignedInteger)(x, y T) T {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
|
@ -254,6 +254,16 @@ type Interface struct {
|
|||
types []Type // for contracts
|
||||
}
|
||||
|
||||
// is reports whether interface t represents types that all satisfy pred.
|
||||
func (t *Interface) is(pred func(Type) bool) bool {
|
||||
for _, t := range t.types {
|
||||
if !pred(t) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// emptyInterface represents the empty (completed) interface
|
||||
var emptyInterface = Interface{allMethods: markComplete}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue