在Delphi中命名/可选参数? [英] Named/optional parameters in Delphi?

查看:120
本文介绍了在Delphi中命名/可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其中一个Delphi演示应用程序中,我偶然发现一些语法,我不知道接受的Delphi编译器:

  // ...... \Demos\DelphiWin32\VCLWin32\ActiveX\OleAuto\SrvComp\Word\ 
// Main.pas,第109行

Docs.Add(NewTemplate:= True); //注意分配

我似乎无法再现这种类型的参数传入我自己的代码,我从来没有看到有人使用它。所以这些是我的问题:




  • 我可以在常规方法中使用这个,它是Delphi语言还是这是一些编译器,用于自动化对象?


  • 为了能够使用它,需要什么?


  • 像C#4的命名和可选参数?








附加信息:当

方法的许多可选参数时,我通常会通过
记录或简单类,但是看起来我不会
需要这个语法。我知道
的默认参数值,但是
的有用性是有限的,因为
不能为省略的
提供任何参数。在JavaScript
我一直使用这个命名参数样式
(不同于
语法),它的功能很强大。



解决方案

显然,Delphi语言支持命名参数,因为它们出现在示例Delphi代码中。 Delphi支持自动化对象上的命名参数,它们是实现 IDispatch 接口的对象。 对类型的参数和返回类型可以有;特别是,他们不能是Delphi类。



我不认为您从命名参数中获得的便利将超过您所需要的性能方法调用路由通过 IDispatch.Invoke 方法。呼叫也可能需要使用 GetIDsOfNames 。你没有看到更多的代码,因为后期绑定通常是人们试图避免的。尽可能使用早期绑定,以避免查找调度ID和间接方法调用的成本。



Delphi通过允许默认值来支持非自动化代码中的可选参数。只要您也省略所有后续参数的实际参数,您可以省略具有默认值的任何参数的实际参数 - 编译器确保函数的声明允许。



我认为可选参数被高估了。他们为(1)人编写代码节省时间,但不能为(许多)人阅读代码。任何阅读它需要知道什么是默认值是任何未指定的参数,所以您也可以直接提供所有的值。


In one of the Delphi demo applications, I've stumbled upon some syntax that I didn't know the Delphi compiler accepted:

// ......\Demos\DelphiWin32\VCLWin32\ActiveX\OleAuto\SrvComp\Word\    
// Main.pas, line 109

Docs.Add(NewTemplate := True); // note the assignment

I can't seem to reproduce this type of parameter passing in my own code, and I never see anyone use it. So these are my questions:

  • Can i use this in "normal" methods and is it part of "the Delphi Language", or is this some compiler hack for automation objects?

  • What's needed in order to be able to use this?

  • Is this anything like C#4's named and optional parameters?


Additional information: I usually pass records or simple classes when there are many optional parameters to methods, but it looks like I wouldn't need that with this syntax. I'm aware of default parameter values, but their usefulness is limited because you cannot provide any parameters to the right of an omitted one. In JavaScript I'm using this named parameter style all the time (be it with different syntax), and it's powerful.

解决方案

Clearly the Delphi language supports named parameters since they appear right there in sample Delphi code. Delphi supports named parameters on automation objects, which are objects that implement the IDispatch interface. There are restrictions on the types the parameters and return types can have; in particular, they can't be Delphi classes.

I don't think the convenience you seek from named parameters would outweigh the performance hit you'd take by having every method call routed through the IDispatch.Invoke method. A call may also need to use GetIDsOfNames first. You don't see this in more code because late binding is usually something people try to avoid. Use early binding whenever possible to avoid the cost of looking up dispatch IDs and indirect method invocations.

Delphi supports optional parameters in non-automation code by allowing default values. You can omit the actual parameters for any parameter with a default value as long as you also omit the actual parameters of all subsequent parameters — the compiler ensures that a function's declaration allows for that.

I think optional parameters are overrated. They save time for the (one) person writing the code, but not for the (many) people reading the code. Whoever's reading it needs to know what the default values will be of any unspecified parameters, so you may as well just provide all the values explicitly anyway.

这篇关于在Delphi中命名/可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆