D - operator overloading
- "Ivan Senji" <ivan.senji public.srce.hr> Jan 25 2004
- J Anderson <REMOVEanderson badmama.com.au> Jan 25 2004
- "Ivan Senji" <ivan.senji public.srce.hr> Jan 25 2004
- Ilya Minkov <minkov cs.tum.edu> Jan 26 2004
1. i like D very much1
2. I only recently got some time to start exploring it and i have a
question:
i have this code:
class Cout
{
Cout opShl(char[] x)
{
printf("%.*s",x);
return this;
}
Cout opShl(int x)
{
printf("%d",x);
return this;
}
...
}
The idea is to make a class cout that would act like c++ cout.
Is there any way to write something like:
Cout opShl(Cout c,A f)
{
//printf that prints object f of type A
return c;
}
it doesn't work the way i would like it, i have to explicitly call the
function ti print type A!
Jan 25 2004
Ivan Senji wrote:1. i like D very much1 2. I only recently got some time to start exploring it and i have a question: i have this code: class Cout { Cout opShl(char[] x) { printf("%.*s",x); return this; } Cout opShl(int x) { printf("%d",x); return this; } ... } The idea is to make a class cout that would act like c++ cout. Is there any way to write something like: Cout opShl(Cout c,A f) { //printf that prints object f of type A return c; } it doesn't work the way i would like it, i have to explicitly call the function ti print type A!
up a few times in the newsgroup. However you can use the bracket notation to do streams ie cout(arg1)(arg2)(arg3) Also you could extend one of the classes (either Cout or A) and add opShl (or opShl_r). Like: class Cout { } class T2 { T2 opShl_r(Cout x) { printf("T2"); return this; } } class T3 : T2 { T3 opShl_r(Cout x) { printf("T3"); return this; } } int main ( char [] [] args ) { Cout test = new Cout; T2 test2 = new T3; test << test2; } But that still won't be as flexible as free functions. IMHO It comes down to some problems with using ADL that Walter doesn't like (*there's gotta be a better way*) Also, Walter seems to believe that operators are mainly good for mathematical operations (or specific behaviours) because otherwise you don't know how the operator is going to be used/behave. Moreover if you know how that operator is going to behave, then you have a better chance at optimising it. That is to say, the shift operator is for shifting. So I guess the default behaviour of the bracket notation will be for streams. -- -Anderson: http://badmama.com.au/~anderson/
Jan 25 2004
when i have an empty Cout class everything works:
class Cout
{
}
class T2
{
T2 opShl_r(Cout x)
{
printf("T2");
return this;
}
}
//in main:
T2 t = new T2();
Cout cout = new Cout();
cout << t;
Compiler realizes that the operator opShl_r is defined in class T2 and not
in class Cout
and the example works!
But if i add some operators to Cout like:
class Cout
{
Cout opShl(char[] x)
{
printf("%.*s",x);
return this;
}
Cout opShl(int x)
{
printf("%d",x);
return this;
}
}
the example above: "cout << t;" doesnt work any more and i get an compiler
error message:
E:\D language\proba2\proba2.d(106): function opShl (char[]x) does not match
argument types (T2 )
i think that the compiler is looking for a cout.opShl(t) function and it
doesnt find it
but shouldnt it still find the function t.opShl_r(cout); and use it???
Why does the opShl(char[]x) function in Cout class confuse him?
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:bv19j8$hqq$1 digitaldaemon.com...
Ivan Senji wrote:
1. i like D very much1
2. I only recently got some time to start exploring it and i have a
question:
i have this code:
class Cout
{
Cout opShl(char[] x)
{
printf("%.*s",x);
return this;
}
Cout opShl(int x)
{
printf("%d",x);
return this;
}
...
}
The idea is to make a class cout that would act like c++ cout.
Is there any way to write something like:
Cout opShl(Cout c,A f)
{
//printf that prints object f of type A
return c;
}
it doesn't work the way i would like it, i have to explicitly call the
function ti print type A!
up a few times in the newsgroup.
However you can use the bracket notation to do streams ie
cout(arg1)(arg2)(arg3)
Also you could extend one of the classes (either Cout or A) and add opShl
class Cout
{
}
class T2
{
T2 opShl_r(Cout x)
{
printf("T2");
return this;
}
}
class T3 : T2
{
T3 opShl_r(Cout x)
{
printf("T3");
return this;
}
}
int main ( char [] [] args )
{
Cout test = new Cout;
T2 test2 = new T3;
test << test2;
}
But that still won't be as flexible as free functions.
IMHO It comes down to some problems with using ADL that Walter doesn't
like (*there's gotta be a better way*) Also, Walter seems to believe
that operators are mainly good for mathematical operations (or specific
behaviours) because otherwise you don't know how the operator is going
to be used/behave. Moreover if you know how that operator is going to
behave, then you have a better chance at optimising it. That is to say,
the shift operator is for shifting. So I guess the default behaviour of
the bracket notation will be for streams.
--
-Anderson: http://badmama.com.au/~anderson/
Jan 25 2004
This is a serious problem, since i intended to do stream classes based on a ~ operator, which also has a reversed kind. Besides, IIRC reversed operator oveloads were only introduced to be able to augment a library with overloaded operators by your own types. -eye Ivan Senji wrote:when i have an empty Cout class everything works: class Cout { } class T2 { T2 opShl_r(Cout x) { printf("T2"); return this; } } //in main: T2 t = new T2(); Cout cout = new Cout(); cout << t; Compiler realizes that the operator opShl_r is defined in class T2 and not in class Cout and the example works! But if i add some operators to Cout like: class Cout { Cout opShl(char[] x) { printf("%.*s",x); return this; } Cout opShl(int x) { printf("%d",x); return this; } } the example above: "cout << t;" doesnt work any more and i get an compiler error message: E:\D language\proba2\proba2.d(106): function opShl (char[]x) does not match argument types (T2 ) i think that the compiler is looking for a cout.opShl(t) function and it doesnt find it but shouldnt it still find the function t.opShl_r(cout); and use it??? Why does the opShl(char[]x) function in Cout class confuse him? "J Anderson" <REMOVEanderson badmama.com.au> wrote in message news:bv19j8$hqq$1 digitaldaemon.com...Ivan Senji wrote:1. i like D very much1 2. I only recently got some time to start exploring it and i have a question: i have this code: class Cout { Cout opShl(char[] x) { printf("%.*s",x); return this; } Cout opShl(int x) { printf("%d",x); return this; } ... } The idea is to make a class cout that would act like c++ cout. Is there any way to write something like: Cout opShl(Cout c,A f) { //printf that prints object f of type A return c; } it doesn't work the way i would like it, i have to explicitly call the function ti print type A!
There aren't free floating operators in D. This issue has been brought up a few times in the newsgroup. However you can use the bracket notation to do streams ie cout(arg1)(arg2)(arg3) Also you could extend one of the classes (either Cout or A) and add opShl
(or opShl_r). Like:class Cout { } class T2 { T2 opShl_r(Cout x) { printf("T2"); return this; } } class T3 : T2 { T3 opShl_r(Cout x) { printf("T3"); return this; } } int main ( char [] [] args ) { Cout test = new Cout; T2 test2 = new T3; test << test2; } But that still won't be as flexible as free functions. IMHO It comes down to some problems with using ADL that Walter doesn't like (*there's gotta be a better way*) Also, Walter seems to believe that operators are mainly good for mathematical operations (or specific behaviours) because otherwise you don't know how the operator is going to be used/behave. Moreover if you know how that operator is going to behave, then you have a better chance at optimising it. That is to say, the shift operator is for shifting. So I guess the default behaviour of the bracket notation will be for streams. -- -Anderson: http://badmama.com.au/~anderson/
Jan 26 2004








Ilya Minkov <minkov cs.tum.edu>