One could argue that this sidesteps the compiler's type checking system, deferring it to runtime (which a number of people consider dangerous). However, tools are being developed to type check these things, and until Go does support typechecked generics, unfortunately this will be the way it has to be.
Currently, the tensor package supports limited type of genericity - limited to a tensor of any primitive type.
Go does have typechecked generics as of 1.18. I'm sure you've thought more about this than I have, so I'm very curious to hear your thoughts on templating the Tensor interface by T, so you'd write Tensor[float32] for example. It would allow writing functions operating only on float tensors like so: func dostuff[T Float](t Tensor[T]) Tensor[T]. Another consequence would be Apply taking a function of func (v T) T, rather than func (v interface{}) interface{}).
Ref this quote from https://github.com/gorgonia/tensor?tab=readme-ov-file#generic-features:
Go does have typechecked generics as of 1.18. I'm sure you've thought more about this than I have, so I'm very curious to hear your thoughts on templating the Tensor interface by T, so you'd write
Tensor[float32]
for example. It would allow writing functions operating only on float tensors like so:func dostuff[T Float](t Tensor[T]) Tensor[T]
. Another consequence would beApply
taking a function offunc (v T) T
, rather thanfunc (v interface{}) interface{})
.