The goal is to create a type called TURLString to be called as follows:
var
newURl : TURLString;
begin
newURL.Append('http://').Append('www.thehost.com').Append('path/on/server').Append('?');
...lots of app logic...
newURL.AppendParam('name', 'value').Append('#').AppendParam('name', 'value');
...more params added...
result := httpClient.Get(newURL);
end;
With TURLString defined like this (note its a record):
//from actual code used
TURLString = record
private
FString : string;
public
function Append(APart : string) : TURLString;
function AppendParam(AParam, AValue : string) : TURLString;
end;
function TURLString.Append(APart: string) : TURLString;
begin
FString := FString + APart;
result := self;
end;
function TURLString.AppendParam(AParam, AValue: string): TURLString;
begin
if (not Empty) then
FString := FString + URL_AMB;
FString := FString + AParam + '=' + AValue;
result := self;
end;
When stepping through the fluid calls, the values are appended but when exiting they revert to the first string passed into the first append call and newURL is equal to ‘http://’ while debugging the append call you see ‘http://www.thehost.com/path/on/server?name=value#name=value’.
Is this concept possible with a record?
If you use a value type like a record then you need to assign the final returned result to a variable:
If you use a reference type like a class instance, then you can use the syntax that you used in your question.
The reference type approach treats the data type as mutable, whereas for value types you are best implementing an immutable data type.