Source file test/typeparam/typeswitch4.go

     1  // run
     2  
     3  // Copyright 2021 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  type I interface{ foo() int }
    10  type J interface {
    11  	I
    12  	bar()
    13  }
    14  
    15  type myint int
    16  
    17  func (x myint) foo() int { return int(x) }
    18  
    19  type myfloat float64
    20  
    21  func (x myfloat) foo() int { return int(x) }
    22  
    23  type myint32 int32
    24  
    25  func (x myint32) foo() int { return int(x) }
    26  func (x myint32) bar()     {}
    27  
    28  func f[T I](i I) {
    29  	switch x := i.(type) {
    30  	case T, myint32:
    31  		println("T/myint32", x.foo())
    32  	default:
    33  		println("other", x.foo())
    34  	}
    35  }
    36  func main() {
    37  	f[myfloat](myint(6))
    38  	f[myfloat](myfloat(7))
    39  	f[myfloat](myint32(8))
    40  	f[myint32](myint32(9))
    41  	f[myint](myint32(10))
    42  	f[myint](myfloat(42))
    43  	f[I](myint(10))
    44  	f[J](myint(11))
    45  	f[J](myint32(12))
    46  }
    47  

View as plain text