procedure Test;
begin
if Condition then
begin
var I: Integer;
I := CalculateSomething;
Persist(I);
end;
// many lines below...
Log(I); // Compile error!
end;
procedure NewTest;
var
MyDictionary: TObjectDictionary<string, TObjectList<TMyAmazingClass>>;
Pair: TPair<string, TObjectList<TMyAmazingClass>>;
List: TObjectList<TMyAmazingClass>;
begin
MyDictionary := TObjectDictionary<string, TObjectList<TMyAmazingClass>>.Create;
MyDictionary.Add('one', CreateList);
Pair := MyDictionary.ExtractPair('one');
List := Pair.Value;
ShowMessage(List.Count.ToString);
end;
使用内联变量和类型推断,您可以通过以下方式重写代码:
procedure NewTest;
begin
var MyDictionary := TObjectDictionary<string, TObjectList<TMyAmazingClass>>.Create;
MyDictionary.Add('one', CreateList);
var Pair := MyDictionary.ExtractPair('one');
var List := Pair.Value;
ShowMessage(List.Count.ToString);
end;
procedure TestInlineVars(const ACondition: Boolean);
begin
// BEFORE
if (ACondition) then
begin
var S := 'Inline String';
var I: IInterface := TInterfacedObject.Create;
var F: TFoo;
F.S := 'Managed Record';
end;
// AFTER
end;
请发表评论