News for hackers. Stuff that matters.
Re:Is Objective-C BOOL a boolean type? It depends by pavlov on Wednesday January 10, 2024 @02:26AM (#38937252)
IMHO there wasn’t any reason to bother fiddling with the definition of BOOL.

It was well-understood that Obj-C BOOL and C++ bool are different types. Anybody working in Obj-C++ will deal with a ton of these differences, some minor (like BOOL) and some major (e.g. how NSString and std::string are not at all the same thing). Unifying a few bits here and there doesn’t deliver a lot of practical benefit.

Re:Is Objective-C BOOL a boolean type? It depends by jviotti on Wednesday January 10, 2024 @09:05AM (#38939160)
Author here! We write Objective-C++ to connect existing Objective-C code (that uses BOOL) to C++ (where we want to just expose `bool`), which got us into this fun rabbit hole :)
Re:Is Objective-C BOOL a boolean type? It depends by ryandrake on Wednesday January 10, 2024 @11:14AM (#38940182)
When I did Objective-C applications with C++ business logic, I recall our company's rule was to keep Objective-C code totally separate from the C++ code, in separate files, different headers, even different static libraries if possible--really quarantine them from each other. Then have a single, standalone, hideous Objective-C++ file that served as the smallest possible interface between the two. It's the only way we could keep sane. This was a trauma response from a previous project where no such discipline was maintained, and people just causally intermixed C++, Objective-C, C, and Objective-C++ willy-nilly all over the code, with now predictable results.
Re:Is Objective-C BOOL a boolean type? It depends by chunkyguy on Wednesday January 10, 2024 @03:03PM (#38942429)
This is the best way for having a objc and c++ in same project.
Re:Is Objective-C BOOL a boolean type? It depends by plorkyeran on Wednesday January 10, 2024 @03:17PM (#38942549)
I work on a decade-old codebase where we freely mix C++, Obj-C++ and Obj-C as it makes sense and I can't say it's ever been an issue. Obj-C++ has some oddities, but on the whole mixing obj-c and c++ features works totally fine and I've never encountered a reason to want to separate them.
Re:Is Objective-C BOOL a boolean type? It depends by rogueresearch on Wednesday January 10, 2024 @02:33PM (#38942151)
jviotti, the reason BOOL was not changed to bool sooner was because it would break ABI compatibility. So on Mac for example, they had to wait until the x86_64 to arm64 to finally do it.
Re:Is Objective-C BOOL a boolean type? It depends by dottrap on Wednesday January 10, 2024 @05:13PM (#38944113)
> IMHO there wasn’t any reason to bother fiddling with the definition of BOOL.

There were good reasons to change this to a real boolean. I think it was a WWDC, where kind of discussing this topic, one Apple engineer said they did an audit of production Obj-C code and found multiple instances where their BOOL values were values other than 0 or 1 (which was considered surprising and ultimately bugs).

This can lead to all sorts of subtle problems. One common one encountered by framework users is where something returns a BOOL and the user explicitly compared against == YES or == NO. If the value is say 2, then this code usually results in wrong decision.

Re:Is Objective-C BOOL a boolean type? It depends by mulle_nat on Wednesday January 10, 2024 @05:15PM (#38944131)
Agreed. BOOL in Apple ObjC is a `char` though. That is a constant thorn in the side, when you are doing slightly more sophisticated stuff with argument passing and return values as you actually have to cast a lot (BOOL (*)(id, SEL, ...)).

As the implementer of an Objective-C dialect, I am considering to go the other route and elevate BOOL to NSInteger (sizeof( NSInteger) == sizeof( id) in my case).

Conceivably the compiler could (just) for @properties combine all the BOOL fields into a bitfield, so there would be less grousing about wasted space. Maybe or maybe not.

* https://developer.apple.com/documentation/objectivec/bool?la...

* https://github.com/iterate-ch/rococoa/blob/master/ObjcMsgSen...