4 ms·
Most of what the author suggests is available in (modern) C++ - while maintaining backwards compatibility with C code: * Textual inclusion: C++20 has modules,
by einpoklum 7y ago
Most of what the author suggests is available in (modern) C++ - while maintaining backwards compatibility with C code:
* Textual inclusion: C++20 has modules, where you include sematically, not textually.
* Bitwise operator precedence: Not soundly supported, but you can sort-of have custom infix operators in C++, see:
https://stackoverflow.com/questions/15632217/defining-new-infix-operators https://stackoverflow.com/questions/15632217/defining-new-in...
so, specifically, one could implement the alternative modulo.
* Leading zero for octal: C++ has custom literals, so you could define a _octal or _hex etc and get the value determined at compile-time.
* No power operator: Same thing in Odin and C and C++, IIANM.
* Switch with default fallthrough: Since C++17, there's an official [[fallthrough]] anottation. You can make your compiler fail in cases when you implicitly fallthrough. So, not as elegant, but you can ensure you don't mess up and get the wrong behavior.
* (Type syntax: Nope, C++ has the contrived syntax of C.)
* Weak typing: With a library, you can have strong aliases which are not interchangeable with the aliased type. See this blog post:
https://foonathan.net/2016/10/strong-typedefs/ https://foonathan.net/2016/10/strong-typedefs/
and the library it links to. Things would be better, though, if the C++ standard committee allowed for an operator.
* Bytestrings: This is a library issue really. And both C and C++ have libraries which deal with wider characters, with UTF-8 and what-not.
* ++ and -- : You don't _have_ to use them... and it's possible to have static tools which forbid them in source files.
* ! Operator: C++ has !, && and || , but also "not", "and" and "or". I like the latter.
* Multiple returns: It's easy to return a tuple in C++, and with C++17 you can even construct and initialize multiple variables like that, e.g. `auto [index, name] = get_index_and_name(whatever)`.
* Errors: C++ is multi-paradigmatic here, supporting the traditional status return, an expected<T> type (either an actual T or an error; not yet standardized by available via widely-used ibrary), and exceptions. Monadic-style programming is not yet supported and will likely not be in C++20, but there are discussions about this.
* Nulls: With `std::optional` and `gsl::non_null`, and especially with no return types necessary, you can comfortably avoid using `nullptr` in C++ code.
So - with some choices and a little discipline (really not much!), you can have all of this "Odinish" behavior you like.
Of course - the price of C backwards compatibility and supporting multiple programming paradigms is complexity of the formal language definition, grammatic ambiguity, and a syntax that is not always pleasant.
- strictusername 7y agoRight, but C++ is evil, remember? It's better to make your own language from the ground up than using something that thousands of people have worked on over decades.
- beart 7y agoA new language can take all the lessons of a previous one without being locked into backward compatability. It doesn't have to start from zero.
- viraptor 7y agoThe optionals / non_null won't play well with external libraries though, right? As in, if I have to interface with a third party library taking plain pointers, I would have to either unwrap on every usage, or create an extra interface in between which unpacks the arguments and wraps the result - or did I miss some better solution?
- einpoklum 7y ago> won't play well with external libraries though, right? 1. Library use boundaries are indeed a place where you often need to "unpack" more complex types. But then - external library APIs do tell you whether they take ownership of pointers and generally what you can expect from them. And it's not so bad to write lib_foo_function(my_unique_ptr.release(), other, args); instead of my_function(std::move(my_unique_ptr), other, args); 2. Libraries slowly adopt features of language standards, and typically lag behind. To be honest, though - this is going slower than I would have liked or expected.