-
Notifications
You must be signed in to change notification settings - Fork 82
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
[proposal] Syntactic sugar for constructors #129
Comments
I would rephrase this the other way around, which has the exact same effect: it is the application of a type which introduces a new instantiation. I.e.
is the same as
The proposal has also to explain what happens when a control is invoked twice:
This could be interpreted in two ways (or it could be forbidden):
or
|
I haven't thought through your suggestion, Mihai, but regarding Cole's original proposal: This might feel a bit less magic if there were explicit syntax to specify whether a control needs to be instantiated. I'm not a fan of the terminology, but static control c1(...) { ... }
static control c2(...) { ... }
static control c3(...) { ... } I expect the vast majority of controls to be "static" in this sense, so perhaps it would be better to require some kind of more explicit syntax to indicate that the control does need to be instantiated, but it's not obvious to me what would be readable in that case, especially not without larger changes in the language. ( |
To be honest I don't really understand what static is supposed to mean. The annotation approach has multiple other benefits:
|
I appreciate the effort to eliminate syntactic confusion, but I agree with @mbudiu-vmw. I wouldn't want to use a keyword like |
I'd like to propose tabling this and revisiting for a later version of the language. There are two reasons. First, the implementation turns out to be tricky, because we need to allow type names to be used as instances but also as types. For example: control sugar(...) { ... }
control my_constructor(...)(sugar s) // "sugar" used as a type name
{ ... }
my_constructor(sugar) c; // "sugar" used as an instance Second, after working through some examples and writing a PR for this change against the spec, I find the syntax more confusing than convenient. Type names refer to types, except when We could surely make this work. However, in the short term, I would rather err on the side of a simpler language and implementation---with slightly more verbose P4 programs---rather than risk introducing bugs for a convenience feature. |
This is a useful proposal, so we can simplify it. |
I like this idea. I'll amend the PR for the spec and finish the implementation. |
Add language defining the constructor syntactic sugar in #129.
* Fix for issue #537 * Implement spec proposal p4lang/p4-spec#129 * Make it work for parsers too
Can we close this now? |
Closing after discussion with Cole. |
Problem
Names of parser/control definitions and instances must be distinct. When a definition is instantiated only once, this leads to boilerplate code in the beginning of a block:
For large programs, eg. switch.p4, there may be many controls instantiated once in a top-level control, and the list of control instantiations---with names like "do_X"---can be quite long.
This example uses controls, but the same problem arises for parsers.
Proposal
Add syntactic sugar to ease the case where a parser/control is only instantiated once. With no constructor parameters, this:
is considered equivalent to this:
where the name
c1_t
is not used elsewhere in the program.Examples
Under this proposal, there would be three ways to define parsers/controls.
The first example would now look like:
The text was updated successfully, but these errors were encountered: