<div dir="auto"><div><div class="gmail_quote"><div dir="ltr">Ryan Joseph <<a href="mailto:ryan@thealchemistguild.com">ryan@thealchemistguild.com</a>> schrieb am Do., 14. Juni 2018, 10:03:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">type<br>
TEntity = record<br>
position: TVec2;<br>
end;<br>
TEntityPtr = ^TEntity;<br>
<br>
type<br>
TDoor = record<br>
with entity: TEntityPtr; // import entity namespace for entire TDoor scope<br>
state: boolean;<br>
end;<br>
<br>
var<br>
door: TDoor;<br>
begin<br>
door.entity := GetAvailableEntity; // pop an entity from storage<br>
door.position := V2(1, 1); // entity is “with” so we get access to its members (door.entity^.position)<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Something like that only leads to confusion. Pascal is a rather explicit language so manually declaring a Position property inside TDoor would be more in the spirit of the language. </div><div dir="auto"><br></div><div dir="auto">A possible alternative would be generic type helpers, like this (just an example, not working code):</div><div dir="auto"><br></div><div dir="auto">=== code beging ===</div><div dir="auto"><br></div><div dir="auto">type</div><div dir="auto"> generic TEntityUserHelper<T> = type helper for T</div><div dir="auto"> private</div><div dir="auto"> function GetPosition: TVec2D; inline;</div><div dir="auto"> procedure SetPosition(constref aValue: TVec2D); inline;</div><div dir="auto"> public</div><div dir="auto"> property Position: TVec2D read GetPosition write SetPosition;</div><div dir="auto"> end;</div><div dir="auto"><br></div><div dir="auto">function TEntityUserHelper.GetPosition: TVec2D;</div><div dir="auto">begin</div><div dir="auto"> Result := Self.Entity^.Position;</div><div dir="auto">end;</div><div dir="auto"><br></div><div dir="auto">procedure TEntityUserHelper.SetPosition(constref aValue: TVec2D);</div><div dir="auto">begin</div><div dir="auto"> Self.Entity^.Position := aValue;</div><div dir="auto">end;</div><div dir="auto"><br></div><div dir="auto">type</div><div dir="auto"> TDoor = record</div><div dir="auto"> Entity: PEntity;</div><div dir="auto"> State: Boolean;</div><div dir="auto"> end;</div><div dir="auto"> TDoorHelper = specialize TEntityUserHelper<TDoor>;</div><div dir="auto"><br></div><div dir="auto">var</div><div dir="auto"> door: TDoor;</div><div dir="auto">begin</div><div dir="auto"> // init door</div><div dir="auto"> door.Position := Vec2D(21, 42);</div><div dir="auto">end;</div><div dir="auto"><br></div><div dir="auto">=== code end ===</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
// works with function parameters also.<br>
// this is almost like a class helper or at very least mimics “self” in methods.<br>
<br>
procedure OpenDoor(with var door: TDoor);<br>
begin<br>
state := true; // with imports door namespace into entire function scope<br>
end;<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">The declaration of the function (in the interface section) would need to contain the "with" as all parameters have to match (and its only that parts are removed from the definition (default parameters, modifiers), but not the other way around) and thus the declaration would "spill" information about the function while not needing to. </div><div dir="auto">And again I think that as an expressive language the explicit usage of a "with" block is better than something like this. Please also not that there are people out there that advocate *against* the usage of "with" at all as it is likely to introduce bugs with it. </div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"></blockquote></div></div></div>