-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Macros and user-defined control flows #3507
Comments
This example illustrates a few potential concerns with this proposal:
Pulling all those observations together, here's my pitch: I think this proposal is really about lambdas. For example, in C++ we can already write your example like this: void Test(std::function<bool()> condition, std::function<i32()> step, std::function<void()> statement) {
while (condition()) {
statement();
step();
}
}
i32 i = 0;
Test([&]{return i < 3;}, [&]{return ++i;}, [&]{
Print("Ding!");
}); This version addresses the concerns I raised above:
However, it has some problems of its own:
The good news is that those problems seem solvable. In fact, Carbon has solved the first one already:
For example, if we use Rust's lambda syntax and Swift's approach to trailing closures, your example could be:
|
This issue is really proposing a full new feature. If a new feature makes sense, it should be proposed with Carbon's proposal process. We also agree with @geoffromer 's observations, so it would seem good if and when exploring this to consider things like a lambda-based approach. As to whether it makes sense to work on this type of metaprogramming (macro-like constructs that expand to control flow), the leads don't think expanding our metaprogramming facilities matches the current priorities of the Carbon project. If and when its a better fit for the priorities, we'd definitely be interested in proposals to advance this area, but right now we're focused on getting the critical path to 0.1 ironed out. |
Summary of issue:
Carbon can provide a way for programmers to declare macros or user-defined control flows. They help programmers to write implementations easy and fast.
Details:
First of all, let's introduce
:?
declaration syntax:We can declare expression variables with
:?
notation. They can be either compile-time or run-time, it depends on the context. In the example,condition
is compile-time. The compiler will replace everycondition
witha < b
inif
.But
:?
will be at run-time if it's a function parameter. The compiler will create a mutable lambda for the expression, and the mutable lambda expression will be passed to the function.In the example, the compiler will declare mutable lambdas for
i < 3
and++i
.Now, considering forced-inlined functions, we may declare them with
fn!
keyword. The compiler will not create mutable lambdas for expression arguments in this case. The compiler will replace the parameters in the function with passed expression variable arguments at compile-time:It's like the previous example except that it's at compile-time. That's because
Test
function is declared withfn!
keyword as a forced-inlined function. The compiler will generate the following code for the above example:With this feature, we can declare user-defined control flows too. Let's assume that we can apply
fn!
functions to block statements if they are declared with deducablestatement
parameter. In the following example, I'm using the syntax of proposed variadics #2240 in Carbon:The point is, the programmer can write its own statements instead of
Print("Ding!");
and callTest
function on it at compile-time.Here,
statement
parameter will be deduced from block statement{...}
.each statement
is each statement that are written inside{...}
. Maybe we can find better names forstatement
andStatement
. The compiler will generate the following example from the above example:Any other information that you want to share?
The rules and syntax that I've explained here are not a major part of my suggestion. If you are interested in adding such feature to Carbon programming language, I'll prepare a proposal for it. And I'll try to improve/enhance it as much as possible. Thanks.
The text was updated successfully, but these errors were encountered: