Hacker News new | ask | show | jobs
by dottrap 897 days ago
As a historical note, this was also a thorny issue for language bridges to Objective-C, going beyond just Obj-C++. I'm thinking of bridges like PyObjC, RubyCocoa, CamelBones, and LuaCocoa.

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.

1 comments

I didn't comprehensively investigate the issue to learn whether this reflected a more systematic change in reported types, but FWIW a code comment from some Lua bridging code:

  -- 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
  end