
Objective-C Generics - jawngee
https://github.com/tomersh/Objective-C-Generics
======
pkaler
This isn't really generics in the C++ or Ada sense. This is code replacement
using preprocessor macros and the stringize operator to create homogenous
containers.

Generic programming usually implies that a package, class, or function is
parameterized by types. And a generic/template instantiation phase has been
added to the toolchain after the preprocess stage but before the type checking
phase.

I would have called this homogenous containers.

>> Now you can use generics with arrays and sets just as you normally do in
Java, C#, etc.

I would have documented that as: "Now you can use homogeneous containers with
arrays and sets just as you normally do in Java, C#, etc."

~~~
cowls
Yes they are actually called generics in Java..

So this is just going to cause confusion, though I am very impressed with all
the long words you used.

~~~
smikhanov
In Java, generics is a possibility to parametrize any type (not just a
container) with another type. Just like in any other language that support
generics.

If you limit generics in Java to container types only (like at the original
link), it would be absolutely more correct to call the result "homogenous
containers".

Take Callable<T>, for example. This is a generic type that is not a container
and is not in the scope of project at the OP's link.

------
fleitz
Generics don't make much sense in dynamic languages.

You don't need to cast to the correct class to call a method.

If the only selector you use is length then any object that responds to length
will work.

~~~
cageface
As other posters have stated, they're very useful for collection classes. I've
had a lot of stupid bugs in Obj-C that would have been caught by the compiler
if it could verify that only the right kinds of objects are going into an
NSArray, for instance.

------
lukeredpath
Other than adding dozens of methods to NSArray/MutableArray for each "generic"
you define and limiting my choice of collection to an array (rather than a set
or dictionary), why would you use this rather than define your own collection
class that keeps the underlying store encapsulated and enforces its own type-
safety?

If type-safety is that important, a custom collection class will enrich your
domain model and be more obvious.

~~~
btown
Viewing the source code, it does seem to support sets and dictionaries.

------
RyanZAG
This is pretty awesome - it detects a lot of potential bugs you can miss when
moving objects into and out of collections and ensures you always know what
types of objects are expected when passing collections into other controllers.
I'll definitely be giving it a try, thanks.

------
jheriko
This is interesting - a 'clever' trick to give the functionality of generics.

Although one could argue that its also fantastically stupid by using a macro
in such a way that isn't obvious on inspection and might necessitate reading
the header.

I do quite like it however...

I'd imagine that Objective-C prevents any of the performance benefits gained
by the compiler knowing the types with its heavyweight super dynamic late
binding golden sledgehammer of an architecture. I'd be curious to measure it
when I get some time...

~~~
chrisdevereux
It won't. If the compiler attempted to inline, it'd break runtime method
replacement and all sorts of other stuff.

One problem with this approach is that using protocols to emulate C++/Java
generic syntax will cause name collisions if a class already has the same name
as a protocol. NSArray<NSObject> won't work for example, because NSObject is
already a protocol.

~~~
jheriko
> it'd break runtime method replacement and all sorts of other stuff.

yeah, i thought as much. thanks for saving me the effort. :)

------
MaxGabriel
I'm pretty sure with this implementation you can't have multiple generics in
one file because the macros clash or something (working off memory here).

Edit: here's the issue: [https://github.com/tomersh/Objective-C-
Generics/issues/4](https://github.com/tomersh/Objective-C-Generics/issues/4)

------
sgt
Does this work outside of the classes defined in ObjectiveCGenerics.h? E.g.
outside of NSArray, NSSet, NSMutableArray and so on.

~~~
Tloewald
Clearly not. (Or do you mean, could support be added for other things?)

------
seivan
How well does this work? If it works as intended, then this is great!

------
mogui
just... why?

------
greatsuccess
Cue pointless duck typing flamewar...

------
mrcactu5
where is the bottle neck in such code? why is this faster than say Norvig
spell checker? [http://norvig.com/spell-correct.html](http://norvig.com/spell-
correct.html)

~~~
mrcactu5
don't be a dick, just answer the question.

~~~
millerm
I think you meant to reply to the "1000x Faster Spelling Correction: Source
Code released" comment section.

[https://news.ycombinator.com/item?id=7048225](https://news.ycombinator.com/item?id=7048225)

Oh, don't be a dick if I am wrong.

~~~
mrcactu5
congratulations! you're a dick

