| 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 |
// Test cases where a main dictionary is needed inside a generic function/method, because |
| 8 |
// we are calling a method on a fully-instantiated type or a fully-instantiated function. |
| 9 |
// (probably not common situations, of course) |
| 10 |
|
| 11 |
package main |
| 12 |
|
| 13 |
import ( |
| 14 |
"fmt" |
| 15 |
) |
| 16 |
|
| 17 |
type C comparable |
| 18 |
|
| 19 |
type value[T C] struct { |
| 20 |
val T |
| 21 |
} |
| 22 |
|
| 23 |
func (v *value[T]) test(def T) bool { |
| 24 |
return (v.val == def) |
| 25 |
} |
| 26 |
|
| 27 |
func (v *value[T]) get(def T) T { |
| 28 |
var c value[int] |
| 29 |
if c.test(32) { |
| 30 |
return def |
| 31 |
} else if v.test(def) { |
| 32 |
return def |
| 33 |
} else { |
| 34 |
return v.val |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
func main() { |
| 39 |
var s value[string] |
| 40 |
if got, want := s.get("ab"), ""; got != want { |
| 41 |
panic(fmt.Sprintf("get() == %d, want %d", got, want)) |
| 42 |
} |
| 43 |
} |