3 ms·
I wish more languages supported that natively, starting with C and C++. The most typical example, in C, output arguments, passed by pointer, a pattern that I a
by GuB-42 3y ago
I wish more languages supported that natively, starting with C and C++.
The most typical example, in C, output arguments, passed by pointer, a pattern that I also use in C++ because I find passing by non-const reference error-prone since it is not obvious looking at the calling code that an argument may be modified.
But what about NULL? Sometimes it is a good thing that NULL is allowed, sometimes, it doesn't make sense. So again, not obvious. So what to we do? You can document it of course, but the problem is that compilers don't read the docs, and therefore can't tell you if you are doing it wrong. You can play it safe, and design your API so that NULL is always an option, and avoid NULL when using others APIs, but it leads to unnecessary and performance-impacting checks. Preconditions and postconditions would solve that problem: if you pass an argument that can be NULL to a function that doesn't accept NULL, the compiler can warn you. Plus, there is optimization potential, in the same way that C/C++ does with undefined behavior, but this is explicit. You also can also get the choice between safe (runtime checks) or fast (undefined behavior) at compile time.
Making it a core feature of the language rather than an extension (like your rust crate) will help compilers and other tools to take full advantage of what it brings.
- Arnavion 3y agoMicrosoft's compiler has that ability with SAL annotations. But yes a standard way to do it would be nice. https://learn.microsoft.com/en-us/cpp/code-quality/using-sal-annotations-to-reduce-c-cpp-code-defects https://learn.microsoft.com/en-us/cpp/code-quality/using-sal...
- throw0101b 3y ago* https://en.wikipedia.org/wiki/Assert.h https://en.wikipedia.org/wiki/Assert.h ? C11 and C++11 also have compile-time static variants.
- summerlight 3y agohttps://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2900r1.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p29... C++ is preparing this for years, though it's uncertain when it will actually land in C++.
- mhh__ 3y agoD has contract programming e.g. `in(param < 0)` before { I don't think there's all that much that a typical compiler will do with them however sophisticated enough type system I can see some wins.
- nurettin 3y ago> I find passing by non-const reference error-prone since it is not obvious looking at the calling code that an argument may be modified. VS code makes it pretty obvious by adding a visual & prefix to the argument. I hope that kind of visual help becomes widely adopted.
- 8n4vidtmkvmk 3y agoI've started using abseil null/no null https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://github.com/abseil/abseil-cpp/blob/master/absl/base/nullability.h&ved=2ahUKEwjrrYbq7fGCAxWOLUQIHe4XBhMQFnoECBAQAQ&usg=AOvVaw2pa6BSJIUtJs0Mld7Zj2-I https://www.google.com/url?sa=t&source=web&rct=j&opi=8997844... We have a linter that will try to catch cases where you didn't null check but the field is nullable