[comp.lang.objective-c] Operator overloading

murphy@photon.mpr.ca (Gail Murphy) (12/14/90)

Does Objective-C support operator overloading?

Gail Murphy                     | Voice : (604) 293-5462
MPR Teltech Ltd. 		| Fax   : (604) 293-5787
8999 Nelson Way, Burnaby, BC    | E-Mail: murphy@mprgate.mpr.ca
Canada, V5A 4B5                 |         mprgate.mpr.ca!murphy@uunet.uu.net

lerman@stpstn.UUCP (Ken Lerman) (12/19/90)

In article <1990Dec14.155211.6598@mprgate.mpr.ca> murphy@photon.mpr.ca writes:
->Does Objective-C support operator overloading?
->
->Gail Murphy                     | Voice : (604) 293-5462
->MPR Teltech Ltd. 		| Fax   : (604) 293-5787
->8999 Nelson Way, Burnaby, BC    | E-Mail: murphy@mprgate.mpr.ca
->Canada, V5A 4B5                 |         mprgate.mpr.ca!murphy@uunet.uu.net


Objective-C supports method overloading, but not operator (+, -,...)
overloading.

Ken

zack@bosslog.UUCP (Zacharias Beckman) (12/22/90)

In article <1990Dec14.155211.6598@mprgate.mpr.ca> murphy@photon.mpr.ca writes:
>Does Objective-C support operator overloading?
>
>Gail Murphy                     | Voice : (604) 293-5462
>MPR Teltech Ltd. 		 | Fax   : (604) 293-5787
>8999 Nelson Way, Burnaby, BC    | E-Mail: murphy@mprgate.mpr.ca
>Canada, V5A 4B5                 |         mprgate.mpr.ca!murphy@uunet.uu.net

Nope; Objective-C is not capable of overloading per the definition of the word
if you are coming from C++.  I.e.:

  Overloading allows multiple functions with the same name to be defined
  provided their argument lists differ sufficiently for calls to be resolved.
  (ARM pp. 307).

In other words, you can't overload a function, method or operator in
Objective-C.  It's unfortunate -- I really miss this capability, particularly
in method overloading.

-- 
Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
Lower Code Dweller    |  Courage to change those I can,
Boss Logic Inc.       |  and Wisdom to know the difference.
uunet!bosslog!zack    |  --- Sinead O'Conner.

zack@bosslog.UUCP (Zacharias Beckman) (12/22/90)

In article <5928@stpstn.UUCP> lerman@stpstn.UUCP (Ken Lerman) writes:
>In article <1990Dec14.155211.6598@mprgate.mpr.ca> murphy@photon.mpr.ca writes:
>->Does Objective-C support operator overloading?
>
>Objective-C supports method overloading, but not operator (+, -,...)
>overloading.

Eh?  Not unless your using a better Objective-C compiler than I am!  I hope
you are -- then I can upgrade!  Overloading is not the same as overriding;
which is to say, the same method name in the same object with a different
parameter list is overloading.  Objective-C doesn't support that for methods,
functions or operators.  If I'm wrong, please let me know!

-- 
Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
Lower Code Dweller    |  Courage to change those I can,
Boss Logic Inc.       |  and Wisdom to know the difference.
uunet!bosslog!zack    |  --- Sinead O'Conner.

lerman@stpstn.UUCP (Ken Lerman) (12/28/90)

In article <191@bosslog.UUCP> zack@bosslog.UUCP (Zacharias Beckman) writes:
->In article <1990Dec14.155211.6598@mprgate.mpr.ca> murphy@photon.mpr.ca writes:
->>Does Objective-C support operator overloading?
->>
->>Gail Murphy                     | Voice : (604) 293-5462
->>MPR Teltech Ltd. 		 | Fax   : (604) 293-5787
->>8999 Nelson Way, Burnaby, BC    | E-Mail: murphy@mprgate.mpr.ca
->>Canada, V5A 4B5                 |         mprgate.mpr.ca!murphy@uunet.uu.net
->
->Nope; Objective-C is not capable of overloading per the definition of the word
->if you are coming from C++.  I.e.:
->
->  Overloading allows multiple functions with the same name to be defined
->  provided their argument lists differ sufficiently for calls to be resolved.
->  (ARM pp. 307).
->
->In other words, you can't overload a function, method or operator in
->Objective-C.  It's unfortunate -- I really miss this capability, particularly
->in method overloading.
->
->-- 
->Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
->Lower Code Dweller    |  Courage to change those I can,
->Boss Logic Inc.       |  and Wisdom to know the difference.
->uunet!bosslog!zack    |  --- Sinead O'Conner.

IMHO the polymorphism available through Objective-C qualifies as
operator overloading.

More specifically, two classes may have methods with the same name
(actually, with the same selector).  The user of the method may be
oblivious to the fact that two such functions exist, and the proper
function will be automatically called based on the receiver.

In Objective-C, the proper method is executed based on the class of
the receiver, rather than the type of the arguments, but from a
functional point of view, the receiver is one of the arguments of the
method.

IMHO the quotation from the ARM is a description of what C++ provides
rather than a definition of operator overloading.

I'd be interested in hearing more details of what method overloading
functionality is missing which lead Zacharias to write:  "I really miss
this capability, particularly in method overloading."

As an old time Objective-C programmer, I might be able to offer some
suggestions.

As always, the preceding is my opinion, not necessarily that of
Stepstone, and may the force be with you.

Ken

lerman@stpstn.UUCP (Ken Lerman) (12/28/90)

In article <192@bosslog.UUCP> zack@bosslog.UUCP (Zacharias Beckman) writes:
->In article <5928@stpstn.UUCP> lerman@stpstn.UUCP (Ken Lerman) writes:
->>In article <1990Dec14.155211.6598@mprgate.mpr.ca> murphy@photon.mpr.ca writes:
->>->Does Objective-C support operator overloading?
->>
->>Objective-C supports method overloading, but not operator (+, -,...)
->>overloading.
->
->Eh?  Not unless your using a better Objective-C compiler than I am!  I hope
->you are -- then I can upgrade!  Overloading is not the same as overriding;
->which is to say, the same method name in the same object with a different
->parameter list is overloading.  Objective-C doesn't support that for methods,
->functions or operators.  If I'm wrong, please let me know!
->
->-- 
->Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
->Lower Code Dweller    |  Courage to change those I can,
->Boss Logic Inc.       |  and Wisdom to know the difference.
->uunet!bosslog!zack    |  --- Sinead O'Conner.

If I had read this response prior to the previous one I just responded
too, my response would have been different.

It appears that we differ in our definitions of what method
overloading is.  At this point, I hope we understand the differing
definitions and will drop discussion of which is "right".

Objective-C permits:

Class A:
- foo:aFoo

Class B:
- foo:aFoo or
- foo:aBar

In this case, the implementation is chosen dynamically by whether the
receiver is an A or a B.  I would call this method overloading; I
believe Zacharias would not.

Objective-C also permits:

Class A:
- foo:(int)a

Class B:
- foo:(double)b

In this case, the implementation is chosen by whether the receiver is
an A or a B.  I would call this method overloading.  I don't know
whether Zack (pardon the familiarity) would.  If the wrong argument
type were supplied, Objective-C would generate an error.

To do this, though, the Objective-C compiler would have to know
whether the receiver is an A or a B.  You can do this in your code by
writing your declaration as: "A anObject;" or "B anObject;" as
appropriate.

As far as having a better Objective-C compiler than Zack has, I should
hope so.  The one I'm using hasn't been released yet.  :-) (This
functionality is in the language specification beginning with 4.0)

If Zack will send me email, or phone me at (203)426-1875, I can find
out about getting him an upgrade if necessary.

I write this as an exercise in freedom of speech without permission
from Stepstone.  May the force be with me.

Ken

zack@bosslog.UUCP (Zacharias Beckman) (01/02/91)

In article <5952@stpstn.UUCP> lerman@stpstn.UUCP (Ken Lerman) writes:

>IMHO the polymorphism available through Objective-C qualifies as
>operator overloading.

Polymorphism is, at least in my vocabulary, different from operator
overloading.  In overloading, a single object's method (or a function) has
more than one invocation.  Operator overloading specifies that operators,
such as +, -, >, <, =, etc., can be overloaded to provide a functionality
that the programmer supplies.

This is not polymorphism.  Polymorphism is available in both C++ and
Objective-C.  My personal favorite is C++, as I do not agree with the very
loose typing of Objective-C.  I also feel that C++ is more powerful,
offering a few features which are key to me:  multiple inheritance,
overloading, operator overloading, public/private/protected keywords, and
others.  I am also looking forward with great expectations to templates and
exception handling.

>I'd be interested in hearing more details of what method overloading
>functionality is missing which lead Zacharias to write:  "I really miss
>this capability, particularly in method overloading."

As an example, here is a very simple class to show the overloading available
in C++ (keep in mind the definition above).

  class Point : public Shape {
      uint x;
      uint y;
      uint z;
    public:
      void set(int a = 0, int b = 0, int c = 0) { x = a; y = b; z = c; }
      void set(Singularity & s, int i = 0) { set(s.x(), s.y(), i); }
      void set(Point & p) { set(p.x(), p.y(), p.z()); }

      Point & operator=(Point & p) { if (p != this) set(p); }

      uint x() { return x; }
      uint y() { return y; }
      uint z() { return z; }
  };

You might want to actually have this class descend from a Singularity class
which would represent a 2-d point.  But, to keep things simple, I didn't do
that.  Obviously, the class doesn't do much.  However, it does allow the
following:

  Point myPoint;                  // declare a couple of Point objects
  Point anotherPoint;
  Singularity single;             // a singularity (a 2-d point)

  anotherPoint.set(50, 50, 50);   // set anotherPoint to [50,50,50]
  single.set(80, 80);             // presumably set singularity to [80,80]

  myPoint.set(anotherPoint);      // set myPoint to [50,50,50]
  myPoint.set();                  // set myPoint to [0,0,0]
  myPoint.set(50, 100);           // set myPoint to [50,100,0]
  myPoint.set(50, 100, 5);        // set myPoint to [50,100,5]
  myPoint.set(single);            // set myPoint to [80,80]
  myPoint.set(single, 5);         // set myPoint to [80,80,5]
  myPoint = anotherPoint;         // set myPoint back to [50,50,50]

My point (pun partially intended) is that it is a tool which makes my life
as a programmer a bit easier.  This example also shows default arguments in
action, which is also "missing" from Objective-C.  What it comes down to is
having an object Thing with the method set (in C++) or an object Thing with
the methods setInt, setFloat, setOther, etc. (in Objective-C).  C++ allows
us to be truly "object-oriented" -- we send a set method, and don't worry
about what the argument is.
-- 
Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
Lower Code Dweller    |  Courage to change those I can,
Boss Logic Inc.       |  and Wisdom to know the difference.
zack@bosslogic.com    |  --- Sinead O'Conner.

cox@stpstn.UUCP (Brad Cox) (01/02/91)

In article <193@bosslog.UUCP> zack@bosslog.UUCP (Zacharias Beckman) writes:
>Polymorphism is available in both C++ and
>Objective-C.  My personal favorite is C++, as I do not agree with the very
>loose typing of Objective-C.

This is not to start a flame war, just to point out that this is like 
saying 'Variables are supported in both C and shell. My personal
favorite is C, as I do not agree with the very loose typing of that shell
provides for connecting filters through pipes.

There's a figure in my 'Planning the Software Industrial Revolution'
article (November 1990 IEEE Software magazine) that proposes a vocabulary 
for discussing tools at diverse software architectural levels, with 
	pipes/filters as a 'rack-level' modularity/binding technology, 
	Fabrik/Metaphor lightweight processes as a 'card-level' technology, 
	Smalltalk/Objective-C as a chip-level technology 
	Ada, C, and C++ as gate- and block-level technologies.
	
Unfortunately, the published figure contains a serious error in Objective-C's 
favor at the card-level integration level, that they've agreed to fix in 
the next issue.

Just as tightly-coupled gate- and block-level technologies have a place
in hardware engineering, but do not compete with loosely-coupled chip-,
card-, and rack-level technologies, the same is true in software.  Of
course, without a mature vocabulary for making such distinctions, we
waste energy with language wars.

I have always believed that the Objective-C versus C++ debates have been
driven more by misunderstanding than by appreciation for their differences.
I've never viewed them as competitors, any more than shell and C are
competitors. As a better C, C++ provides a better substrate on which to 
base new incarnations of Objective-C.
-- 

Brad Cox; cox@stepstone.com; CI$ 71230,647; 203 426 1875
The Stepstone Corporation; 75 Glen Road; Sandy Hook CT 06482

glang@Autodesk.COM (Gary Lang) (01/06/91)

>IMHO the polymorphism available through Objective-C qualifies as
>operator overloading.

Oh come on. I'm as big a fan of OC as the next guy, but the fact that
I can't build my own floating point package using my own function
calls and then overload math operators so that the code looks like all
the other math code in the world means that you really don't have
_operator_ overloading (BTW this is an essential feature for some work
I have in mind for a new platform that's coming out that you might
want to support soon - email me for details).

I agree with you; I don't miss the method overloading in C++ because of
the way OC works. But don't overstate your case...you cannot overload
the basic operator tokens of your language.

On the other hand, OC on the NeXT under 2.0 is supposed to support C++
so presumably you can do this there (I haven't tried it yet because
everything I'd meant to overload is done through method overloading as
you describe, and I don't need it)...

- g
-- 
Gary T. Lang  (415)332-2344 x2702  
Autodesk, Inc.
Sausalito, CA.
MCI: 370-0730

glang@Autodesk.COM (Gary Lang) (01/06/91)

>I've never viewed them as competitors, any more than shell and C are

Well said.

- g

-- 
Gary T. Lang  (415)332-2344 x2702  
Autodesk, Inc.
Sausalito, CA.
MCI: 370-0730