I made a playground if anyone wants to play with it and figure out how to 
test their ideas.

https://go.dev/play/p/h1nqHJAdVG8

On Sunday, September 10, 2023 at 12:24:54 AM UTC+1 Jason E. Aten wrote:

> Like how, specifically?
>
> func (m *Matrix[float64]) NanToZero() {
>     for i := 0; i < m.Nrow; i++ {
>         for j := 0; j < m.Ncol; j++ {
>             v := m.At(i, j).(float64)
>             if math.IsNaN(v) {
>                 m.Set(i, j, 0)
>             }
>         }
>     }
> }
> gives
> ./slurp1.go:944:9: invalid operation: cannot use type assertion on type 
> parameter value m.At(i, j) (value of type float64 constrained by Addable)
>
> On Sunday, September 10, 2023 at 12:04:53 AM UTC+1 Dan Kortschak wrote:
>
>> On Sat, 2023-09-09 at 15:39 -0700, Jason E. Aten wrote: 
>> > New to generics... how can I write a method for a Matrix[float64] 
>> > that uses a math function that expects a float64?   I get: 
>> > 
>> > ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
>> > constrained by Addable) as float64 value in argument to math.IsNaN 
>> > 
>> > Casting it to float64() does not change this error(!) 
>> > 
>> > here is the essentials: 
>> > 
>> > type Addable interface { 
>> >     ~complex128 | ~complex64 | ~float64 | ~float32 | 
>> >         ~byte | ~uint16 | ~uint32 | ~uint64 | 
>> >         ~int8 | ~int16 | ~int32 | ~int64 | ~int 
>> > } 
>> > 
>> > type Matrix[T Addable] struct { 
>> >     Nrow int 
>> >     Ncol int 
>> >     Dat        []T 
>> > } 
>> > ... 
>> > func (m *Matrix[float64]) NanToZero() { 
>> >     for i := 0; i < m.Nrow; i++ { 
>> >         for j := 0; j < m.Ncol; j++ { 
>> > 
>> >             if math.IsNaN(m.At(i, j)) {  // line 936, where the error 
>> > is; ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
>> > constrained by Addable) as float64 value in argument to math.IsNaN 
>> > 
>> >                 m.Set(i, j, 0) 
>> >             } 
>> >         } 
>> >     } 
>> > } 
>> > 
>> > go version go1.20.6 linux/amd64 
>>
>> You will probably need to type assert. 
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4f7e642b-ef44-4564-93e7-b2f7c10ffb6dn%40googlegroups.com.

Reply via email to