invoking a method



Looking at examples supplied with MF Net Express reminded me of something that seems odd to me about all OO languages I've looked in to. They pretty much all invoke methods in a somewhat similar format in that the object comes first, followed by the name of the method, followed by any parameters.

COBOL 2002
invoke object "method" using by value parm1, parm2
MF COBOL
invoke object::"method"(parm1, parm2)
Java
object.method(parm1, parm2)
Objective-C
[object method:parm1 parm2name:parm2]
Ruby
object.method parm1, parm2

One thing that I've come to believe is that it would often be more "natural" (for me) if the method name was specified first. The method is very often a verb. So why not something like:
insert employee into list

where 'insert' is the method name, 'employee' is the object to be inserted (the parameter) and 'list' is the object that is to receive the message. 'into' is just a noise word to make it more readable.

What follows below is code from one of the MF ISO2002 examples, along with modified versions in other languages. The last "language" is just something I've been toying with. Right now it's a 2000+ line text document of my thoughts on an 'ideal' (hah!) programming language. Probably that's all it will ever be. But still, it keeps me off the streets.

COBOL 2002:
working-storage section.
01 displayList usage object reference DLinkList.
procedure division.
set displayList to DLinkList::"new"
invoke displayList "add" using by value emp1
invoke displayList "add" using by value car1
invoke displayList "add" using by value indPlace1
invoke displayList "add" using by value car2
move listSize of displayList to noItems
if noItems > 0
invoke displayList "getfirst" returning displayItem
invoke displayItem "display"
perform until exit
set displayItem to displayList::"getnext"
if displayItem not = null
invoke displayItem "display"
else
exit perform
end-if
end-perform
end-if
exit program.

Micro Focus COBOL:
working-storage section.
01 displayList DLinkList.
procedure division.
set displayList to DLinkList::"new"
invoke displayList::"add"(emp1)
invoke displayList::"add"(car1)
invoke displayList::"add"(indPlace1)
invoke displayList::"add"(car2)
move displayList::"listSize" to noItems
if noItems > 0
set displayItem to displayList::"getfirst"
invoke displayItem::"display"
perform until exit
set displayItem to displayList::"getnext"
if displayItem not = null
invoke displayItem::"display"
else
exit perform
end-if
end-perform
end-if
exit program.

Java:
{
DLinkList displayList;

displayList = new DLinkList();
displayList.add(emp1);
displayList.add(car1);
displayList.add(indPlace1);
displayList.add(car2);
noItems = displayList.listSize();
if (noItems > 0) {
displayItem = displayList.getfirst();
displayItem.display();
while () {
displayItem = displayList.getnext();
if (displayItem != NULL)
displayItem.display();
else
break;
}
}
}

Objective-C:
{
DLinkList *displayList;

displayList = [[DLinkList alloc] init];
[displayList add:emp1];
[displayList add:car1];
[displayList add:indPlace1];
[displayList add:car2];
noItems = [displayList listSize];
if (noItems > 0) {
displayItem = [displayList getfirst];
[displayItem display];
while () {
displayItem = [displayList getnext];
if (displayItem != NULL)
[displayItem display];
else
break;
}
}
}

Ruby:
begin
displayList = DLinkList.new
displayList.add emp1
displayList.add car1
displayList.add indPlace1
displayList.add car2
noItems = displayList.listSize
if noItems > 0
displayItem = displayList.getFirst
displayItem.display
while true
displayItem = displayList.getnext
if displayItem
displayItem.display
else
break
end
end
end
end

"Frank's programming language":
begin
displayList := new DLinkList$
[add emp1 to displayList]
[add car1 to displayList]
[add indPlace1 to displayList]
[add car2 to displayList]
noItems := displayList.listSize #(listSize is a property)#
test noItems
when gt 0
displayItem := [getfirst displayList]
[display displayItem];
loop until exit
displayItem = [getnext displayList]
test displayItem
when true
[display displayItem]
when false
exit
end
end
end
end

Like Objective-C, I've wrapped brackets around the object invocation. To me it makes it a bit more readable, plus it would probably making source code parsing simpler. If the method is simply a "property" (or "attribute", depending on your language preference) it would be:
object.property
I suppose you could also have
[property of object]
but I prefer the former. Plus, while I can see
object1.property := object2.property
I can't really picture:
[property of object] := [property of object]
Well, I can, but again I prefer the former.
A regular (non-OO) "function call" would probably just be:
func(parm1, parm2)

Comments and constructive criticism welcomed. Ignoring my nonsense is perfectly fine. Comments like "don't you have better things to do with your time then create yet another programming language" will be ignored. I have no illusions of this "language" going anywhere beyond this text document. :-)

Frank
.



Relevant Pages

  • Re: invoking a method
    ... document of my thoughts on an 'ideal' programming language. ... invoke displayList "getfirst" returning displayItem ... invoke displayItem "display" ... perform until exit ...
    (comp.lang.cobol)
  • Re: invoking a method
    ... move listSize of displayList to noItems ... invoke displayList "getfirst" returning displayItem ... invoke displayItem "display" ... perform until exit ...
    (comp.lang.cobol)
  • Re: invoking a method
    ... something that seems odd to me about all OO languages I've looked in to. ... invoke object "method" using by value parm1, ... invoke displayList "getfirst" returning displayItem ... perform until exit ...
    (comp.lang.cobol)