@@ -23,15 +23,16 @@ type Numeric interface {
23
23
24
24
// numericAbs matches a struct containing a numeric type that has an Abs method.
25
25
type numericAbs [T Numeric ] interface {
26
- ~ struct { Value T }
26
+ ~ struct { Value_ T }
27
27
Abs () T
28
+ Value () T
28
29
}
29
30
30
31
// absDifference computes the absolute value of the difference of
31
32
// a and b, where the absolute value is determined by the Abs method.
32
33
func absDifference [T Numeric , U numericAbs [T ]](a , b U ) T {
33
- d := a .Value - b .Value
34
- dt := U {Value : d }
34
+ d := a .Value () - b .Value ()
35
+ dt := U {Value_ : d }
35
36
return dt .Abs ()
36
37
}
37
38
@@ -50,20 +51,29 @@ type Complex interface {
50
51
// orderedAbs is a helper type that defines an Abs method for
51
52
// a struct containing an ordered numeric type.
52
53
type orderedAbs [T orderedNumeric ] struct {
53
- Value T
54
+ Value_ T
54
55
}
55
56
56
57
func (a orderedAbs [T ]) Abs () T {
57
- if a .Value < 0 {
58
- return - a .Value
58
+ if a .Value_ < 0 {
59
+ return - a .Value_
59
60
}
60
- return a .Value
61
+ return a .Value_
62
+ }
63
+
64
+ // Field accesses through type parameters are disabled
65
+ // until we have a more thorough understanding of the
66
+ // implications on the spec. See issue #51576.
67
+ // Use accessor method instead.
68
+
69
+ func (a orderedAbs [T ]) Value () T {
70
+ return a .Value_
61
71
}
62
72
63
73
// complexAbs is a helper type that defines an Abs method for
64
74
// a struct containing a complex type.
65
75
type complexAbs [T Complex ] struct {
66
- Value T
76
+ Value_ T
67
77
}
68
78
69
79
func realimag (x any ) (re , im float64 ) {
@@ -82,13 +92,17 @@ func realimag(x any) (re, im float64) {
82
92
83
93
func (a complexAbs [T ]) Abs () T {
84
94
// TODO use direct conversion instead of realimag once #50937 is fixed
85
- r , i := realimag (a .Value )
95
+ r , i := realimag (a .Value_ )
86
96
// r := float64(real(a.Value))
87
97
// i := float64(imag(a.Value))
88
98
d := math .Sqrt (r * r + i * i )
89
99
return T (complex (d , 0 ))
90
100
}
91
101
102
+ func (a complexAbs [T ]) Value () T {
103
+ return a .Value_
104
+ }
105
+
92
106
// OrderedAbsDifference returns the absolute value of the difference
93
107
// between a and b, where a and b are of an ordered type.
94
108
func OrderedAbsDifference [T orderedNumeric ](a , b T ) T {
0 commit comments