Discussion:
operator overloading
(too old to reply)
Marko Riedel
2006-03-06 11:27:32 UTC
Permalink
Hi folks,

here is a question for you GNUstep wizards out there. Can anyone suggest
how best to simulate operator overloading, which is missing from Objective
C? I would like to overload operators such as "add" or "multiply." Can I
ask GCC to output its parse tree and then try to add overloading manually?
BTW is there a tutorial on how to load classes at runtime (sorry if this
is a very basic question).

BTW I tremendously enjoyed the FOSDEM 2006 slides that Nicolas Roard
published.

Best regards,

Marko


+-------------------------------------------------------------+
| Marko Riedel, EDV Neue Arbeit gGmbH, ***@yahoo.de |
| http://www.geocities.com/markoriedelde/index.html |
+-------------------------------------------------------------+






___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
Sašo Kiselkov
2006-03-06 21:18:54 UTC
Permalink
Post by Marko Riedel
Hi folks,
here is a question for you GNUstep wizards out there. Can anyone suggest
how best to simulate operator overloading, which is missing from Objective
C? I would like to overload operators such as "add" or "multiply." Can I
ask GCC to output its parse tree and then try to add overloading manually?
I think this question is better asked on GCC mailing lists - we just use GCC, we
don't hack it.
Post by Marko Riedel
BTW is there a tutorial on how to load classes at runtime (sorry if this
is a very basic question).
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSBundle.html
http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFBundles/Tasks/loading.html#//apple_ref/doc/uid/20001127
http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/index.html#//apple_ref/doc/uid/10000051i

--
Saso
Ingolf Jandt
2006-03-06 22:57:50 UTC
Permalink
Post by Marko Riedel
Can anyone suggest
how best to simulate operator overloading, which is missing from Objective
C? I would like to overload operators such as "add" or "multiply." Can I
Operator overloding is "missing" in ObjC as it is strictly speaking not a principle of object orientation, but originates from ADT-based languages like Ada. (So they might be of choice for you.) But if I understand you correctly you can achieve approximately what you want simply by writing methods like -(myObject) add: (myObject *) secondAddend; and so on.

Sorry if I am completely missing the point. Seems likely to me.

Ingolf
__________________________________________________________________________
Erweitern Sie FreeMail zu einem noch leistungsstarkeren E-Mail-Postfach!
Mehr Infos unter http://freemail.web.de/home/landingpad/?mc=021131
Pete French
2006-03-07 00:22:30 UTC
Permalink
Post by Ingolf Jandt
Operator overloding is "missing" in ObjC as it is strictly speaking
not a principle of object orientation, but originates from ADT-based
languages like Ada. (So they might be of choice for you.)
Dont have the original post to reply to, but it occurs to me that the
original posted can simply write in ObjC++ and use C++'s operator
overloading if they really feel they have to do this. Wont that work ?

-pcf.
Jeff Teunissen
2006-03-15 22:57:43 UTC
Permalink
Post by Pete French
Post by Ingolf Jandt
Operator overloding is "missing" in ObjC as it is strictly speaking
not a principle of object orientation, but originates from ADT-based
languages like Ada. (So they might be of choice for you.)
Dont have the original post to reply to, but it occurs to me that the
original posted can simply write in ObjC++ and use C++'s operator
overloading if they really feel they have to do this. Wont that work ?
No, it won't.

Objective-C objects/classes cannot take part of C++ features in ObjC++.
--
| Jeff Teunissen -=- Pres., Dusk To Dawn Computing -=- deek @ d2dc.net
| GPG: 1024D/9840105A 7102 808A 7733 C2F3 097B 161B 9222 DAB8 9840 105A
| Core developer, The QuakeForge Project http://www.quakeforge.net/
| Specializing in Debian GNU/Linux http://www.d2dc.net/~deek/
Sheldon Gill
2006-03-07 01:05:56 UTC
Permalink
Post by Marko Riedel
Hi folks,
here is a question for you GNUstep wizards out there. Can anyone suggest
how best to simulate operator overloading, which is missing from Objective
C? I would like to overload operators such as "add" or "multiply." Can I
ask GCC to output its parse tree and then try to add overloading manually?
I'm not sure what exactly you are trying to achieve or why you want this.

Anyway, Objective-C is O-O rather than functional so operator overloading isn't
needed. Just add the appropriate methods to classes so you might have:

-[EgMatrix add: (EgMatrix *)anotherMatrix];
-[EgComplex add: (EgComplex *)anotherComplex];

If you're thinking C++ operator overloading then you're looking for a method
lookup which considers the type/class of the argument... so...

If you wish to "overload" the add method then you need to change the signatures
slightly:

-[EgMatrix add: (id *)aNumericObject];
-[EgMatrix addMatrix: (EgMatrix *)anotherMatrix];
-[EgMatrix addComplex: (EgComplex *)anotherMatrix];

-[EgComplex add: (id *)aNumericObject];
-[EgComplex addMatrix: (EgMatrix *)anotherMatrix];
-[EgComplex addComplex: (EgComplex *)anotherComplex];

then add introspection like this

- (id)add: (id)aNumericObject
{
if (aNumericObject_isa_EgComplex)
return [self addComplex: aNumericObject]
if (aNumericObject_isa_EgMatrix)
return [self addMatrix: aNumericObject]
}

Considering the similarity of the code, rather than try to get GCC's parse tree
it'd be *much* easier and equally effective to write a template compiler. It
really only needs to be a rather dumb pre-processor for this level of
complexity. You may even get away with using the standard C pre-processor.
Post by Marko Riedel
BTW is there a tutorial on how to load classes at runtime (sorry if this
is a very basic question).
Look at the documentation for NSBundle.


Regards,
Sheldon
Philippe C.D. Robert
2006-03-07 18:48:40 UTC
Permalink
Hi,
Post by Marko Riedel
Hi folks,
here is a question for you GNUstep wizards out there. Can anyone
suggest
how best to simulate operator overloading, which is missing from
Objective
C? I would like to overload operators such as "add" or "multiply."
Can I
ask GCC to output its parse tree and then try to add overloading
manually?
Operator overloading is not supported by ObjC, although it would be
very helpful in some situations, e.g. when writing complex maths
code. Instead of fiddling with GCC internals I would rather use the C
preprocessor, though (even if this is far from elegant...).
Post by Marko Riedel
BTW is there a tutorial on how to load classes at runtime (sorry if
this
is a very basic question).
The NSBundle documentation... or do you mean using the runtime
directly???

-Phil
--
Philippe C.D. Robert
http://www.nice.ch/~phip
Marko Riedel
2006-03-07 20:49:02 UTC
Permalink
Post by Philippe C.D. Robert
Hi,
[...]
Post by Philippe C.D. Robert
Operator overloading is not supported by ObjC, although it would be
very helpful in some situations, e.g. when writing complex maths
code. Instead of fiddling with GCC internals I would rather use the C
preprocessor, though (even if this is far from elegant...).
Can you give an example of using the C preprocessor to achieve infix
operator overloading?

Best regards,

Marko


+-------------------------------------------------------------+
| Marko Riedel, EDV Neue Arbeit gGmbH, ***@yahoo.de |
| http://www.geocities.com/markoriedelde/index.html |
+-------------------------------------------------------------+






___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
Philippe C.D. Robert
2006-03-10 07:27:06 UTC
Permalink
Post by Marko Riedel
[...]
Post by Philippe C.D. Robert
Operator overloading is not supported by ObjC, although it would be
very helpful in some situations, e.g. when writing complex maths
code. Instead of fiddling with GCC internals I would rather use the C
preprocessor, though (even if this is far from elegant...).
Can you give an example of using the C preprocessor to achieve infix
operator overloading?
Nope, sorry, I am afraid I didn't exactly investigate... I just meant
that before I would dig into gcc internals I would try to use the
preprocessor (if and only if using methods is no option at all!).

-Phil
--
Philippe C.D. Robert
http://www.nice.ch/~phip
Pete French
2006-03-16 00:10:36 UTC
Permalink
Post by Jeff Teunissen
Objective-C objects/classes cannot take part of C++ features in ObjC++.
True, but you can wrap one in the other - depends what he's trying to
do. hardly the most elegant solution though I know...

-bat.
Ingolf Jandt
2006-03-16 13:13:36 UTC
Permalink
Post by Jeff Teunissen
Objective-C objects/classes cannot take part of C++ features in ObjC++.
You do can mix the code. The documented limitation just means you can't create ObjC child classes of C++ classes, get selectors for C++ methods and such things.

(It is still not clear to me why not to use a backend in another language for non-OO purposes.)

ingolf
______________________________________________________________
Verschicken Sie romantische, coole und witzige Bilder per SMS!
Jetzt bei WEB.DE FreeMail: http://f.web.de/?mc=021193
Jeff Teunissen
2006-03-16 17:56:23 UTC
Permalink
Post by Ingolf Jandt
Post by Jeff Teunissen
Objective-C objects/classes cannot take part of C++ features in ObjC++.
You do can mix the code. The documented limitation just means you can't
create ObjC child classes of C++ classes, get selectors for C++ methods
and such things.
Of course you can "mix the code", that's the whole point. You can't mix the
_features_. ObjC objects can't be templates, you can't send them to iostreams,
they have to have "C" linkage, you can't throw an exception from C++ and catch
it in ObjC or vice versa, ObjC objects can't interact with operator
overloading, and so on. An Objective-C++ compiler is a normal C++ compiler
with the syntax and runtime additions from Objective-C. It works because all
of the additional syntax is illegal C++ (just as with C). :)
--
| Jeff Teunissen -=- Pres., Dusk To Dawn Computing -=- deek @ d2dc.net
| GPG: 1024D/9840105A 7102 808A 7733 C2F3 097B 161B 9222 DAB8 9840 105A
| Core developer, The QuakeForge Project http://www.quakeforge.net/
| Specializing in Debian GNU/Linux http://www.d2dc.net/~deek/
Loading...