Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2013-04

Does FreePascal support named parameters?

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

Does FreePascal support named parameters?

Are they in the pipeline?

I am not interested in the whole nine yards in case it will be difficult to implement. I am thinking of something that will make it easier to read, but not change the code generation, ie the order of the parameters should remain the same, so that the compiler can generate the same code.

consider a function such as.

Func(aNumber: integer; aString: string);
I want to be able to write Func(aNumber := 4, aString := 'anystring');
If I type Func(aString := 'anystring',4) the compiler should generate
an error as the order of parameters has been changed.

Coming to think of it will be just as easy to wrap the parameter names in comments i.e Func({aNumber}4, {aString} 'anystring'), much simpler and almost is good. Is such a style acceptable? I don't mind using it in my own code.

Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Anthony Walter
to Frank Church

Name parameters are supported through variants.

procedure Test(V: Variant);
begin
  V.SomeMethod(12, ParamOne := '', ParamTwo := 12.5);
end;

Internally what happens is V is checked to for IDispatch, next GetIDsOfNames is called to find the dispid of SomeMethod, then a variant array of arguments is constructed (with the names), and finally Invoke is called.

This comes from the Microsoft COM concept of "automation", though automation encompasses a bit more than IDispatch, including marshalling, remoting, and error handling; passing errors back from a remote machine for example.

People don't really use this stuff much anyone, nor is their much demand for people with knowledge of the inner workings of COM. But if you want, you can write your own objects which implement IDispatch.

See the source code in the download link on this page: http://www.codebot.org/delphi/?doc=9573

ScripTools.pas class TScriptlet which exposes arbitrary pascal methods and properties to a Javascript engine.

Reply by email

Sven Barth
to Frank Church: Does FreePascal support named parameters? Are they in the pipeline? I am not interested…
"Frank Church" wrote
Does FreePascal support named parameters? Are they in the pipeline?

Yes and no. As Anthony wrote such a construct is available for Variants and thus is supported by the parser. Normally this is disabled though. In case of the parser you should look out for "named_args_allowed" (or similar). What I don't know though is how the names parameters are handled after that...

Show quoted text 6 lines
Func(aNumber: integer; aString: string); I want to be able to write Func(aNumber := 4, aString := 'anystring'); If I type Func(aString := 'anystring',4) the compiler should generate an error as the order of parameters has been changed.

As said: this syntax is already supported (though I don't know to what extent your example would work).

Coming to think of it will be just as easy to wrap the parameter names in comments i.e Func({aNumber}4, {aString} 'anystring'), much simpler and almost is good. Is such a style acceptable? I don't mind using it in my own code.

And here we can see that you haven't worked much with the internals of the compiler yet (Note: no offence intended). Using comments for anything besides comments (and compiler directives) is extremely hard, because they are entirely handled by the scanner. The parser will not know that a comment existed there.

Regards,
Sven

Reply by email

Mark Morgan Lloyd
to Frank Church: Does FreePascal support named parameters? Are they in the pipeline? I am not interested…
Frank Church wrote 17 lines
Does FreePascal support named parameters? Are they in the pipeline? I am not interested in the whole nine yards in case it will be difficult to implement. I am thinking of something that will make it easier to read, but not change the code generation, ie the order of the parameters should remain the same, so that the compiler can generate the same code. consider a function such as. Func(aNumber: integer; aString: string); I want to be able to write Func(aNumber := 4, aString := 'anystring'); If I type Func(aString := 'anystring',4) the compiler should generate an error as the order of parameters has been changed.

The obvious thing would be some variant of the snippet below, but it doesn't work (at least on the version of the compiler I tested).

program Test;
Type
   Point = record
     X,Y : Real
     end;
Var
   Origin : Point = (X:0.0; Y:0.0);
procedure draw(p: point);
begin
end;
begin
   draw(origin);
   draw((X:0.0; Y:0.0))
end.
Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Frank Church
to Anthony Walter: Name parameters are supported through variants. procedure Test(V: Variant); begin…
Anthony Walter wrote 33 lines
Name parameters are supported through variants. procedure Test(V: Variant); begin V.SomeMethod(12, ParamOne := '', ParamTwo := 12.5); end; Internally what happens is V is checked to for IDispatch, next GetIDsOfNames is called to find the dispid of SomeMethod, then a variant array of arguments is constructed (with the names), and finally Invoke is called. This comes from the Microsoft COM concept of "automation", though automation encompasses a bit more than IDispatch, including marshalling, remoting, and error handling; passing errors back from a remote machine for example. People don't really use this stuff much anyone, nor is their much demand for people with knowledge of the inner workings of COM. But if you want, you can write your own objects which implement IDispatch. See the source code in the download link on this page: http://www.codebot.org/delphi/?doc=9573 ScripTools.pas class TScriptlet which exposes arbitrary pascal methods and properties to a Javascript engine. _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Does this work on Linux as well, or is it only for Windows?

Frank Church ======================= http://devblog.brahmancreations.com

Reply by email

Anthony Walter
to Frank Church

Frank,

The Javascript in Delphi code depends on active script engine. and component of Windows. So no, the example I provided does not run on Linux.

But, and dynamic methods and properties (as well as named optional arguments) are supported in Free Pascal, as long as you use a Variant (or OleVariant) type which references an IDispatch instance. You can your your own implementation of IDispatch on any platform, and the example I provided does that. See TScriptlet which converts a TComponent derived instance into an IDispatch and is thus compatible with dynamic and properties methods when addressed via a Variant.

Example: MyVariant := TYourDispatchImplemention.Create as IDispatch; { and later } MyVariant.SomeMethod(12.5, 'ParamOne' := EditControl.Text); { Calls TYourDispatchImplemention GetIDsOfNames() passing 'SomeMethod', then Invoke() with the dispid from the prior method and arguments packed into a variant array }

Reply by email