This week I'm trying to learn objective C. It's quite a nice language, with reference counted garbage collection (albeit manual reference counting), and fairly loose typing (compared to C++ or Java that is).
The syntax is rather strange though. I think that's the most offputting thing about the language. To call a method on an object, it looks something like:
[myobject method]But then if there's a parameter to go into the method, it has a colon in it:
[myobject doSomethingWith:anotherobj]It's also got some named parameters stuff, so you can have:
[foo insertObject:bar atIndex:5]Note that the first parameter is
bar
, but it's almost not a named parameter. This is interesting because I use this kind of call style in Perl a lot, where I'd do:
$self->method($param, %options);Which seems to be pretty much the same thing, except Objective-C has it built into the language, and comes with parameter checking.
anArray insert: anObject at: 5
[array insertObject: object at: 5]
Re:It's a direct lift from Smalltalk
Matts on 2002-09-08T19:02:37
I don't understand your final point about variable names. Could you expand on it?
Someone once suggested to me it was nice that Obj-C was nice because it showed you very clearly what parts were method calls, and what parts were just regular C code. I'm not sold on that yet, as I don't really have a problem with that in other languages. But my mind is open on it, and the OS X tools are nice enough that it won't sway me either way.Re:It's a direct lift from Smalltalk
pdcawley on 2002-09-08T20:43:40
Consider a Smalltalk method declarationThe method selector tells you what each argument is for, the parameter name tells you what it is. Actually, good Smalltalk style is to only use type suggesting variable names for method argument names. The method body that I lifted the above from, for instance, continues:OrderedCollection at: anInteger put: anObject| index |
index:= anInteger asInteger. | index |
is Smalltalk formy $index
. Note that there's an assumption in the implementation that the first argument won't necessarily be an integer; the name of the variable simply implies that it will be treated as an integer.
There's a better discussion of Type Suggesting and Role Suggesting variables in Kent Beck's Smalltalk Best Practice Patterns, a book I never seem to stop recommending; even if you don't do Smalltalk, it's full of good stuff for anyone who does OO programming in a dynamic language. It's warped my brain to the extent I keep finding myself writingvariableNamesLikeThis
, which would be okay for private stuff, but runs counter to perlstyle for stuff I might want to release.
http://www.rubygarden.org/ruby?BradCox