www.digitalmars.com         C & C++   DMDScript  

D - define operator syntax??

reply Charles Hixson <charleshixsn earthlink.net> writes:
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
next sibling parent reply "Vathix" <vathix dprogramming.com> writes:
"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
parent Charles Hixson <charleshixsn earthlink.net> writes:
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
prev sibling parent reply jhenzie mac.com writes:
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
parent jhenzie mac.com writes:
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