News for hackers. Stuff that matters.
Re:OS X app in plain C by conradev on Thursday May 05, 2016 @02:52PM (#11638794)
> The ability to use a high-level, dynamic language (ObjC), C, and even inline assembly in a single source file is unique to Objective-C (at least among the "mainstream" languages), and something I think is often under-appreciated.

You can even use C++, too, which is awesome. Putting Objective-C objects in C++ structs "just works" thanks to ARC.

One thing most people don't know is that Objective-C was originally implemented as a precompiler for C.

Re:OS X app in plain C by valarauca1 on Thursday May 05, 2016 @03:43PM (#11639136)
Objective-C was a modification of the GCC developed by NeXT. NeXT didn't publicly release their patches and the FSF/GNU threatened legal action. NeXT released their sources and the GCC now supports Obj-C.

Source: https://en.wikipedia.org/wiki/GNU_General_Public_License#Leg...

Re:OS X app in plain C by msbarnett on Thursday May 05, 2016 @05:05PM (#11639658)
> Objective-C was a modification of the GCC developed by NeXT.

No.

Objective-C was developed in the early '80s (as a C pre-processor, as the Grandparent post said) by Brad Cox, and commercialized by himself and Tom Love via a company they created together called Stepstone. Cox wrote a book about their product, Object Oriented Programming: An Evolutionary Approach, in 1986. I own a copy. It's pretty good!

Some time later, NeXT decided to build their user-space APIs around Objective-C, and bought the rights to Objective-C from Stepstone outright. It was at that point that they started modifying GCC to directly compile Objective-C rather than pre-process it into C.

Re:OS X app in plain C by pixelglow on Thursday May 05, 2016 @08:34PM (#11640829)
I have the book too. It's interesting as a history lesson and initial motivations for Objective-C.

In particular, in the beginning Objective-C was a lot looser typed at compile time than it is now: no NSString* or NSWindow*, it was all just id. It was an attempt to make C look like Smalltalk.

The language eventually evolved more toward the static-typed end of the spectrum, culminating in support for lightweight generics and ostensibly birthing Swift along the way.

Re:OS X app in plain C by mpweiher on Thursday May 05, 2016 @06:28PM (#11640153)
> Objective-C was a modification of the GCC developed by NeXT.

No. Objective-C started out as a set of C Macros, then an actual pre-processor was created, mostly to deal with uniquing selectors. Once the pre-processor was there it was used to create actual syntax.

Documented in "Object Oriented Programming: An Evolutionary Approach".[1] Still one of the best books on OO out there, because it treats OO as an architectural style with tradeoffs relative to other styles, rather than as a religion to be accepted (or nowadays as the devil to be cast into hell).

It also clearly describes the deliberate hybrid style that seems to be mostly forgotten now: object largely implemented in C, but connected via dynamic messaging. "Software-ICs".

I am somewhat surprised by seeing the 1986 release date, I guess I must have gotten it pretty soon after it was published. Used it as a template to implement an Objective-C pre-processor + runtime + basic classes on my Amiga (had just gotten a C compiler for it).

One of the reasons I got a NeXT was because NeXTStep was largely implemented in Objective-C, which to me meant they "got" it. And it also meant I could enjoy programming in Objective-C without having to maintain my own :-)

[1] http://www.amazon.com/Object-Oriented-Programming-Evolutiona...

Re:OS X app in plain C by colejohnson66 on Sunday May 08, 2016 @05:51PM (#11656092)
> No. Objective-C started out as a set of C Macros, then an actual pre-processor was created, mostly to deal with uniquing selectors. Once the pre-processor was there it was used to create actual syntax.

I'm curious. What did that early form of Obj-C look like?

Re:OS X app in plain C by conradev on Thursday May 05, 2016 @04:18PM (#11639348)
More details:

> In order to circumvent the terms of the GPL, NeXT had originally intended to ship the Objective-C frontend separately, allowing the user to link it with GCC to produce the compiler executable. After being initially accepted by Richard M. Stallman, this plan was rejected after Stallman consulted with GNU's lawyers and NeXT agreed to make Objective-C part of GCC.

Source: https://en.wikipedia.org/wiki/Objective-C#Popularization_thr...

Re:OS X app in plain C by jheriko on Thursday May 05, 2016 @07:25PM (#11640487)
you mean objective-c++? c++ has no idea about obj-c...

another thing most people forget is that the entire programming community rejected obj-c as deficient in its very early days... it only persists thanks to the ego of former nextstep employees afaik. and now apple employees got lumped with it... which i'd suspect helped create the internal pressure for swift... so some good came at the end of it all. :)

Re:OS X app in plain C by astrange on Thursday May 05, 2016 @08:26PM (#11640793)
Objective-C was the major inspiration for Java - you'll notice how little it looks like C++ - and therefore C# and co, so it's been very influential ever since the 90s.

It's not worth listening to anyone who dislikes Obj-C just because of the [] syntax, which doesn't matter after the first 48 hours.

Re:OS X app in plain C by _asummers on Thursday May 05, 2016 @10:13PM (#11641226)
This about mirrors my opinions about Objective-C.

http://twistedoakstudios.com/blog/Post8237_a-years-worth-of-...

Re:OS X app in plain C by astrange on Friday May 06, 2016 @07:43PM (#11647527)
> Lastly, neither has generic types.

Not true anymore; ObjC has generics with type erasure. (But not all the cases can be exported to Swift.)

> Instead, Objective-C has nil, which is like null except it won’t stop the program when you accidentally use it.

That's… not quite right… but I guess he has the visible effects down. Anyway, ObjC has nullability annotations now for this.

> For example, in Objective-C many objects have both a “core foundation” form and a “new style” “NeXTSTEP” form

This is not legacy baggage, it's an intentional C bridge.

> This is justified by conventions strongly favoring you not catching exceptions, and apparently the speed benefits are non-negligible, but it still blows my mind that Apple has their language default to incorrect behavior.

He shouldn't use ARC+exceptions, because Cocoa itself is not exception-safe. Just quit the app if you get one.

Re:OS X app in plain C by chillacy on Friday May 06, 2016 @03:54AM (#11642398)
> It's not worth listening to anyone who dislikes Obj-C just because of the [] syntax, which doesn't matter after the first 48 hours.

That's pretty much my thoughts these days. If you have only the most superficial understanding of these languages, then that's what you'll get caught up on, but that's like saying Java and JavaScript are similar because they both use curly braces and dot notation for their methods. Yea, that is true, but it's a superficial similarity.

Re:OS X app in plain C by informatimago on Friday May 06, 2016 @09:48AM (#11643910)
Objective-C is what makes developping applications on iOS so much more efficient than on Android; even if only indirectly, thru the frameworks provided: if Android cannot provide frameworks usable more efficiently than Apple, it's because they're using Java instead of Objective-C.

https://infinum.co/the-capsized-eight/articles/android-devel...

Re:OS X app in plain C by dep_b on Thursday May 05, 2016 @03:39PM (#11639105)
I think it actually was a precompiler for C up til a point. Still Objective-C objects have underlying C functions that are being called whenever you try to call a method or anything on an Objective-C object. Theoretically you should be able to call functions on Objective-C objects from C.
Re:OS X app in plain C by conradev on Thursday May 05, 2016 @04:10PM (#11639309)
> Still Objective-C objects have underlying C functions that are being called whenever you try to call a method or anything on an Objective-C object.

The same goes for C++ or Swift. Getting the function pointer for a method can be involved, but all functions adhere to the C ABI.

Re:OS X app in plain C by poizan42 on Thursday May 05, 2016 @04:20PM (#11639373)
At least MSVC on x86 uses "thiscall" by default which puts the this pointer in ecx. You cannot call those functions from C without specific compiler support.
Re:OS X app in plain C by vardump on Thursday May 05, 2016 @06:39PM (#11640219)
Sure you can.

Just write an asm stub to call, that sets up ecx and other things right.

Re:OS X app in plain C by poizan42 on Thursday May 05, 2016 @08:33PM (#11640823)
The claim was: The same goes for C++ or Swift. Getting the function pointer for a method can be involved, but all functions adhere to the C ABI.

This is clearly false since at least one C++ compiler generates functions that does not adhere to any of the common calling conventions for C on that platform (or "C ABIs" if you will).

Besides that, I said you could not call them from C. An asm stub is not C. Neither is inline assembly even though it's a common extension to C.

Re:OS X app in plain C by jheriko on Thursday May 05, 2016 @07:27PM (#11640498)
thats not true if you inspect the details of the specific ABI and cheat a little or use some inline assembler... but cl is easily the worst compiler for a C++ ABI.

they break it on tiny incremental updates sometimes... at least historically (in fairly recent history at least).

Re:OS X app in plain C by poizan42 on Thursday May 05, 2016 @08:43PM (#11640864)
> thats not true if you inspect the details of the specific ABI and cheat a little or use some inline assembler...

Some x86 C compilers supports calling conventions that lets you put things in registers, e.g. Microsofts "fastcall". But I wouldn't consider that part of the standard "C ABI" for Windows given that it's practically never used for externally visible functions. As I said "without specfic compiler support".

Using inline assembler is just cheating, it's not C.

> but cl is easily the worst compiler for a C++ ABI. they break it on tiny incremental updates sometimes... at least historically (in fairly recent history at least).

That is more about name mangling schemes and layout of standard library classes though. As far as I know the calling conventions has been quite fixed for a long time.

Re:OS X app in plain C by chris_wot on Thursday May 05, 2016 @07:45PM (#11640588)
What C ABI? Can you point me to the spec?
Re:OS X app in plain C by deathanatos on Friday May 06, 2016 @02:08AM (#11642030)
I don't believe C standardizes the ABI across all OS/hardware combinations. But some OS/hardware combinations are standardized; for example, there's the AMD64 ABI[1], which I think just about every operating system that runs on amd64 uses … except Windows.

[1]: http://www.x86-64.org/documentation/abi.pdf

Re:OS X app in plain C by chris_wot on Friday May 06, 2016 @12:12PM (#11644912)
Yeah, but that's a bit of a long bow... That's not a C ABI :-)