Closed
Description
If we have a Pointer<Uint8>
and would like to get one that is 1 bytes further we can
Pointer<Uint8> p;
final p1 = p.elementAt(1);
final p1 = Pointer.fromAddress(p.address + 1);
None of which seem very nice.
Also the name elementAt
would suggest one actually gets the element at a certain offset, which it doesn't, it gets the address of the element at an offset.
In C one would simply
uint8_t* p = ...;
uint8_t* p10 = p + 1;
We should consider adding extension methods such as
extension on Pointer<Uint8> {
Pointer<Uint8> operator +(int offset) =>
Pointer.fromAddress(address + offset);
}
then the example will turn into
Pointer<Uint8> p;
final p10 = p + 1;
Even the following would work out of the box:
Pointer<Uint8> p;
p++;
If we do so, there may no longer be a need to have elementAt
and could deprecate & remove it.
There's a question whether we should support operator-
and if so, whether it should take two pointers and return the difference as an integer or it should take an int and return a pointer minus the offset.
/cc @dcharkes opinions?