Is Objective-C BOOL a boolean type? It depends
jviotti.com
jviotti.com
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.
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...
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.
BOOL was one of those things that would have been really nice to be a real bool because when bridging to other languages that had a real boolean type, the idioms could be completely natural when writing in the bridged language. But since BOOL was a 'signed char', using only the Obj-C runtime introspection, this by default would typically bridge to something weird like an integer.
In Mac OS X 10.5, Apple officially supported PyObjC and RubyCocoa, and introduced "BridgeSupport" which provided additional metadata and dylibs containing symbols for inline functions, so any language could create a full bridge to Cocoa. The metadata could be used to interpret if a 'signed char' could actually be used as a boolean or not. But BridgeSupport was not available for 3rd party APIs (unless the authors decided to support it, which was almost never).
There were bug requests filed for Apple to properly redefine BOOL to a real boolean for the 64-bit Mac ABI, before Apple had finalized it, but Apple didn't fix this in time. My memory is hazy, but I think when the iPhone ABI came around, they didn't have time to address this. So it would be another full arch ABI before there would be a chance to address this again.
-- 2022-12-30: On macOS 12/x86_64 methods returning BOOL return integers
-- from objc.msgsend as the type encoding uses the 'c' code for char rather
-- than 'B' for _Bool (C) or bool (C++). But on macOS 11/arm64 the type
-- encoding is 'B' and we get a boolean return type.
local function itob(v, err)
if isinteger(v) then
return v ~= 0
elseif isboolean(v) then
return v
else
return error(err or sformat("expected boolean or integer, got %s", type(v)), 3)
end
endhttps://stackoverflow.com/questions/3016846/is-there-any-dif...
I wouldn't wish Obj-C as a first language on any soul. Swift on the other hand? Jokes aside, that's a beautiful language.
It was better before ObjC 2.0; dot syntax makes it hard to tell out of context what a line of code is doing, which was never a problem with the original language.
It was also better before UIKit; The killer app is Interface Builder, which is unbelievably painful to use with UIKit compared to how it used to be with pre-CoreAnimation, pre-autolayout AppKit.
Swift on the other hand? Full of chaos and ugly (func, let vs var - really).
We need a Powers of Ten with programming languages.