我可以内联声明变量而不是在函数顶部声明变量吗?

发布于 2024-10-01 01:05:57 字数 339 浏览 4 评论 0原文

我大约 5 年前使用过 Visual Basic。

但我从 5 年前开始使用 delphi(当时大多数开发人员从 delphi 跳到 Visual Studio)delphi 和 VB 一样简单,同时又非常强大。自 pascal 以来,Delphi 发生了许多变化(例如:在 pascal 中必须以不同的方式组合字符串,而不仅仅是使用 + ),以便使脚本编写更快。

但是为什么在delphi中我们必须在顶部声明var,当我为一个过程编写许多语句时,我必须向上滚动并声明一个var然后再次向下滚动。 delphi 是世界上最好的(有时是唯一的)最快速的 IDE 之一,但为什么他们不支持在任何地方声明变量,就像在 vb c# 等中一样

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风筝有风,海豚有海 2024-10-08 01:05:57

可以使用代码模板在过程中间声明 var,并且在较新的 Delphi 版本中,Live 模板也默认启用该功能。

我只需输入 var,然后按 CTRL+J 并输入名称,然后键入 IDE 即可完成剩下的工作。

对我来说没什么大不了的。

Mike Rozlog 的实时模板演示:

http://edn.embarcadero.com/article/40284

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

萌︼了一个春 2024-10-08 01:05:57

如果您必须滚动,则说明您的方法太长,可能需要重构。

也就是说,在最新版本的 Delphi 中,您可以使用以下方法声明变量而无需移动光标重构系统

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.

并安 2024-10-08 01:05:57

这个比 Jens 的 Ctrl-J 快捷键还要好。

  1. 编写您的代码,例如:

    <前><代码>我:= 0;

  2. 注意“I”下面有一条红色波浪线(意味着它未声明)。

  3. 点击“I”,输入快捷键:Shift+Ctrl+V

  4. 向上会弹出声明变量窗口,通常会出现已经为您找出了正确的类型。

  5. 如果需要,更改类型并按 Enter 键关闭窗口。

我喜欢这个快捷方式并且一直使用它。

Even better than Jens' Ctrl-J shortcut is this.

  1. Write your code, e.g.:

    I := 0;
    
  2. Notice that "I" has a red squiggly line under it (meaning it is undeclared).

  3. Click on "I" and type the shortcut: Shift+Ctrl+V

  4. Up will pop a declare variable window which usually will have figured out the correct type for you.

  5. If necessary change the type and hit enter to close the window.

I love this shortcut and use it all the time.

柠北森屋 2024-10-08 01:05:57

您问了这个问题:“为什么他们不支持在任何地方声明变量,就像在 vb c# 等中一样”

答案如下:因为语言设计者认为内联声明变量令人困惑且难以阅读。如果所有变量都在方法级别声明,则很容易找到它们的声明并且很容易确定它们的类型。他们认为内联声明使得跟踪变量及其类型变得困难。

其他语言设计者更喜欢内联声明变量的能力,但代码和变量声明之间的清晰界限是许多人喜欢 Pascal 的原因之一。

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.

陪你搞怪i 2024-10-08 01:05:57

从 Delphi 版本 10.3 Rio 开始,可以内联声明变量和常量。

甚至可以推断类型。

示例:

procedure Test1;
begin
  var i : Integer;
  i := 10;
  WriteLn(i);
end;

变量可以内联和赋值:

procedure Test2;
begin
  var i : Integer := 10;
  WriteLn(i);
end;

可以推断内联变量类型:

procedure Test3;
begin
  var i := 10;
  WriteLn(i);
end;

内联变量的范围可以限制在开始/结束块:

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;

内联变量可以在 for-infor 内部使用-to 循环:

procedure Test5;
begin
  for var i := 1 to 10 do
    WriteLn(i);
end;

常量也可以内联,并且类型可以推断为普通常量:

procedure Test6;
begin
  const i : Integer = 10;
  const j = 20;
  WriteLn(j);
end;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文