codekingpro/portable-devtools
114k
1// run2 3// Copyright 2021 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7// Test cases where a main dictionary is needed inside a generic function/method, because8// we are calling a method on a fully-instantiated type or a fully-instantiated function.9// (probably not common situations, of course)10 11package main12 13import (14 "fmt"15)16 17type C comparable18 19type value[T C] struct {20 val T21}22 23func (v *value[T]) test(def T) bool {24 return (v.val == def)25}26 27func (v *value[T]) get(def T) T {28 var c value[int]29 if c.test(32) {30 return def31 } else if v.test(def) {32 return def33 } else {34 return v.val35 }36}37 38func 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}44 