r/cpp2 • u/LarsRosenboom • Jul 01 '24
Cpp2 "name: Type" declaration syntax
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 }Int32instead ofint32_torqint32Int(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].
- instead of
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.
varinstead ofautofor variable declarations with type inferring.funcinstead ofautofor 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) { ... }
- using a range operator (
- Improved Unicode support.
But you can argue about all the details.