Source file test/typeparam/typeswitch7.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  func f[T any](i interface{foo()}) {
    10  	switch i.(type) {
    11  	case interface{bar() T}:
    12  		println("barT")
    13  	case myint:
    14  		println("myint")
    15  	case myfloat:
    16  		println("myfloat")
    17  	default:
    18  		println("other")
    19  	}
    20  }
    21  
    22  type myint int
    23  func (myint) foo() {
    24  }
    25  func (x myint) bar() int {
    26  	return int(x)
    27  }
    28  
    29  type myfloat float64
    30  func (myfloat) foo() {
    31  }
    32  
    33  func main() {
    34  	f[int](nil)
    35  	f[int](myint(6))
    36  	f[int](myfloat(7))
    37  }
    38  

View as plain text