6 ms·
Understanding Objective-C by transpiling it to C++
- chrisbrandow 3y agoNifty! Of course, if one is interested in really understanding the nitty gritty implementation of a lot of Objective-C (and UIKit classes), then there is probably no better place to poke around than Mike Ash's blog. He now works for Apple and hasn't posted much since then, but the level of detail combined with the clarity of his writing made it a go-to source for everyone in early days of iOS development. It demonstrates why his getting hired by Apple for systems/language engineering was highly overdetermined. https://www.mikeash.com/pyblog/ https://www.mikeash.com/pyblog/
- lloydatkinson 3y agoI know basically nothing about the Apple language ecosystem but why does no one ever mention or seemingly use Objective C++? Is it that Objective C is a better version of C with OO and C++ features? If that’s the case why was Objective C++ created?
- filleokus 3y agoI've always understood Objective C++ to be a way to mix in C++ source code in an Objective-C code base, not really a new language but more of a "mode" of linking / compiling. > Objective-C++ does not add C++ features to Objective-C classes, nor does it add Objective-C features to C++ classes. For example, you cannot use Objective-C syntax to call a C++ object, you cannot add constructors or destructors to an Objective-C object, and you cannot use the keywords this and self interchangeably. The class hierarchies are separate; a C++ class cannot inherit from an Objective-C class, and an Objective-C class cannot inherit from a C++ class. In addition, multi-language exception handling is not supported From a Stack Overflow comment / archived Apple docs: https://stackoverflow.com/a/3684159 https://stackoverflow.com/a/3684159 https://web.archive.org/web/20101203170217/http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html https://web.archive.org/web/20101203170217/http://developer....
- jwells89 3y agoYes, in my experience the most common usages are to pair an Objective-C Cocoa UI to cross platform C++ internals, to use C++ for more performance critical parts of an app, or even to just be able to take advantage of the plethora of C++ libraries without going all-in on C++. Things might’ve changed with Swift being around now, but as I understand one place where the Obj-C UI + C++ internals setup used to be common was Audio Unit extensions due to the high latency sensitivity of anything working with audio.
- TillE 3y agoThat first one is probably the most common, you'll see .mm files for Apple-specific platform code in many big cross-platform C++ projects.
- dagmx 3y agoObjective C++ is just a way to mix C++ and Objective C code in the same file. It’s largely a convenience and not its own language so to speak. It’s functionally the equivalent of how you can use C code within C++ code, and a hint to the compiler to allow parsing of C++ syntax as well as ObjC
- saagarjha 3y agoIt sees heavy use, most of it just isn’t public. WebKit is, but for example Facebook uses it for their apps.
- jviotti 3y agoAuthor here. We do a LOT of Objective-C++ at Postman (https://www.postman.com https://www.postman.com), as our upcoming desktop framework is primarily C++.
- olliej 3y agoObjective-c++ is used extensively, it’s just not covered in most discussions of objc because then you’re having to explain the exciting interaction between it objc object semantics and C++’s. Eg the complexity skyrockets. The biggest public user of it would likely be webkit, but plenty of closed software both in and outside of apple use it. But the reality is that it is at this point primarily a bridging language. Post the introduction of ARC one of the biggest win/reason to use it over raw objc went away. But it’s still tremendously valuable as a way to have a nice API that is also ABI stable in a way that isn’t the C and C++ misery pit. Of course now swift has the same ABI stability support as well as a slew of other improvements so there seems ever less reason to write new objc.
- ksherlock 3y agoWhat's there to mention about it? Long ago, c++ exceptions and objective C exceptions didn't play well together but they've been unified so that's not an issue anymore. (In the beginning, Objective c exceptions were macros around setjump()/longjump(). Objective C 2.0 added actual language level support with @try/@catch/@finally/@throw which were compatible with C++ exceptions)
- astrange 3y agoYou shouldn't use Objective-C exceptions though; most code (and by default all ARC code) is not exception safe and will leak memory if you jump through it.
- flohofwoe 3y agoI bet there's a ton of Objective-C++ code around that serves as a shim between a C++ codebase and macOS/iOS system frameworks. The difference to plain ObjC isn't all that big except that the "C part" uses C++ semantics (I guess that's why nobody explicitly mentions it as ObjC++ but just files it under the ObjC umbrella name)
- eschaton 3y agoObjective-C++ was created to enable the creation of the Lotus Improv spreadsheet. The underlying spreadsheet engine was developed in C++ and bridging bidirectional via C to Objective-C for the UI was judged to be too inconvenient, and the developer tools folks at NeXT realized the C++ and Objective-C grammars were easily reconcilable so they put them together to allow mixing at the statement level. It worked great!
- mrpippy 3y agoI had no idea Clang had this rewriter, what an odd feature to keep around for all these years. I wonder if Apple actually uses/used it for anything.
- saagarjha 3y agoIt isn’t really supported anymore so I don’t think so.
- zer0zzz 3y agoWindows iTunes if memory serves.
- jviotti 3y agoDo you have a reference for this? Would love to include it in the post
- zer0zzz 3y agoNo just folk lore from folks I work with. I used to work at Apple and now work somewhere where we do a lot of iOS work where there are a lot of former Apple people that worked on Foundation AppKit clang etc in another life.
- robomartin 3y agoObjective-C is interesting. The problem I had back when we were using it was lack of performance. For us, in many cases, refactoring code down to C easily improved performance over 400 times. Code that struggled to deliver output in a usable time scale quickly became better than real time. Today, in computing, we are using far more energy than necessary because of the proliferation of inefficient languages in server, desktop and mobile codebases. I would guess we could achieve a non-trivial improvement in carbon footprint if computing were to have the same kinds of energy efficiency requirements we impose on appliances and vehicles. This study [0], published in 2017, evaluated the time, energy and memory efficiencies of some thirty languages. Sadly they did not include Objective-C. Swift did make the list. Table 4 shows the normalized results. C was most energy and time efficient and very close (third) in memory efficiency. Swift consumed nearly three times more energy, four times slower and nearly three times the memory. Python requires 76 times more energy, 72 times more time and three times more memory. Clearly there is a cost to choosing a language along these vectors. Given the ubiquity of computing in human life today, I am sometimes surprised that we have not become far more critical about permanently baking-in terribly bad energy inefficiency into systems and products. Developing energy-efficient software using languages like C, C++ and Rust isn't really that difficult. [0] https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sle...
- galad87 3y agoThe object oriented part of Objective-C was never meant for high performance code. Apple documentation always recommended using C when performances are an issue, which can be easily done because Objective-C is in fact C plus a bunch of object oriented concepts. However, in the end if you want every bit of performance you have to use SIMD intrinsics or write asm directly, so even C can be 8x times or more slower than a manually optimized code.
- robomartin 3y agoThe key point is: As a percentage of the totality of software engineering, virtually nobody looks at energy optimization. Smart phones? Well, the manufacturers likely pay attention to this to some degree. However, I can guarantee you that almost none of the millions of app developers working on third party apps ever look at energy as an optimization vector. That was my point, the carbon footprint of computing systems is dominated --and permanently marked-- by the energy-dominant language running on it.
- VancouverMan 3y agoPortable Object Compiler may be of interest, too. It's an open source compiler for a more traditional dialect of Objective-C, and it generates C code: https://sourceforge.net/projects/objc/ https://sourceforge.net/projects/objc/ The bootstrap source release of Portable Object Compiler itself shows the kind of C code the compiler generates from Objective-C: https://sourceforge.net/projects/objc/files/bootstrap/ https://sourceforge.net/projects/objc/files/bootstrap/
- eschaton 3y agoPOC isn’t really worth looking at as an Objective-C, its author was kind of a nut who long insisted that NeXT “ruined” Objective-C with its additions back in the early 1990s.
- Rexxar 3y agoI have started to do the same thing with go but it's largely unfinished. The next thing I will try to do when I have some time is to implement goroutine with the new c++20 coroutines. (https://github.com/Rokhan/gocpp https://github.com/Rokhan/gocpp)
- deviantbit 3y agoI loved Objective-C when I was developing on the NeXT. I haven't done with it much since then. I don't think it really got the credit it deserved. Templates would have been nice to have with it. Messages where so useful.
- eschaton 3y agoNah, template metaprogramming really doesn’t fit with the language philosophy; every message-send generates (essentially) the same code because it needs to be handled at runtime time, so templates won’t buy you anything. However, the lightweight generics that we added a decade ago so ago are great for when you want to add additional type hints to catch compile time bugs, just like explicit type declarations in general: They don’t affect the generated code, they just provide additional compiler diagnostics. Swift on the other hand goes far enough down the same terrible rabbit hole as C++ that, as with C++, its type system was recently demonstrated to be Turing complete. Thus long term maintenance of large, complex codebases is going to be much more difficult because you can no longer look at code in isolation and have a basic idea of what it does.
- deviantbit 3y agoI disagree. Templates would have given messages a whole new world. I haven't looked at Objective-C generics. They didn't exist in 1989.
- Towaway69 3y agoSlightly off topic but did anyone ever use Objective-J[0], specifically Cappuccino which was a great framework for building JS frontends. I built an application using Cappuccino after having built a bunch of apps for iPhone and it was the most intuitive framework after getting involved in Xcode and Co. Is cappuccino still a thing since Apple went to swift? [0] https://www.cappuccino.dev/learn/objective-j.html https://www.cappuccino.dev/learn/objective-j.html
- JimRoepcke 3y agoI built an app using Cappuccino and Objective-J back around 2010. We even built an EOF/CoreData clone on top of it. Good times. It was incredible to work with, but ultimately not very “webby”. I’d rather have WebObjects back. It should have never died.
- Towaway69 3y agoLooking back it's amazing how much effort the authors made to make it objective-C like and not at all like any other Web technology. I guess that made it too niche for a broader audience.
- dottrap 3y agoI saw a few presentations from the 280 North guys. It was clearly impressive what these 3 guys built. Learning from Cocoa/Objective-C/Interface Builder well, they realized they needed to build 3 separate, but interconnected things, the framework (Cappuccino), the language (Objective-J), and the GUI builder (Atlas). They sold to Motorola for $20 million in 2010, and that was the kind of the last I heard. https://news.ycombinator.com/item?id=1631002 https://news.ycombinator.com/item?id=1631002
- zer0zzz 3y agoI’m not sure if the authors comment that the rewriter isn’t used is accurate. It might have been used for the windows port of iTunes.
- _3u10 3y agoIt’s mostly just objc_msg_send and a couple other methods in that header.
- k4st 3y agoCool! You might even be able to run Rellic [1,2] on the LLVM IR produced by Clang when compiling Objective-C code. If it works, this will spit out goto-free C code, not C++. [1] https://github.com/lifting-bits/rellic https://github.com/lifting-bits/rellic [2] https://blog.trailofbits.com/2022/05/17/interactive-decompilation-with-rellic-xref/ https://blog.trailofbits.com/2022/05/17/interactive-decompil...