source: doc/proposals/named-parameters.md @ fcf3493

Last change on this file since fcf3493 was f3811df, checked in by Andrew Beach <ajbeach@…>, 4 months ago

Added an overview of how named parameters could be added to Cforall.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1Named Parameters
2================
3An examination of the possibility of adding named parameters to Cforall.
4Named parameters allow arguments to be passed and matched to a parameter by
5their name instead of their position in the argument list.
6
7A comparison of positional and named argument passing:
8    make_position(getWidth(), getHeight());
9    make_position(.x = getWidth(), .y = getHeight());
10
11The example of a Python style printer using optional named parameters:
12    printp("Error:", errorCode, .file=serr, .endl="");
13
14Variations of this feature can be found in various languages:
15+   Python - Keyword Arguments
16+   Swift - Argument Labels
17+   Ada - Named Association
18+   C - Designators (limited)
19
20Overview of New Features
21------------------------
22In terms of code written, this feature interacts with the following:
23
24Function Applications and Arguments:
25When a function is applied and passed arguments those arguments must be
26provided either as `Positional Arguments` or `Named Arguments`.
27
28Positional arguments use the existing C syntax and named arguments could
29reuse member designator syntax (`.NAME = EXPR` in an argument list).
30
31Function Declarations and Parameters:
32When a function is declared its parameters may be defined as `Positional
33Parameters` or `Named Parameters`. Unlike with arguments, this is not an
34either or thing, parameters are actually in three groups `Positional Only`,
35`Named Only` and `Positional or Named`. In addition, all parameters can
36be `Required Parameters` or `Optional Parameters`.
37
38Current C syntax should be used for positional parameters. New syntax will
39be needed for named-only or named-or-positional parameters. Something like,
40`TYPE .NAME` (a dot at the front of the parameter name, to reflect the
41argument form).
42
43Current Cforall does have some support for optional parameters and default
44arguments. An optional parameter is declared by proving it with a default
45argument (putting `= EXPR` after the parameter declaration). There is also
46syntax for explicitly requesting the default argument is used (`@`).
47
48As an extension, we could allow array designators (`[ POSITION ] =`) as a way
49to explicitly give the position of an argument. This is not an existing
50Cforall feature, nor directly related to named parameters, but it is an
51extension of C semantics that fits in this area.
52(I would actually recommend against it at this time, parameter lists should
53not be so long that this is useful.)
54
55Function Pointers
56-----------------
57Function pointers do not need to support named parameters, in the same way
58they do not support optional parameters. (You can write an optional parameter
59in a function pointer, but it is ignored.) There could be some way to convert
60or cast between the two forms, but in practice, the types of functions where
61named parameters are useful have very little overlap with those that you
62would pass to a higher order function or use as an assertion.
63
64Argument Matching
65-----------------
66How are arguments connected to parameters. This will become part of the
67overload resolution process, luckily it is a pretty simple straight forward
68pass fail check so does not effect cost. This covers all the features being
69considered, but most can cleanly be removed.
70
71First, the positional parameters have to be sorted out.
72
73Undesignated arguments are positional arguments, if one appears at the front
74of the argument list it is the 0 positional argument, otherwise it must
75follow another positional argument and it goes into the next position. It is
76an error for an undesignated argument to appear after a named argument.
77
78Array designated arguments are positional arguments. The constant expression
79is evaluated and the result is the position of the parameter it is matched
80with.
81
82The same process is way simpler with named arguments, as all are labeled.
83Member designated arguments are named arguments. They are matched with the
84parameter with the same name.
85
86The `@` argument can be used anywhere other arguments can be. The parameter
87it is matched with must be an optional parameters and this explicitly requests
88that the default argument be used.
89
90Then we can just check to make sure no parameter is provided/matched with an
91argument more than once, and that every required parameter is provided
92exactly once. If any arguments could not be matched to a parameter, it is an
93error.
94
95Note that there are no special rules for positional-or-named parameters, they
96can just be used in either case.
97
98Backwards Compatibility
99-----------------------
100All parameters and arguments in C code can treated as (required and)
101positional, except for initializers which are optional and use designators.
102
103Initializers and C designators always consider the underlying parameter's
104position the important part. The designator moves the position in the
105parameter list forward or backward. If an argument is not designated, it is
106put the next position after the previous argument (or the first position if
107it is the first argument).
108
109It should be noted that this is actually more permissive than most languages.
110Other named parameter system enforce that all positional arguments come
111before all named arguments.
112
113However, we could translate this using optional and named-or-positional
114parameters. Removing the ability to have undesignated arguments follow
115a member designated arguments is required for named only parameters, doing
116the same for named-or-positional for a consistent interface.
117
118C also allows chained designators, nested initializers and descending into and
119out of recursive initializers automatically as the beginning or end of those
120sections. In the context of a member/element initializer, the system does
121have enough information to do this, because the target types are fixed by the
122type being initialized.
123
124These should be supported in the C escape initializer (`@=`), but cannot be
125generalized to function calls (not even initializers we resolve as functions)
126because of overloading.
127
128Run-time Implementation
129-----------------------
130The underlying code must be translated into simple C code that does not use
131these parameters or arguments.
132
133For this purpose, we do use the ordering of all parameters, writing them
134out in the order they appear in the declaration.
135Note that the programmer still does not have to (and sometimes cannot)
136interact with the order of parameters, but the compiler will still use them.
137Here it boils down all the named forms down to positional code. This is the
138run-time efficient way to implement it. Other forms of argument packing, such
139as putting the named arguments into a map, tend to be slower and their
140advantages allow for more dynamic behaviour has a harder time using
141effectively.
Note: See TracBrowser for help on using the repository browser.