News for hackers. Stuff that matters.
Re:The eero programming language - a dialect of Objective-C by SeoxyS on Saturday July 07, 2012 @01:44PM (#4212219)
Objective-C is hands down one of my favorite programming language:

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

Re:The eero programming language - a dialect of Objective-C by flatline3 on Saturday July 07, 2012 @04:57PM (#4212705)
Your list reminds me quite a bit of the effusive language used by Apple engineers at WWDC, but does not reflect the larger context and objectivity of the industry outside of Apple.

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.

Re:The eero programming language - a dialect of Objective-C by spullara on Saturday July 07, 2012 @03:20PM (#4212441)
"It's compiled, not interpreted."

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.

Re:The eero programming language - a dialect of Objective-C by SeoxyS on Saturday July 07, 2012 @04:19PM (#4212604)
The presence of a runtime doesn't negate the fact that code is compiled to native machine code, and all the benefits that come with that. Execution speed is quite a bit faster than bytecode-interpreted languages such as the JVM, which in turns is orders of magnitude faster than fully interpreted languages such as Ruby. The code is compiled directly for each architecture the binary supports (choices these days being i386, armv6, and armv7).

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`.

Re:The eero programming language - a dialect of Objective-C by flatline3 on Saturday July 07, 2012 @05:14PM (#4212745)
The JVM compiles the bytecode into machine code, and it compiles the hotspots of an application into optimized native machine code.

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.

Re:The eero programming language - a dialect of Objective-C by spullara on Saturday July 07, 2012 @05:11PM (#4212741)
Java hasn't been bytecode interpreted in 10 years. The objc_msgsend function is not fast. Measure it and see.
Re:The eero programming language - a dialect of Objective-C by bronxbomber92 on Saturday July 07, 2012 @04:21PM (#4212615)
To be fair, their still is a difference between Objective-C and C# and Java (which the OP was trying to contrast). Dynamically dispatched (i.e. a table look-up or pointer dereference) doesn't change the fact that the callee (the method to be executed after being found by the runtime) or the caller (code sending the message & perhaps all the runtime doing the lookup) are both compiled into executable code. This is surely different than how Java and C# are compiled/executed (in most cases) -- which I think was the intended point :).
Re:The eero programming language - a dialect of Objective-C by spullara on Saturday July 07, 2012 @08:51PM (#4213192)
Java and C# are compiled into machine code before execution. Also, Objective-C isn't table lookup or pointer dereferenced, it is more akin to a (cached) reflective lookup and dispatch.
Re:The eero programming language - a dialect of Objective-C by bronxbomber92 on Saturday July 07, 2012 @09:42PM (#4213285)
You're right that method invocations aren't simply a table lookup or pointer dereference, but in the end it boils down to a jump to the address of some all ready compiled (machine) code which is what I was trying to make a point of.

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.

Re:The eero programming language - a dialect of Objective-C by stcredzero on Saturday July 07, 2012 @04:02PM (#4212557)
Most developers are just interested in the compile-time type verification.
Re:The eero programming language - a dialect of Objective-C by tikhonj on Saturday July 07, 2012 @04:06PM (#4212570)
In which case it is no different from Java or C#: both also give you compile-time checks.
Re:The eero programming language - a dialect of Objective-C by stcredzero on Saturday July 07, 2012 @11:03PM (#4213435)
My comment is more about the programmers than it is about the languages.
Re:The eero programming language - a dialect of Objective-C by SeoxyS on Saturday July 07, 2012 @04:23PM (#4212620)
The compiler and static analyzer nowadays can do a lot more than that, it can catch typos, dead ends, nonsensical statements, memory management issues (such as leaks or usage of a dangling pointer), unguarded implicit casts or assignments in if clauses, and a lot more. It's saved my ass countless times, catching bugs before they ever hit production.
Re:The eero programming language - a dialect of Objective-C by cageface on Saturday July 07, 2012 @04:46PM (#4212670)
It has an amazing and fully supported debugger in the form of LLDB.

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.

Re:The eero programming language - a dialect of Objective-C by emp on Saturday July 07, 2012 @08:08PM (#4213109)
I think he means the LLDB console. You can inspect anything, however deeply you want. Create new objects. Assign them to variables. Run code snippets against your existing state at the breakpoint. Learn new APIs interactively. Reduce the need to compile/run as often. It's the closest I can get to a Smalltalk Workspace/Transcript. I agree that the GUI debugger is limited, and after living in the console for some time, next to useless.
Re:The eero programming language - a dialect of Objective-C by to3m on Saturday July 07, 2012 @08:31PM (#4213151)
I use lldb from Xcode, but I don't use the GUI part, because it's worse than useless. The command line is better, but not by THAT much. It seems to regularly get confused by debug info, so if it fails to tell you "no member info found due to forward declaration of struct XXX" (or something similar) when you're trying to print a local value of type XXX, you'll just get gibberish. Then sometimes it just dies when stepping over function calls.

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.

Re:The eero programming language - a dialect of Objective-C by rudiger on Saturday July 07, 2012 @04:24PM (#4212623)
- 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.

Please elaborate why Objective-C's approach to memory management is Right, and why GC is Wrong.

Re:The eero programming language - a dialect of Objective-C by SeoxyS on Saturday July 07, 2012 @04:35PM (#4212642)
Because memory management is something that's known at compile time, and doesn't need to be evaluated at runtime. It's the same reason a compiler will essentially substitute the expressions `2 + 2` to simply `4` and save the extra CPU cycle. Most GCs do a barely acceptable job of managing memory, and often lead to hard-to-diagnose bugs. With manually managed memory, you know exactly what's going on and can easily inspect what's actually going on by diving into a debugger. You're also not wasting memory letting the runtime hold onto things you don't need anymore, or wasting CPU cycles trying to figure out what's still needed.

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.

Re:The eero programming language - a dialect of Objective-C by flatline3 on Saturday July 07, 2012 @05:30PM (#4212788)
I'd just like to touch on GC vs ARC/RefCounting, and leave the buffer mangling for another comment.

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);
  });
Re:The eero programming language - a dialect of Objective-C by jim_kaiser on Sunday July 08, 2012 @07:14AM (#4214333)
I think you are a little confused as to how exactly ARC works. ARC is mostly compile-time and adds the memory management code automatically during compile time. So, the runtime overhead compared to a GC is much lower.

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.

Re:The eero programming language - a dialect of Objective-C by flatline3 on Sunday July 08, 2012 @07:23AM (#4214341)
I'm not confused as to how ARC works. Reference counting systems require a high level of bookkeeping for short lived objects as compared to GC systems.

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.

Re:The eero programming language - a dialect of Objective-C by flatline3 on Saturday July 07, 2012 @05:39PM (#4212814)
After covering reference counting in my first reply, I want to tackle the pointer arithmetic question here.

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.

Re:The eero programming language - a dialect of Objective-C by pcwalton on Saturday July 07, 2012 @07:39PM (#4213056)
Reference counting is a form of garbage collection. It does spend CPU cycles trying to figure out what isn't needed, so your claim that it doesn't "waste CPU cycles" is not true. Its advantages over tracing GC are that memory can be reclaimed promptly, that it does a pretty good job of spreading out the CPU load in an incremental sense, and that it's simple to implement and reason about. The big downside is cycles; lots of large reference-counted codebases constantly struggle with cycles (for example, Firefox). Adding cycle detection to a reference counted system is not easy to do in a performant way and almost nobody that I'm aware of but Firefox even attempts to do it.

Without unsafe memory management, you can still do pointer arithmetic. See Cyclone's fat pointers, Go's slices, Rust's slices, etc.

Re:The eero programming language - a dialect of Objective-C by pjmlp on Saturday July 07, 2012 @04:50PM (#4212683)
> It's compiled, not interpreted. Even Java and C# are compiled into bytecode for a massive VM to interpret.

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.

Re:The eero programming language - a dialect of Objective-C by mpweiher on Saturday July 07, 2012 @01:49PM (#4212229)
Agree with a lot, it's also my favorite, and not because any big company told me to like it. In fact, I found Objective-C even before NeXT, let alone Apple picked it up.

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.

Re:The eero programming language - a dialect of Objective-C by SeoxyS on Saturday July 07, 2012 @01:57PM (#4212251)
My limited experience with GNUStep has been disappointing. It's missing too many useful libraries to be useful. I haven't used Cocotron, personally, though I imagine they're related. So while technically what I said is untrue, I believe that practically, it is.
Re:The eero programming language - a dialect of Objective-C by DHowett on Saturday July 07, 2012 @07:41PM (#4213059)
"I'd only need Foundation & libdispatch."

"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?

Re:The eero programming language - a dialect of Objective-C by neutronicus on Saturday July 07, 2012 @02:02PM (#4212262)
I completely agree about header files. Having all of the type signatures and argument names associated with a library in one place is great.
Re:The eero programming language - a dialect of Objective-C by pjmlp on Saturday July 07, 2012 @04:55PM (#4212696)
Header files are a relic from the 70's, which was replaced by sane module systems in the 80's.

Only stone age languages insist in using header files.

Re:The eero programming language - a dialect of Objective-C by frou_dh on Saturday July 07, 2012 @03:53PM (#4212528)
When using an IDE, that seems the type of thing that should be automatically derived from code rather having to be written and maintained by hand.

i.e. More suited to some kind of read-only overview browser than just another finicky text file.

Re:The eero programming language - a dialect of Objective-C by cageface on Saturday July 07, 2012 @04:50PM (#4212679)
Absolutely. I recently started working on an Android port of an iOS app of mine and it's a big time saver letting the tools take care of these details. Not to mention how much more powerful refactoring tools can be when they don't have to worry about a preprocessor.
Re:The eero programming language - a dialect of Objective-C by to3m on Saturday July 07, 2012 @08:39PM (#4213168)
Visual Studio will generate class declarations for CLR classes from the associated metadata, if you do a `go to declaration' on a system class.

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.