Arraymancer
Arraymancer copied to clipboard
Return `var` results when indexing
It would be handy to add indexing operators with return type var Tensor[T]
so that the result can be passed to a function for in-place operations. This may relate to #52.
Can you clarify what you mean by providing an example?
There are multiple ways to get a mutable value from a Tensor
:
- using mutable iterators: https://mratsim.github.io/Arraymancer/accessors.html#mitems.i%2CTensor%5BT%5D and friends
- using
[]
on a mutable tensor:-
[]
callsatIndex
here after desugaring: https://github.com/mratsim/Arraymancer/blob/master/src/arraymancer/tensor/private/p_accessors_macros_read.nim#L206 - which has both a mutable and non mutable overload: https://mratsim.github.io/Arraymancer/p_accessors.html#atIndex%2CTensor%5BT%5D%2Cvarargs%5Bint%5D_2
-
- using
atContiguousIndex
: https://mratsim.github.io/Arraymancer/accessors.html#atContiguousIndex%2CTensor%5BT%5D%2Cint_2 - using a
RawMutableView
: https://github.com/mratsim/Arraymancer/blob/master/src/arraymancer/laser/tensor/datatypes.nim#L20 by using https://mratsim.github.io/Arraymancer/datatypes.html#unsafe_raw_buf%2CTensor%5BT%3A+KnownSupportsCopyMem%5D%2Cstaticbool_2
And probably more than I'm not aware of right now.
I'm all for it but every time I tried I had nasty bugs that surfaced in the test suite. Some of which were corrupting the return data iirc.
In particular returning var seq[T]
is broken and if var seq[T] doesn't work, there is no chance for var Tensor[T]
to work
https://github.com/mratsim/Arraymancer/pull/420/commits/91850c5e6dd723d29b3b35072f57af4b7988d82d#diff-9827c41e5a724052d30ac0f444f15f465729f1116cdd2614a16f27fd3ad041c4R98
The workaround is to assign the result of indexing to a var first and pass that to the function.
@Vindaar When I tried using the []
operator in a place where a mutable result was needed I got a compiler-error because apparently it didn't return a mutable result.
@mratsim Is this due to some upstream bug with Nim?
Thanks for the workaround, I've been doing that. Reminds me of using Fortran...