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/666e12c82257dd5dc9e207767061eaa65d2ffa13.camel%40kortschak.io.

Reply via email to