7 ms·
Looking forward to finding out what kind of feedback and discussion this generates. Some of the changes seem to make the syntax pointlessly different. /*
by metadat 4y ago
Looking forward to finding out what kind of feedback and discussion this generates. Some of the changes seem to make the syntax pointlessly different.
/* Proposed "new" c++ syntax */
main: () -> int = {
cout << "hello world\n";
}
// vs.
/* Classix c++ syntax */
int main() {
cout << "hello world\n";
}
The "new" way looks a lot like a copy of the newer Java syntax for closures.
- nickhalfasleep 4y agoThe idea is to have a consistent way across many items, functions, lambdas, etc…
- humanrebar 4y agoSome of the syntax changes are to support simple LR parsing. Ideally cpp2 would have boring grammar descriptions available for popular off-the-shelf parser generators. No need to link libclang into everything.
- hackingthenews 4y agoThis is explained in the talk he gave. One of the benefits is that with new syntax the semantics can change without breaking backward compatibility, e.g. [[nodiscard]] can become default for all functions written in the new style.
- williamvds 4y agoFrom a C/C++ programmer's POV those may look like GOTO labels, I agree. The proposed syntax is probably a consequence of a modern trend in using := for declaration and assignment. x := 5 Where you can optionally insert the type between the colon and equals, otherwise it is deduced x : int = 5
- teo_zero 4y agoIn English, the colon is used to separate a term to its definition, so it looks perfectly apt to mark declarations. Besides, it makes it trivial for text editors to implement "go to definition", and many other niceties without a real semantic parser.
- thechao 4y agoI used to code for a language called "Spad" — originally shipped in 1972(?). The syntax: x : t = e Was called "headform" and was the only syntactic form for the whole language. (Expressions used a shunting yard algorithm to desugar into a sequence of headform.) The language included modules, classes, parameterics, functions, constraints, ..., whatever.
- naruhodo 4y agoWell, everything looks like a pile of dirt when the only tool you have is a Spad. ducks
- dvratil 4y agoI don't think this has anything to do with modern trends - := is a formal way of writing assignment (the widely used = is actually an equality sign), and has already been present in Pascal for decades (and other languages as well).
- Jtsummers 4y agohttps://en.wikipedia.org/wiki/ALGOL_58 https://en.wikipedia.org/wiki/ALGOL_58 ALGOL, the := in programming languages used for assignment comes from IAL which became ALGOL 58. It's definitely not a modern development. Go reintroduced it to popular mainstream languages, sort of. Except they still use = for assignment and := is used when it's also declaring a new variable.
- int_19h 4y ago:= for assignment ultimately comes from math, as do most operators in early Algol: https://www.masswerk.at/algol60/report.htm#2_3 https://www.masswerk.at/algol60/report.htm#2_3
- rafram 4y agoI’m not sure I’d := is a “modern trend.” Smalltalk had it circa 1972, but none of the modern-day hip languages I can think of do. Your latter example is a common syntax, but the colon is omitted with the type in most languages (TypeScript, Rust, etc.).
- charcircuit 4y agoX being a modern trend does not mean it was not done in the past. It doesn't mean that it hasn't trended in the past either.
- the_af 4y agoSo what does it mean then? It was done in the very distant past. It was done in the intervening time. And it's done now. "Modern" trend?
- SaddledBounding 4y ago> The proposed syntax is probably a consequence of a modern trend... ML uses `name: type = value`. It came out over a decade before C++ and just one year after C.
- cvoss 4y agoIt's not modern at all. Type system notation has been using x : T to say "x has type T" for at least 50 years. It's the standard in academia, and has existed in real-world programming languages since 1971. [0] [0] https://cstheory.stackexchange.com/questions/43971/why-colon-to-denote-that-a-value-belongs-to-a-type https://cstheory.stackexchange.com/questions/43971/why-colon...
- forrestthewoods 4y agoIt is an old idea and also a modern trend. Trends come and go. It is currently trendy. 90s fashion is also trendy right now. Which is great because a few years ago it was 80. The cycle continues!
- the_af 4y ago"Currently trendy" how? Scala and Kotlin, to name just a few established programming languages, use the ": type" suffix style. They are neither "recent" nor from the original Algol era.
- int_19h 4y agoIt depends on how you define "current", I suppose, but personally I'd consider Kotlin to be fairly recent.
- the_af 4y agoWell, Kotlin is from 2011 and Scala from 2004! 11 and 18 years ago, respectively. I'd say neither is recent by any reasonable sense of the word that would imply their syntax or conventions are "a current trend".
- int_19h 4y ago11 years is not all that much for a PL, especially if you count from the date it was first announced, rather than from the first stable release (which for Kotlin was in 2016). For example, Rust started in 2006, and Rust 1.0 was in 2014, so it's actually older the Kotlin - but I think it still qualifies as "new", and many still consider it not fully battle-tested. For another example, Python goes all the way back to 1991, with v1.0 in 1994. But it didn't really start to trend until early 00s.
- noobermin 4y agoAll the replies are missing something, does cpp2 have goto labels? What happened to them. Unless you now have named loops in modern C++ now this removing goto is problematic.
- onedognight 4y agoIt’s actually close to the new C++ syntax for functions. Take a look at the output C++ in the regression-tests/test-results folder: auto main() -> int { cout << "hello world\n"; } The difference between the two is mostly the : and the =. The colon is for declarations being written: i : int = 0; Instead of int i = 0; main is the variable/function above and its type is ()->int and it’s being assigned, i.e. defined, hence the =. This syntax removes a ton of parsing complexity and makes the language more regular. Functions are defined and assigned similarly to variables.
- dataflow 4y agoThat colon is going to be quite burdensome to type, due to the need to hold the Shift key on a line where you otherwise might not need to. It might sound silly but I do think little things like this can really hinder adoption without people necessarily realizing it.
- coliveira 4y agoTell this to people using Python. Every function, if, and loop has a colon at the end.
- dataflow 4y agoYou misread my comment. I'm not saying "colons are bad", I'm saying "excessive holding of keys is bad". if (cond) { } requires 4 presses of Shift (or 2, if you combine them). if cond: foo requires 1 press of Shift. I'm not sure what part of my comment makes you believe that I think the latter comes out worse than the former. If anything, the fewer keypresses probably strongly helped Python's success without people necessarily realizing it. Moreover, this might very well be one thing hindering more widespread adoption of Python's type annotations. You also need to consider how common each construct is. A class declaration requiring 20 excessive keystrokes is not necessarily worse than a variable declaration requiring 1 excessive keystroke.
- 4y ago
- hardlianotion 4y agoIn that particular case, it looks like he declared and assigned a thunk called main. It looks different to me than the original-style main.
- zozbot234 4y ago> main: () -> int = { Y U NO fn??!? So close, and yet so obviously wrong.
- Koshkin 4y agofn is no fun
- duped 4y agoWhy add a keyword when you don't need it?
- bogdanoff_2 4y agoFor easier grepping
- deleted 4y ago[deleted]
- teo_zero 4y agoOne answer would be: So that when you hit the ( you know it's a list of parameters and not an expression, even before you spot the -> I still haven't read enough about syntax 2 to understand if it allows any other use of ( after a : but if yes, a keyword like fn or fun would make the parser much more regular.
- criddell 4y agoBoth versions are supposed to return an int but neither has a return statement. Is that not an error?
- hota_mazi 4y agoIt's disappointing they would keep that odd << syntax, which has always looked weird to me. Why not take this opportunity to streamline the calling syntax? Streams are no different than regular function calls, e.g. cout.output("hello").output("world\n")
- layer8 4y agoYou are talking about library API design, not language design. A new C++ syntax will not change how functions in the standard library are named (e.g. operator<<).
- CyberDildonics 4y agoThat is an operator overload of a library and not built in language syntax. Even so there is now std::format https://en.cppreference.com/w/cpp/utility/format/format https://en.cppreference.com/w/cpp/utility/format/format
- hota_mazi 4y agoDoh, you're absolutely correct.
- Sharlin 4y agoThe "type first" declaration syntax is a source of much wailing and gnashing of teeth when it comes to trying to parse C++. See the "most vexing parse" [1]. [1] https://en.wikipedia.org/wiki/Most_vexing_parse https://en.wikipedia.org/wiki/Most_vexing_parse
- tsimionescu 4y agoThe most vexxing parse was a huge problem in C++ and a very niche problem in C not because of "type first" declaration syntax, but because C++ constructor calls in variable initialization are essentially identical to function declarations. This is a big reason why Java or C#, also type-first syntax languages, have no such ambiguity. The two examples on Wikipedia are both caused by this, even though it's not explicitly spelled out: int i(int(my_dbl)); // two calls to int's constructor // in Java/C# style syntax: int i = new int(new int(my_dbl)); //no ambiguity TimeKeeper time_keeper(Timer()); // in Java/C# style syntax: TimeKeeper time_keeper = new TimeKeeper(new Timer()); C++ could have also chosen to reserve a keyword for initialization even for in-place values, just like they did with `new`, but they chose to re-use function call syntax instead. I would also say that just moving the type after the variable name doesn't fully solve the issue - it's the : syntax for initialization that does this. Here is a hypothetical C++ where the syntax remained the same as today, but (return) type was moved at the end of a declaration: typedef double my_dbl; typedef int i; foo (my_dbl double ) void { i int (int(my_dbl)); //unambigous: i is an int initialized with an int constructed from my_dbl f (double) int; //unambigous: f is a function from double to int g int (my_dbl); //unambigous: g is an int constructed from my_dbl bar((my_dbl)i); //ambigous: calling bar with i cast to double, or calling it with a function from my_dbl to i (double to int)? }
- DerekL 4y agoSimilar problems happen even in C. Suppose you have this line inside a function: X *y; Are you declaring a pointer, or are you performing multiplication? It depends on what “X” is.
- 4y ago
- oblio 4y agoThe "new" way is basically updated Pascal, which was the right way to do it, from back in the 80's. Similar story with C strings vs Pascal strings, another can of worms.
- snvsn 4y agoI had the same reaction initially but then realized that this makes the variable declaration / initialization syntax very consistent. variable: Type [= initial_value]; // initializes foo to an int with an initial value of 43 foo: int = 43; // initializes main to a (presumably, as I haven't read the // spec yet, const)pointer to a function that takes no // arguments and returns an int. main: () -> int = { ... }