我可以内联声明变量而不是在函数顶部声明变量吗?
我大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
可以使用代码模板在过程中间声明 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
如果您必须滚动,则说明您的方法太长,可能需要重构。
也就是说,在最新版本的 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.
这个比 Jens 的 Ctrl-J 快捷键还要好。
编写您的代码,例如:
<前><代码>我:= 0;
注意“I”下面有一条红色波浪线(意味着它未声明)。
点击“I”,输入快捷键:Shift+Ctrl+V
向上会弹出声明变量窗口,通常会出现已经为您找出了正确的类型。
如果需要,更改类型并按 Enter 键关闭窗口。
我喜欢这个快捷方式并且一直使用它。
Even better than Jens' Ctrl-J shortcut is this.
Write your code, e.g.:
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.
您问了这个问题:“为什么他们不支持在任何地方声明变量,就像在 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.
从 Delphi 版本 10.3 Rio 开始,可以内联声明变量和常量。
甚至可以推断类型。
示例:
变量可以内联和赋值:
可以推断内联变量类型:
内联变量的范围可以限制在开始/结束块:
内联变量可以在
for-in
或for 内部使用-to
循环:常量也可以内联,并且类型可以推断为普通常量:
From Delphi version 10.3 Rio it is possible to declare variables and constants inline.
Even the type can be inferred.
Examples:
Variables can be inlined and assigned:
Inlined variable types can be inferred:
Scope of inlined variables can be limited to begin/end blocks:
Inlined variables can be used inside
for-in
orfor-to
loops:Constants can also be inlined and type can be inferred as normal constants: