[fpc-pascal] Dynamic arrays using management operators
Ryan Joseph
ryan at thealchemistguild.com
Wed Jun 6 04:51:00 CEST 2018
I fixed up my code on GitHub so the records members including the ref count are actually on the heap so the ref count is actually used now. :) There’s still 2 problems though that make dynamic arrays better:
1) the := operator for implicit arrays like [1,2,3] is bugged currently so requires a really long and ugly type cast (a := TIntArray.ValueArray([1,2,3]);) . Sven just got a := [] syntax working for dynamic arrays so record wrappers are harder to use in that regard. Hopefully that can be fixed.
2) dynamic arrays can index directly into records and write to fields but the [] operator overload can’t do this. That’s really annoying because you need to use calls like a.GetValuePtr(0)^.x := 1 to assign to an array of Vec2’s. Dynamic arrays could just do a[0].x := 1;
#2 is maybe not fixable for FPC without serious intervention. Basically it requires something like C++’s & alias which makes a pointer but assumes the pointer -> syntax. FPC can’t overload functions by result type so you’d have to choose one or the other and be stuck with always using ^ to dereference , even for read operations.
property ArrayValues[index:TArrayIndex]: T read GetValue; default;
property ArrayValuesPtr[index:TArrayIndex]: TArrayValuePtr read GetValuePtr;
So yeah dynamic arrays for records are still a better choice unless you don’t need to “index and write”.
Regards,
Ryan Joseph
More information about the fpc-pascal
mailing list