views:

836

answers:

4

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.

+2  A: 

If you declare your procedure like so:

procedure DoSomething(AParam : integer = 0);

... it will assume a value of 0 for the parameter if it isn't given. As I recall, parameters with default values have to be at the end of the call, so like this:

procedure DoSomething(AFirstParam : string; AParam : integer = 0);

not like this:

procedure DoSomething(AParam : integer = 0; ASecondParam : string);
Tim Sullivan
+4  A: 

It is basically "some compiler hack for automation objects". I sometimes have to use it for Excel and Word automation.

e.g.

  MSExcel.Application.Cells.Replace(What:='', Replacement:='', LookAt:=xlPart,
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True);

Is equivalent to VBA

Application.Cells.Replace(What='', Replacement='', LookAt=xlPart, _
   SearchOrder=xlByRows, MatchCase=False, SearchFormat=True, ReplaceFormat=True)
Gerry
+12  A: 

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.

Rob Kennedy
I'd like them: You could call a function saying "I want the exact default bahaviour except for *this* one thing." and the reader would immediately get your intention. As it is now you have to scan lots of calls to the function where they match the default params and where they differ.
Ulrich Gerhardt
I agree with Ulrich. From my time using Ruby, being able to specify named parameters is very handy, especially for functions which may have many parameters and you only care about a couple. They also increase the readability of the code, as you don't need to know the parameter calling order to understand what each parameter is for.
Tim Sullivan
A: 

This is not a Delphi feature. It is OLE's feature.

It seems you have never programmed in any of Microsoft's languages.

Yogi Yang 007