named parameter

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: named parameter

Free Pascal - General mailing list

Am 29.05.2017 07:13 schrieb "Ryan Joseph" <[hidden email]>:
>
>
> > On May 28, 2017, at 5:22 PM, Mark Morgan Lloyd <[hidden email]> wrote:
> >
> >> IMO though it does improve readability in long functions with lots of
> >> parameters, like windows api style procedures that have 5 or more
> >> parameters and you can't figure out which param is
> >> which
> >
>
> You mean like this?
>
> function Foo (theString: string; options: set of TFoo): boolean;
> begin
> end;
>
> Foo(theString: 'foo', options: []);
>
> Objective-C has this mandatory and it made for stupidly long method names that just looked terrible but it was optional Pascal makes it look nicer. The declaration syntax is already the same as the label for the calling syntax so that seems like a nice fit.

If anything like this would be implemented, then the already existing syntax (for variant dispatch calls) would be used which uses ":=".

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: named parameter

Ryan Joseph

> On May 29, 2017, at 1:01 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
>
> If anything like this would be implemented, then the already existing syntax (for variant dispatch calls) would be used which uses ":=".
>
>

What are variant dispatch calls? Maybe something from Delphi I’m not familiar with.

That’s too bad, a single colon matches the declaration better and looks nice.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: named parameter

Mark Morgan Lloyd-5
In reply to this post by Ryan Joseph
On 29/05/17 05:15, Ryan Joseph wrote:
>> On May 28, 2017, at 5:22 PM, Mark Morgan Lloyd <[hidden email]> wrote:> >> IMO though it does improve readability in long functions with lots of>> parameters, like windows api style procedures that have 5 or more>> parameters and you can't figure out which param is>> which>
> You mean like this?
> function Foo (theString: string; options: set of TFoo): boolean;beginend;
> Foo(theString: 'foo', options: []);
> Objective-C has this mandatory and it made for stupidly long method names that just looked terrible but it was optional Pascal makes it look nicer. The declaration syntax is already the same as the label for the calling syntax so that seems like a nice fit.

Except that there's no provision for setting a parameter passed as a set
element to true if it's not given explicitly. Sometimes you want that,
and simply inverting the meaning of a parameter results in values which
appear contrived in the context of the "real world" problem being solved.

The example I gave was from a terminal emulator, where "echo",
"loopback" and so on are well-accepted concepts in the industry. And
having a parameter "any_character_set_as_long_as_its_not_apl" is
downright silly :-)

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: named parameter

Free Pascal - General mailing list
In reply to this post by Ryan Joseph

Am 29.05.2017 10:26 schrieb "Ryan Joseph" <[hidden email]>:
>
>
> > On May 29, 2017, at 1:01 PM, Sven Barth via fpc-pascal <[hidden email]> wrote:
> >
> > If anything like this would be implemented, then the already existing syntax (for variant dispatch calls) would be used which uses ":=".
> >
> >
>
> What are variant dispatch calls? Maybe something from Delphi I’m not familiar with.

It's usually used in the context of OLE automation though one could also use this with independently of OLE/COM.
For an example see here:
https://www.freepascal.org/~michael/articles/word/word.pdf

> That’s too bad, a single colon matches the declaration better and looks nice.

An assignment is the logical choice cause you're assigning a value to a parameter. A colon is used for a declaration, which is a completely different kind of "operation".

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Bls: named parameter

Free Pascal - General mailing list
In reply to this post by Free Pascal - General mailing list
Hi, Sven… 

This is what I'm thinking… 

// function declaration function f(i: integer = 0; s: string = ''; d: double); begin // fill code here end; // method calling f(s := 'text', 0.1); // supply the s and d param f(0.2); // only supply the required d param // supply all the params without proper order is fine, // as long as the parameter name is given f(d := 0.3, i := 3, s := 'text')
// supply param with similar variable name is fine, // because the compiler knows what each means s := 'another text'; d := 0.4; m(s := s, d := d);
 

Well, it's all inspired from Swift, but I think it suits Pascal as well. 

As you said the code is already there in FPC compiler, is it possible to support such syntax in next release? At least some of them. Can we have this named parameter feature in a particular switch mode, say {$MODESWITCH NAMEDPARAMETERS} or something, so it doesn't bother anyone who don't want this feature.

Thank you.

Regards,

–Mr Bee



Pada Sabtu, 27 Mei 2017 21:18, Sven Barth via fpc-pascal <[hidden email]> menulis:


2017-05-27 16:12 GMT+02:00 Michael Van Canneyt <[hidden email]>:

>
>
> On Sat, 27 May 2017, Sven Barth via fpc-pascal wrote:
>
>> 2017-05-27 9:54 GMT+02:00 Michael Van Canneyt <[hidden email]>:
>>>
>>>
>>>
>>> On Sat, 27 May 2017, Mr Bee via fpc-pascal wrote:
>>>
>>>> Hi,
>>>>
>>>> As Pascal mostly well known as a safe, easy to read, and elegant
>>>> language,
>>>> don't you think Pascal needs named parameter? I mean for ALL kind of
>>>> parameters, not just for Variants. When you have a function with many
>>>> parameters having default values, you know that named parameter is
>>>> desirable. For example:
>>>>
>>>> function f(p1: string = ''; p2: integer = 0; p3: boolean = false);
>>>>
>>>> But you only need to supply the third parameter, you still must supply
>>>> the
>>>> first and second ones with appropriate default values, like this:
>>>>
>>>> f('', 0, true);
>>>>
>>>> while with named parameter, you can do this:
>>>>
>>>> f(p3 := true);
>>>>
>>>> I believe it would raise Pascal's code readability. I know it has been
>>>> discussed before. I know somehow the parser had been able to read such
>>>> syntax. So, why don't we have the option to enable it for people who
>>>> want
>>>> it? Kinda a syntax switch mode.
>>>>
>>>> What do you think? :)
>>>
>>>
>>>
>>>
>>> Opinions on what constitutes readable code clearly differ :)
>>>
>>> But as far as I know, the parser is not able to read this syntax ?
>>
>>
>> The parser supports it for dispatch calls on variants (both methods
>> and properties). You even wrote that in your own article about Word
>> automation: https://www.freepascal.org/~michael/articles/word/word.pdf
>> ;)
>
>
> Yes, in Delphi. But I didn't know FPC also supports it ?

Yes, it does. I didn't find a testcase for it right away, but the
compiler definitely contains code for this.

Regards,

Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12