New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add specification for named parameters #599
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very clear on the difference between _
and None
. Does the former mean that caller need not provide the value while the latter means something else?
If `_` is provided as the default value the compiler will initialize the | ||
optional parameter with an appropriate value. | ||
|
||
In addition, we define the value `None`as a keyword to specify a parameter for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding None
as a keyword is going to break any program that already uses None
as a name anywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that could be a problem. However, I have seen no uses of None in the entire body of p4_16 code that I have access to.
In addition, we should be able to detect the context and have the defined meaning only when used as an argument "initializer".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't think we need to have a None default value. We don't need it for anything.
It is much simpler to just an annotation of @optional
on parameters that do not need to have a value specified, as it is today. However, we can only allow such parameters for objects that are interpreted by the back-end, e.g., package or extern constructor parameters.
We can have default parameter values, but they have to have the right type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not clear to me why would you prefer an annotation over explicitly stating the optionality in the language. Just because we currently have an experimental feature implemented in the compiler? I can promise that we implement the new feature instead.
Line 1477 states that all optional parameters are applicable to objects not exposed to the control plane. If it is not coming across, please suggest different wording.
In addition, we define the value `None`as a keyword to specify a parameter for | ||
which there should be no default value. The programmer must specifically | ||
provide a value for a parameter initialized with `None`, Otherwise, for an | ||
invocation that does not specify a value, the compiler may assume that all uses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the programmer must specifically provide a value for the parameter, how can there be an invocation that does not specify a value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use None, we assume that that parameter is not used at all. With a '_' (default value) we assume a default value described in the table (empty controls and parsers, default instances of externs, or zero valued ints, headers, structs, metadata.
I wrote a very compact summary of calls with named arguments, which I hope handles all of the use cases described in this PR:
Called:
Instantiated
3.1. Current syntax:
3.2. New syntax:
4.1. Previously calls used to be of the form:
Now we will also allow
4.2. Argument names must correspond to parameter names. Arguments can 4.3. A call can have either all argument names supplied or none. 4.4 Overloading: overloaded objects are allowed if any call can be
A parameter can be annotated as optional using @optional:
If all optional parameter are at the end, then object can be called Optional parameters can only appear for the following objects:
Justification: the implementations of these objects is not done in P4.
A parameter can be annotated with a default value of the correct type:
Default parameter values can appear in all declarations that appear in section 1. If all default parameter values are at the end the object can be 6.1. Combining default values and optional parameters The same parameter cannot be both optional and have a default value. An object with both default parameters values and optional parameters
|
There are few key points:
|
Do we really need both @optional and default parameters? They seem to mostly fill the same niche. @optional really only makes sense for extern functions/methods, as we have no syntax for determining if an optional argument was supplied or not in anything that has a body. It currently has an additional related meaning for abstract methods (they can be defined in an instance with a method that does not include the optional argument), which is different from the meaning anywhere else. |
The difference between the two is that an optional parameter may not be given any value (i.e. will have the value NONE). This machinery avoids having a special value for NONE in the language. |
But indeed, it looks a bit ugly. If we had a nice version of NONE we would not need optional anymore. |
I think this can be closed as it is superseded by #626 |
Fixes #598