Date: Mon, 10 Feb 2025 16:42:11 +0000
Pau Miquel Montequi Hernandez wrote:
> I would like to discuss a revision of the for loop syntax, with the goal of
> evaluating possible improvements that could enhance its usability.
> - for (int i = 0 ... 42).
> - for (int i = 0 -> 42).
> - for (int i = 0 => 42).
> - for (int i = 0 <- 42).
> - for (int i = 0 <= 42).
You might like to look at what Swift has done. Specifically they have:
for i in 0...42 {
}
That includes both 0 and 42. For a half-open range the syntax is 0..<42.
I don't think there is a way to indicate a reverse range but I could be wrong.
See e.g.
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators#Range-Operators
No doubt other languages have similar things.
They also have "trailing closures", which make foreach-type functions that
take a body callback a bit easier to use. Rather than:
my_foreach(args, [&] (auto arg) {
....
});
you would have:
my_foreach(args) [&] (auto arg) {
}
i.e. the lambda after the function call's closing paren is treated as
an additional
argument to the function, just as if it were inside the closing paren. This
removes the nasty-looking mixture of } and }); that we can currently
have in
code with nested control flow.
I've been writing a bit of Swift lately and there are a few things that
C++ could
usefully copy, IMHO. I don't think I'd advocate for the 1...5 range
syntax, but I
would support trailing closures, perhaps combined with a concise lambda syntax.
Regards, Phil.
> I would like to discuss a revision of the for loop syntax, with the goal of
> evaluating possible improvements that could enhance its usability.
> - for (int i = 0 ... 42).
> - for (int i = 0 -> 42).
> - for (int i = 0 => 42).
> - for (int i = 0 <- 42).
> - for (int i = 0 <= 42).
You might like to look at what Swift has done. Specifically they have:
for i in 0...42 {
}
That includes both 0 and 42. For a half-open range the syntax is 0..<42.
I don't think there is a way to indicate a reverse range but I could be wrong.
See e.g.
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/basicoperators#Range-Operators
No doubt other languages have similar things.
They also have "trailing closures", which make foreach-type functions that
take a body callback a bit easier to use. Rather than:
my_foreach(args, [&] (auto arg) {
....
});
you would have:
my_foreach(args) [&] (auto arg) {
}
i.e. the lambda after the function call's closing paren is treated as
an additional
argument to the function, just as if it were inside the closing paren. This
removes the nasty-looking mixture of } and }); that we can currently
have in
code with nested control flow.
I've been writing a bit of Swift lately and there are a few things that
C++ could
usefully copy, IMHO. I don't think I'd advocate for the 1...5 range
syntax, but I
would support trailing closures, perhaps combined with a concise lambda syntax.
Regards, Phil.
Received on 2025-02-10 16:42:13