PDA

View Full Version : Calling a method when arithmetic operators are used on objects...




MorphingDragon
Sep 13, 2010, 06:57 AM
Calling a method when arithmetic operators are used on objects, Is this possible in Objective-C bar some sort of C macro.

EG:

myFraction = aFraction * anotherFraction;

//Is the same as

myFraction = [aFraction multiplyBy: anotherFraction];

It seems such a waste of time and readability to call such methods on such objects when they only represent numbers.



Sykte
Sep 13, 2010, 07:18 AM
Calling a method when arithmetic operators are used on objects, Is this possible in Objective-C bar some sort of C macro.

EG:

myFraction = aFraction * anotherFraction;

//Is the same as

myFraction = [aFraction multiplyBy: anotherFraction];

It seems such a waste of time and readability to call such methods on such objects when they only represent numbers.


If you've created the fraction class than it's as simple as adding a new method. If not than you will need to either subclass the fraction class or use categories.

I would recommend reading on both subclassing and categories.

MorphingDragon
Sep 13, 2010, 07:23 AM
No I mean so you can just use * / + - on objects instead of multiplyBy: divideBy: methods etc.

lee1210
Sep 13, 2010, 07:26 AM
This is called operator overloading, and Objective-C does not support this feature. I appreciate this because you know exactly what's going on at a glance. There are good arguments for overloading, but for now Objective-C doesn't have it, so message passing is where it's at. With code completion it's not much more typing, and I think it's still quite readable.

-Lee

foidulus
Sep 13, 2010, 07:49 AM
This is called operator overloading, and Objective-C does not support this feature. I appreciate this because you know exactly what's going on at a glance. There are good arguments for overloading, but for now Objective-C doesn't have it, so message passing is where it's at. With code completion it's not much more typing, and I think it's still quite readable.

-Lee

Though I wish they would make an exception for strings(which would be sacrelig, I know!), doing even basic string manipulation can get ugly quick and looks bad to boot.

MorphingDragon
Sep 13, 2010, 08:28 AM
This is called operator overloading, and Objective-C does not support this feature. I appreciate this because you know exactly what's going on at a glance. There are good arguments for overloading, but for now Objective-C doesn't have it, so message passing is where it's at. With code completion it's not much more typing, and I think it's still quite readable.

-Lee

I would only really use it so the code is more like natural arithmetic.

I had a look at some of the C# and C++ implementation and gotchas... they look painful.

Sykte
Sep 13, 2010, 09:19 AM
No I mean so you can just use * / + - on objects instead of multiplyBy: divideBy: methods etc.


Oops my fault, I didn't read the entire post. I'm going to blame it on reading the post at 8:00am on my IPad.

lee1210
Sep 13, 2010, 09:29 AM
I would only really use it so the code is more like natural arithmetic.

I had a look at some of the C# and C++ implementation and gotchas... they look painful.

I know you're going through a book right now, but in a "real world" scenario where you had wrappers that you wanted to do arithmetic with it would be best to have some manner to link operations so you don't have a lot of objects being created as intermediate values of the calculation just to be thrown away. Just something to keep in mind for performance' sake.

-Lee

Sander
Sep 13, 2010, 10:30 AM
I simply can't bring myself to stay out of a thread about operator overloading. I'll just sit back and smile though, and not ruin anybody's mood.

:)