D - define operator syntax??
- Charles Hixson (8/8) Oct 01 2003 Are there any examples of how one defines an operator?
- Vathix (10/18) Oct 01 2003 Make a member function named cat, something like this:
- Charles Hixson (8/26) Oct 01 2003 So I would need to create methods something like this:
- jhenzie mac.com (18/26) Oct 01 2003 I believe ~ resolves to cat and car_r
- jhenzie mac.com (2/31) Oct 01 2003
Are there any examples of how one defines an operator? In particular, I want to define the append operator (~) for a class which isn't a descendant of String or Array, though it has an internal data container which is a char[], so the append operator appears to be the correct approach. The manual specifically says that this is possible, but if it says how, then I missed it. And I haven't been able to find any examples so far while looking through Phobos.
Oct 01 2003
"Charles Hixson" <charleshixsn earthlink.net> wrote in message news:blfnbf$2ua6$1 digitaldaemon.com...Are there any examples of how one defines an operator? In particular, I want to define the append operator (~) for a class which isn't a descendant of String or Array, though it has an internal data container which is a char[], so the append operator appears to be the correct approach. The manual specifically says that this is possible, but if it says how, then I missed it. And I haven't been able to find any examples so far while looking through Phobos.Make a member function named cat, something like this: class Foo { char[] bar; this(char[] ibar) { bar = ibar; } Foo cat(Foo append) { return new Foo(this.bar ~ append.bar); } } www.digitalmars.com/d/operatoroverloading.html for the rest.
Oct 01 2003
Vathix wrote:"Charles Hixson" <charleshixsn earthlink.net> wrote in message news:blfnbf$2ua6$1 digitaldaemon.com... ... Make a member function named cat, something like this: class Foo { char[] bar; this(char[] ibar) { bar = ibar; } Foo cat(Foo append) { return new Foo(this.bar ~ append.bar); } } www.digitalmars.com/d/operatoroverloading.html for the rest.So I would need to create methods something like this: phrase cat (phrase str){ return new phrase (data ~ str.toS() );} phrase cat (char[] str){ return new phrase (data ~ str); } phrase cat_r (char[] str){ return new phrase (str ~ data); } (That seems to handle all the obvious cases that I can think of. I want to allow it to be catenated with strings, but the result to be a phrase.)
Oct 01 2003
I believe ~ resolves to cat and car_r So you would need to define methods as follows. class X { char[] myAttribute; ... X cat(X instance) { myAttribute ~= X.myAttribute; return this; } } I think that will work for you although you will need to implement cat_r. Be careful to ensure that concatenation makes sence for your type. I know you will I am just being paranoid, too many years dealing with the community that use the potential. In article <blfnbf$2ua6$1 digitaldaemon.com>, Charles Hixson says...Are there any examples of how one defines an operator? In particular, I want to define the append operator (~) for a class which isn't a descendant of String or Array, though it has an internal data container which is a char[], so the append operator appears to be the correct approach. The manual specifically says that this is possible, but if it says how, then I missed it. And I haven't been able to find any examples so far while looking through Phobos.
Oct 01 2003
My bad, cat should return new instance of X In article <blfolj$3062$1 digitaldaemon.com>, jhenzie mac.com says...I believe ~ resolves to cat and car_r So you would need to define methods as follows. class X { char[] myAttribute; ... X cat(X instance) { myAttribute ~= X.myAttribute; return this; } } I think that will work for you although you will need to implement cat_r. Be careful to ensure that concatenation makes sence for your type. I know you will I am just being paranoid, too many years dealing with the community that use the potential. In article <blfnbf$2ua6$1 digitaldaemon.com>, Charles Hixson says...Are there any examples of how one defines an operator? In particular, I want to define the append operator (~) for a class which isn't a descendant of String or Array, though it has an internal data container which is a char[], so the append operator appears to be the correct approach. The manual specifically says that this is possible, but if it says how, then I missed it. And I haven't been able to find any examples so far while looking through Phobos.
Oct 01 2003