views:

122

answers:

5

I know:

C++, Java, and tons others:

object.method() , object.method(arg)  

Objective-C:

[object method] , [object method:arg]

Smalltalk:

object method   , object method: arg

PHP, Perl

object->method(), object->method(arg)
$object->method;
$object->method($arg1, $arg2, ...);

OCaml

object#method  ,  object#method args

CLOS

(method object) ,  (method object arg) 

And even, I have used:

method object
method(object)

Can you name other alternatives ways of sending a message to an object ( I think that would be the correct term ) in different programming languages?

+5  A: 

Refer to

Wikipedia: Comparison of OO Languages -> Method call style

jitter
+1  A: 

Most interesting may be the "generic function" notation - in CLOS (the O-O part of Common Lisp), they're called like (method object arg) (with defgeneric for method as a generic function and defmethod to define its specialization/override); in languages more oriented to algebraic notation, like Dylan and Python (e.g. with PEAK), the calls will be more like method(object, arg) instead.

The coolest thing about the generic/method approach (which Common Lisp pioneered, AFAIK) is that it doesn't have to treat the first object differently from all the others -- method dispatch can happen upon any combination of arguments' runtime types, not just the runtime type of one of the arguments (single-argument dispatching is obviously covered as a special case;-). Say goodbye to Visitor and all other variants (cfr e.g. this paper) which basically try to simulate multiple dispatching in single-dispatch languages!-)

Alex Martelli
+1 I forgot about CLOS ( to be honest, I didn't knew it was OO )
OscarRyz
A: 

Following on from Alex Martelli's comment, multi methods in the JVM language Nice

FiniteStateMachineMultiMethodExample

VisitorPatternMultiMethodExample

and other examples.

igouy
+1  A: 

Just for completeness’ sake:

While VB.NET normally uses the same style as C#, one can also omit the parentheses on methods and on parameterless functions/constructors:

stream.WriteLine "Hello"
Dim x = stream.ReadLine

But the IDE will automatically add the parentheses for parametrized method calls (oddly enough, it won’t do the same for functions and constructors).

Furthermore, VB.NET still knows the (somewhat antique) Call keyword which once again forces parentheses around parametrized method calls:

Call stream.WriteLine("Hello") '// is the same as'
stream.WriteLine("Hello")
'// This won’t compile:'
Call stream.WriteLine "Hello"
Konrad Rudolph
+1  A: 

in Perl, its:

$object->method;
$object->method($arg1, $arg2, ...);

and the potentially dangerous indirect object syntax (which wont work if you have a sub named method in the current scope:

method $object $arg1, $arg2...;
Eric Strom