4 ms·
> The fact that pointers automatically coerce into bools makes some sense in C but is an aberration for modern C++. It's just an example of a legacy "feature" p
by smitherfield 9y ago
> The fact that pointers automatically coerce into bools makes some sense in C but is an aberration for modern C++. It's just an example of a legacy "feature" pointing its ugly head to sabotage a modern C++ construct.
This is a rather odd complaint; I can't think of any programming language which has first-class support for nullable types, and where they aren't truthy/falsy, at least in conditional contexts. It's a pretty straightforward boilerplate-reducing idiom.
Did you mean to say builtin arrays coercing to pointers? (I'd agree that's probably the biggest problem with C and, by extension, C++).
Or did you mean `NULL` being an `intptr_t` in C++? That's the very rare C++ misfeature that doesn't come from C (where it's a `void*`), but at least C++11 `nullptr` fixes that.
- kibwen 9y agoJava is a language where nullable types aren't truthy/falsy. The following code won't compile: String foo = null; if (foo) { // error: incompatible types: String cannot be converted to boolean System.out.println("was true"); } else { System.out.println("was false"); } If one changes `if (foo)` to `if (foo != null)`, then the code will compile.
- smitherfield 9y agoRight. I haven't written any Java in a while so it slipped my mind.
- pjmlp 9y agoSame applies to any language derived from Algol linage.
- Y_Y 9y agoHaskell has this with `Maybe`, although you could write the Bool instance yourself pretty easily if you liked.