If we inspect your point about compilation vs interpretation, we ought to note that this is a technical decision, not an implementation constraint. Mono compiles C#/F# into native code when targeting iOS.
If we look at lldb, I think your assessment is overly generous. lldb is a new, buggy, but promising debugger. Most languages -- including C#, Java, Haskell, OCaml -- have a standard and viable debugger, and this does not particularly set Objective-C apart.
As for libdispatch, it simply a closure-based thread-pool executor / IO framework, like you'd find in any other language. It's better than pthreads and serves as a nice interface on top of kqueue, but it's certainly not so novel as to be the "best concurrency framework ever".
For example, compare against Microsoft's Reactive Extensions; Rx allows one to implement concurrency declaratively, rather than imperatively with chained closures as is done in with libdispatch.
http://rxwiki.wikidot.com/101samples#toc3
Lastly, I wish to address header files. Everything in a header file could be derived automatically from the original code. This is done on almost every other modern language platform, and there's no reason it shouldn't be done here.
Furthermore, headers do not elide the need for documentation; Objective-C type system is not nearly expressive enough to fully articulate the constraints and invariants of an API, and undocumented APIs are inescapably and destructively ambiguous APIs.
Apple even sets a good example here by providing complete documentation for every supported API in the system.
The Objective-C calls are not really compiled. They are dynamically dispatched at runtime in a very equivalent manner to an interpreter or via reflection in Java. You can read about it here:
http://www.mulle-kybernetik.com/artikel/Optimization/opti-3....
Quite far from the compiled C calls also covered in the linked article.
C code in an Obj-C method is compiled to the exact same machine code as it would be, were it in a C function. Constructs such as ifs, loops, return statements etc are no different than their C counterparts, are are just as fast. Obj-C method calls are simply compiled into calls to the ultra-fast runtime C function `objc_msgsend`.
Java execution is split into two pieces. First is the interpeter, which is used to execute bytecode prior to it being compiled. The second is the compiler, which implements runtime optimized compilation of hotspots into native machine code, including the ability to make assumptions regarding types and then uncompile hotspots if those assumptions prove false.
Before you say "ah ha, an interpeter!", it's also notable that the JVM's interpreter is not an interpeter in the traditional sense (ala Ruby). Rather, the JVM's interpreter is an architecture-specific interpreter that decodes JVM bytecode and spits out unoptimized direct machine code based on a standard set of machine code templates. This is fast, but there's not optimized compiler, and so it's not nearly as fast as the result of the optimizing compiler that is run over the hotspots.
All that said, JIT vs AOT is a technical decision with mixed advantages. In theory, there are optimizations that can only be performed via JIT, based on runtime analysis. The simplicity of AOT can often provide runtime performance gains simply by avoiding the runtime costs of evaluation and compilation. JIT of bytecode allows for binary/library portability across different machines without worrying about rebuilding or shipping multiple binaries. AOT is somewhat more difficult to decompile compared to unoptimized byte code.
Mono compiles down AOT for iOS, in theory you could do the same with Java. GCC did so with their optimizing AOT gcj Java compiler.
For C#, the execution process is C# -> compiler -> CIL bytecode at compile time. Then at runtime CIL bytecode -> JIT -> machine code.
So sure, C# (and I believe Java is similar to C# in this regard) eventually gets compiled to machine code, but it's vastly different from what I believe to be the accepted usage of being a "compiled language." The exception to this being if you AOT compile your code, but in that case you lose sacrifice some language functionality.
I can't agree with this at all. Most objects show up as opaque with no members. You can't even browse the contents of collection classes. Compared to a managed language debugger it's positively stone age.
Something I found today: "p/x $rip" will show you the PC, but "break set -a $rip" won't set a breakpoint there, because lldb can't figure out what $rip means.
The online help is also not very helpful.
Please elaborate why Objective-C's approach to memory management is Right, and why GC is Wrong.
Additionally, and importantly, without manual memory management, you cannot safely use pointers and memory directly. The whole idea of playing with memory directly by doing `void buffer = malloc(100)`, and being able to do pointer arithmetic such as `(buffer + 99) = NULL` goes out of the window.
Lastly, Automatic Reference Counting is awesome in that it brings you all the benefits of manual memory management (not having to worry about making mistakes, and saving yourself a little typing) without any of the drawbacks.
There are advantages and drawbacks to GC vs reference counting (whether manual or automated), but the disadvantages of GC certainly do not include doing "barely acceptable job of managing memory, and often lead to hard-to-diagnose bugs." I'd provide a more complete rebuttal, but lacking any specific examples, I can only definitively state that GC significantly reduces the likelihood of having to debug memory related issues, especially tracking down leaks due to reference cycles.
In comparison to GC, the main advantage of reference counting systems is that they are deterministic.
However, they do a lot of book-keeping that is expensive in aggregate and may not even be necessary in a garbage collected system. There are GC designs that mix GC and reference counting to achieve the best of both -- another poster in this thread provided a link to some papers on the subject.
The major downside to reference counting -- other than the expensive-in-aggregate book keeping mentioned above -- is, as mentioned above, that they can't handle reference cycles.
On iOS it's terrible easy to create cycles, and prior to the introduction of __weak, it was almost impossible to build thread-safe code that also involved a cyclic reference, due to it being impossible to invalid another thread's reference to your object (short of hacking out your own zeroing weak reference implementation, which is what people were forced to do).
This ease of creating cycles, especially with blocks and GCD, leads to a proliferation of careful and manual application of __weak designators on self-references. Instead of simply using an instance variable from a block, you must be certain to only access it through a __weak-marked references.
Additionally, you now have to worry about NULL-dereferencing! Example:
__weak MyObject *weakself = self;
_block = ^{
// Whoops! weakself could be nil here. Should have used a property,
// which would have just passed a nil argument to runWithTX. Whoops!
[opManager runWithTX: weakself->_txid];
// Also, weakself could be deallocated *after* the above call succeeds,
// so what we really needed to do as a first step in the block was
// capture a -strong- reference to weakself, and return immediately
// if we'd been deallocated
MyObject *strongSelf = self;
if (self == nil)
return;
// Whoops, accidentally referenced self via _txid below,
// causing a retain cycle
NSLog(@"Running with _txid=%@", _txid);
});
This is awful. If the language used a hybrid ARC refcounting with cycle breaking, that code would simply read: _block = ^{
[opManager runWithTX: _txid];
NSLog(@"Running with _txid=%@", _txid);
});http://stackoverflow.com/questions/7874342/what-is-the-diffe...
Regarding reference cycles, it is not a difficult job at all for any decent coder to work it out. It is a design issue in your application if you think its hard.
If you use protocols and delegates correctly, you can manage it quite simply with the rule of storing delegates as weak pointers.
If you use blocks, you just have to avoid the case of both storing a block in an object and referencing the object from the block.
I had not used a Mac till about 6 months ago and in the last 6 months, I lead the development of an enterprise scale app for iOS dealing with the above issues quite easily by having a good architecture. I have previously worked professionally for around 4 years with Qt/C++ and found it extremely easy to make the switch to iOS IMHO.
Dealing with cycles is not complex, but it is error prone, verbose, and difficult in aggregate as compared to having cycles correctly and automatically handled.
Pointer arithmetic specifically, or direct memory access more generally, is bug prone and dangerous. Its valuable primarily in terms of performance, and is a terrible idea in terms of correctness and security.
In runtime-managed languages, efficient access to memory buffers is achieved by exposing an array type, and then performing basic bounds checking on array access. This is efficient enough for most purposes, is essentially what you achieve by using NSData, and is generally implemented in a way that is vastly more efficient than NSData.
Direct memory access in straight C will be faster, but it's also enormously error prone, and something I avoid if at all possible when writing ObjC or even straight C.
While there's no discounting the performance value of being able to operate at this level (or write straight assembly), it's rarely a useful thing to do when writing most application-level code, especially when weighed against the propensity for failure.
All managed languages allow for extension through C for the cases where low-level performance is required. ObjC has the easiest means of working with C, but that should be balanced against the fact nearly all of ObjC's ugly warts and pain points derive from being a strict C superset.
Without unsafe memory management, you can still do pointer arithmetic. See Cyclone's fat pointers, Go's slices, Rust's slices, etc.
For Java you have interpreters, JIT and native compilers.
C# is always compiled (either JIT or AOT), and both Mono and Microsoft Research have native compilers available for CLR free binary distributions.
One nit: the language is actually not tied to the Apple platform (although Apple bought the name at one time and is definitely the biggest user). There are actually several runtime/compiler/Foundation combos, most prominent of which are GNUStep and Cocotron. I have used both and run my (non GUI) code on Windows, Linux, Solaris, heck even AIX.
"It's missing too many useful libraries to be useful."
libdispatch can be lived without or worked around. What else in your list is missing?
Only stone age languages insist in using header files.
i.e. More suited to some kind of read-only overview browser than just another finicky text file.
Something similar could be done with Objective-C, judging by the output of the `class-dump' tool (see http://www.codethecode.com/projects/class-dump/). If you run it on an app's private frameworks - I hear that Xcode 4 might demonstrate the principle - you'll get back a surprisingly complete set of interface, method and ivar declarations. This suggests that if the will were there, we could do away with header files, even for libraries shipped as compiled objects. It works well for the CLR.
- It's compiled, not interpreted. Even Java and C# are compiled into bytecode for a massive VM to interpret. Objective-C has no VM, it's pure binary + a shared library to implement the runtime. It also uses the very best compiler, clang, which gives incredibly helpful error messages, warnings and suggestions.
- It's a strict superset of C. Even C++ does not meet this criteria. This means any valid C is valid Obj-C and behaves exactly the same way, in any Obj-C file. You can drop down levels of abstraction for performance, and can even write assembly if that's what it takes.
- It has an amazing and fully supported debugger in the form of LLDB.
- It's fully dynamic, allows introspection, duck typing and even monkey patching (with a little effort; method swizzling). Everything is an object, except for native C types.
- It takes the right approach to memory management. Realizes that garbage collectors are abominations, and that managing memory is really the job of either the developer or the compiler.
- It has the most fantastic concurrency framework I've ever used, in the form of `libdispatch`. Now, to be fair, it's a C library that ought to work anywhere, but practically it only works well on Apple's platforms and using clang.
- Apple is moving in the right direction, cleaning up the language, removing annoyances and making syntax more succinct.
- Header files. I think I'm on my own in liking this—but I think headers are amazing. They're a succinct and standalone version of a documentation file that is extremely useful to both developers and the compiler. Use them to describe well the public interface to your class, and you don't even really have to write documentation anymore.
- Solid design patterns and convention-driven. These are getting better by the day, with the recent addition of closures to the language.
There's two major annoyances:
- It's often needlessly verbose. Not in the syntax, mind you, which is just fine, but in the naming conventions. For example, `NSArray` has a method called `enumerateObjectsUsingBlock:` instead of simply `each:`. Add that up to every single name, and you get pretty ugly code—and good luck writing it with anything other than Xcode's context-aware autocompletion.
- It's tied to Apple's platforms, and well not run on anything else. Now, I'm fine with Apple-specific frameworks and GUI frameworks (like QuickTime or UIKit) being Apple-only, but I'd really love to use the language to write a backend service, for which I'd only need Foundation & libdispatch, for example.