Can I declare variables in-line instead of at the top of a function?
I have used visual basic about 5 years ago.
but i have started using delphi 5 years ago (when most developers jumped from delphi to visual studio) delphi is as easy as vb and at the same time it is rad and robust. Delphi is having many changes since pascal (eg : strings must be combined in a different way in pascal instead of just using + ) in order to make scripting faster.
but why in delphi we have to declare var on top , when i am writing many statements for a procedure i have to scroll up and declare a var and come 开发者_运维知识库down again. delphi is one of the best(some times one and only) MOST RAPID'est' IDE in the world but why they did not give support to declare variable anywhere just as in vb c# etc
It is possible to declare a var in the middle of a procedure using a code template and that is also enabled by default with Live templates in newer Delphi versions.
I just type var
and then press CTRL+J and enter the name and type the IDE does the rest.
No big deal for me.
A demonstration of live templates by Mike Rozlog:
http://edn.embarcadero.com/article/40284
If you have to scroll, your method is too long and probably needs to be refactored.
That said, in recent versions of Delphi you can declare a variable without moving your cursor by using the refactoring system.
Even better than Jens' Ctrl-J shortcut is this.
Write your code, e.g.:
I := 0;
Notice that "I" has a red squiggly line under it (meaning it is undeclared).
Click on "I" and type the shortcut: Shift+Ctrl+V
Up will pop a declare variable window which usually will have figured out the correct type for you.
If necessary change the type and hit enter to close the window.
I love this shortcut and use it all the time.
You asked this question: "why they did not give support to declare variable anywhere just as in vb c# etc"
Here's the answer: Because the language designers feel that declaring variables inline is confusing and difficult to read. If all variables are declared at the method level, their declarations are easy to find and their types are easy to determine. They believe that inline declarations make it difficult to track variables and their type.
Other language designers prefer the ability to declare variables inline, but a clean demarcation between code and variable declaration is one of the reasons many people prefer Pascal.
From Delphi version 10.3 Rio it is possible to declare variables and constants inline.
Even the type can be inferred.
Examples:
procedure Test1;
begin
var i : Integer;
i := 10;
WriteLn(i);
end;
Variables can be inlined and assigned:
procedure Test2;
begin
var i : Integer := 10;
WriteLn(i);
end;
Inlined variable types can be inferred:
procedure Test3;
begin
var i := 10;
WriteLn(i);
end;
Scope of inlined variables can be limited to begin/end blocks:
procedure Test4;
begin
var i : Integer := 10;
WriteLn(i);
if i < 20 then begin
var j := 30;
end;
Write(j); // <- Compiler error “Undeclared identifier: ‘j’”
end;
Inlined variables can be used inside for-in
or for-to
loops:
procedure Test5;
begin
for var i := 1 to 10 do
WriteLn(i);
end;
Constants can also be inlined and type can be inferred as normal constants:
procedure Test6;
begin
const i : Integer = 10;
const j = 20;
WriteLn(j);
end;
精彩评论