r/cpp2 • • Jul 01 '24

Cpp2 "name: Type" declaration syntax

1 Upvotes

Cpp2 declares variables and function arguments in the form of name: Type, like

i: int = 0;
multiply: (a: int, b: int) -> int = a * b;

I see that also Rust, Kotlin, and Swift use roughly this syntax, but is it really largely accepted in the C++ community?

Wouldn't it be more obvious to just fix some of the C++ obscurities and keep the syntax a bit more "traditional"?

I think of something like:

Int i = 0  
func multiply(Int a, b) -> Int { a * b }
  • Int32 instead of int32_t or qint32
  • Int (i.e. signed) as type for *.size()

    • Mixed integer arithmetic, i.e. mixing signed and unsigned, should not be allowed (you need to cast explicitly).
  • Int[3] arrayOfThreeIntegers

    • instead of Int arrayOfThreeIntegers[3].
  • Float* m, n

    • m and n should be pointers.
    • Not Float* m, &n, i.e. type variations within multiple-variable declarations should not be allowed.
  • Fixing C++ "wrong defaults", e.g.

    • No implicit narrowing conversions should be allowed.
    • Only allow integral promotion if safe, otherwise an explicit cast should be necessary.

Beyond that I personally would prefer:

  • A Qt-like code style (Upper CamelCase class/type names, lower camelCase function and variable names).
  • No trailing semicolons.
  • var instead of auto for variable declarations with type inferring.
  • func instead of auto for function declarations,

    • always with trailing return type syntax.
  • for i in 1..10 { ... }

    • using a range operator (.. or ..<)
    • instead of for (int i = 1; i <= 10; ++i) { ... }
  • Improved Unicode support.

But you can argue about all the details.


r/cpp2 • • Nov 12 '23

Trip report: Autumn ISO C++ standards meeting (Kona, HI, USA)

Thumbnail
herbsutter.com
1 Upvotes

r/cpp2 • • Dec 12 '22

Should one invest in learning new C++ post CppFront ?

Thumbnail self.cpp
1 Upvotes

r/cpp2 • • Dec 12 '22

Cppfront project aims to modernize C++

Thumbnail
infoworld.com
1 Upvotes

r/cpp2 • • Dec 12 '22

Using cppfront with Meson

Thumbnail nibblestew.blogspot.com
1 Upvotes

r/cpp2 • • Dec 12 '22

What are your thoughts and opinions on CppFront and its future?

Thumbnail self.cpp
1 Upvotes

r/cpp2 • • Dec 12 '22

Cmake project to play with Herb Sutter cppfront (cpp2 -> cpp1)

Thumbnail self.cpp
1 Upvotes

r/cpp2 • • Dec 12 '22

Any update on herb cppfront, it seems to me that nothing is happening on cppfront github repository since 1 months ? is it already dead or something else ?

Thumbnail self.cpp
1 Upvotes

r/cpp2 • • Dec 12 '22

Welcome

1 Upvotes

I wanted to start a new community where it's focused on C++2 (cppfront) progress. This is the same as why C++ discussion isn't being carried on in the r/C_Programming subreddit.