Thread overview
define operator syntax??
Oct 01, 2003
Charles Hixson
Oct 01, 2003
Vathix
Oct 01, 2003
Charles Hixson
Oct 01, 2003
jhenzie
Oct 02, 2003
jhenzie
October 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.

October 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.



October 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 popular language similar to C# but not to be mentioned because of the flame bait 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.
>


October 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.)

October 02, 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 popular language similar to C# but not to be mentioned because of the flame bait 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.
>>
>
>