<div dir="ltr"><div dir="ltr">If the absence of dereferencing is the most important thing to you, you could do something like this:<div><br></div><div><div>program Example;</div><div><br></div><div>{$mode ObjFPC}{$H+}</div><div>{$modeswitch AdvancedRecords}</div><div><br></div><div>type</div><div> TVec2 = record</div><div> X, Y: Integer;</div><div> end;</div><div><br></div><div> TVec2Array = array of TVec2;</div><div><br></div><div> TVecArray = record</div><div> strict private</div><div> Values: TVec2Array;</div><div> public</div><div> class function Create(const ACapacity: Integer = 16): TVecArray; inline; static;</div><div> procedure Resize(const NewLength: Integer); inline;</div><div> property I: TVec2Array read Values write Values;</div><div> end;</div><div><br></div><div> class function TVecArray.Create(const ACapacity: Integer = 16): TVecArray;</div><div> begin</div><div> Result := Default(TVecArray);</div><div> SetLength(Result.Values, ACapacity);</div><div> end;</div><div><br></div><div> procedure TVecArray.Resize(const NewLength: Integer);</div><div> begin</div><div> SetLength(Values, NewLength);</div><div> end;</div><div><br></div><div>begin</div><div> with TVecArray.Create(0) do</div><div> begin</div><div> Resize(1);</div><div> I[0].X := 100;</div><div> I[0].Y := 100;</div><div> end;</div><div>end.</div></div><div><br></div><div>The only downside there is that you obviously don't quite get the real "default" property syntax.</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 10, 2018 at 11:06 PM Ryan Joseph <<a href="mailto:ryan@thealchemistguild.com">ryan@thealchemistguild.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I’d like to know if there is a solution to the problem of indexing into records using [] properties.<br>
<br>
In the example below I have an array of records (a dynamic array for the example) and I’d like to use the [] property to index into the values. The problem is because the array holds records I need to return a pointer to the record so I can access the fields. In the end the only problem is I need to dereference the pointer using ^.<br>
<br>
Are there any solutions we could propose to this? Some ideas:<br>
<br>
1) I looks like we could have a “var” modifier for function results but that concept has only ever existed in function params (like & in C++).<br>
<br>
function GetValue(i: integer): TVec2; var;<br>
<br>
2) Maybe a modifier added to GetValue which tells the compiler to call the function (and result) like a pointer when the indexer is used?<br>
<br>
====================================<br>
<br>
type<br>
TVec2 = record<br>
x, y: integer;<br>
end;<br>
TVec2Ptr = ^TVec2;<br>
TVec2Array = array of TVec2;<br>
<br>
type<br>
TVecArray = class<br>
values: TVec2Array;<br>
function GetValue(i: integer): TVec2Ptr;<br>
procedure Resize(newLength: integer);<br>
property Indexer[i: integer]: TVec2Ptr read GetValue; default;<br>
end;<br>
<br>
<br>
function TVecArray.GetValue(i: integer): TVec2Ptr;<br>
begin<br>
result := @values[i];<br>
end;<br>
<br>
procedure TVecArray.Resize(newLength: integer);<br>
begin<br>
SetLength(values, newLength);<br>
end;<br>
<br>
var<br>
arr: TVecArray;<br>
begin<br>
arr := TVecArray.Create;<br>
arr.Resize(1);<br>
arr[0]^.x := 100;<br>
arr[0]^.y := 100;<br>
end.<br>
<br>
Regards,<br>
Ryan Joseph<br>
<br>
_______________________________________________<br>
fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org" target="_blank">fpc-pascal@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal" rel="noreferrer" target="_blank">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal</a></blockquote></div>