www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opCall example

reply "Saaa" <empty needmail.com> writes:
What does different exactly mean in this example?
I read that s is of type S thus should be calling opCall (S v), but it 
doesn't.

If opCall is overridden for the struct, and the struct is initialized with a 
value that is of a different type, then the opCall operator is called:

struct S{   int a;

    static S opCall(int v)
    {	S s;
	s.a = v;
	return s;
    }

    static S opCall(S v)
    {	S s;
	s.a = v.a + 1;
	return s;
    }
}

S s = 3;	// sets s.a to 3
S t = s;	// sets t.a to 3, S.opCall(s) is not called
Jan 29 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Saaa escribió:
 What does different exactly mean in this example?
 I read that s is of type S thus should be calling opCall (S v), but it 
 doesn't.
 
 If opCall is overridden for the struct, and the struct is initialized with a 
 value that is of a different type, then the opCall operator is called:
 
 struct S{   int a;
 
     static S opCall(int v)
     {	S s;
 	s.a = v;
 	return s;
     }
 
     static S opCall(S v)
     {	S s;
 	s.a = v.a + 1;
 	return s;
     }
 }
 
 S s = 3;	// sets s.a to 3
 S t = s;	// sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
Jan 30 2008
parent reply Extrawurst <spam extrawurst.org> writes:
Ary Borenszweig schrieb:
 Saaa escribió:
 What does different exactly mean in this example?
 I read that s is of type S thus should be calling opCall (S v), but 
 it doesn't.

 If opCall is overridden for the struct, and the struct is initialized 
 with a value that is of a different type, then the opCall operator is 
 called:

 struct S{   int a;

     static S opCall(int v)
     {    S s;
     s.a = v;
     return s;
     }

     static S opCall(S v)
     {    S s;
     s.a = v.a + 1;
     return s;
     }
 }

 S s = 3;    // sets s.a to 3
 S t = s;    // sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
the first statement works cause he created an opCall for S to be called with just an int. the second statement doesn't call S opCall(S) cause a struct assign expression just performs a memory copy when S t = s; to have the second opCall called u would have to write what Ary suggested: S t = S(s); ~Extrawurst
Jan 30 2008
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Extrawurst wrote:
 
 
 Ary Borenszweig schrieb:
 Saaa escribió:
 S s = 3;    // sets s.a to 3
 S t = s;    // sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
the first statement works cause he created an opCall for S to be called with just an int. the second statement doesn't call S opCall(S) cause a struct assign expression just performs a memory copy when S t = s; to have the second opCall called u would have to write what Ary suggested: S t = S(s); ~Extrawurst
Sorry, I didn't know that. I wanted to see where opCall is explained in the digitalmars page... but each time I want to search a concept, if I use search box, it gives me only results of the newsgroups archives. Otherwise, I have to guess where in the left navigation bar I have to click. Isn't there an index of keywords or important concepts?
Jan 30 2008
parent reply Extrawurst <spam extrawurst.org> writes:
see http://www.digitalmars.com/d/2.0/struct.html under "Dynamic 
Initialization of Structs"



Ary Borenszweig schrieb:
 Extrawurst wrote:
 Ary Borenszweig schrieb:
 Saaa escribió:
 S s = 3;    // sets s.a to 3
 S t = s;    // sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
the first statement works cause he created an opCall for S to be called with just an int. the second statement doesn't call S opCall(S) cause a struct assign expression just performs a memory copy when S t = s; to have the second opCall called u would have to write what Ary suggested: S t = S(s); ~Extrawurst
Sorry, I didn't know that. I wanted to see where opCall is explained in the digitalmars page... but each time I want to search a concept, if I use search box, it gives me only results of the newsgroups archives. Otherwise, I have to guess where in the left navigation bar I have to click. Isn't there an index of keywords or important concepts?
Jan 30 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
I've already found it. But, for example, you can use opCall with 
classes. Why opCall isn't also explained in "Classes"?

Another example: I want to see how to implement opApply correctly. I 
must enter "Statements", go to the "foreach" section, and there it is. I 
know this because I tried several links before I found it. Is there an 
index of keywords and concepts? It would be much simple to find 
something with it.

Extrawurst wrote:
 see http://www.digitalmars.com/d/2.0/struct.html under "Dynamic 
 Initialization of Structs"
 
 
 
 Ary Borenszweig schrieb:
 Extrawurst wrote:
 Ary Borenszweig schrieb:
 Saaa escribió:
 S s = 3;    // sets s.a to 3
 S t = s;    // sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
the first statement works cause he created an opCall for S to be called with just an int. the second statement doesn't call S opCall(S) cause a struct assign expression just performs a memory copy when S t = s; to have the second opCall called u would have to write what Ary suggested: S t = S(s); ~Extrawurst
Sorry, I didn't know that. I wanted to see where opCall is explained in the digitalmars page... but each time I want to search a concept, if I use search box, it gives me only results of the newsgroups archives. Otherwise, I have to guess where in the left navigation bar I have to click. Isn't there an index of keywords or important concepts?
Jan 30 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ary Borenszweig" <ary esperanto.org.ar> wrote in message 
news:fnpr6g$4sb$1 digitalmars.com...
 I've already found it. But, for example, you can use opCall with classes. 
 Why opCall isn't also explained in "Classes"?
Because it's only with structs does "static opCall" have any special meaning. They are structs' version of constructors. With classes, "static opCall" isn't anything special, it just lets you call the class type as if it were a function. The compiler doesn't ever use it.
 Another example: I want to see how to implement opApply correctly. I must 
 enter "Statements", go to the "foreach" section, and there it is. I know 
 this because I tried several links before I found it. Is there an index of 
 keywords and concepts? It would be much simple to find something with it.
Try the wiki? Click the comments button at the top-right. In fact, on the "operator overloading" page the very first thing in the wiki is "why isn't opApply listed and where is it documented?" ;) I know, not exactly what you were looking for, but the wiki often provides extra, more intelligent links between sections of the docs.
Jan 30 2008
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Jarrett Billingsley wrote:
 "Ary Borenszweig" <ary esperanto.org.ar> wrote in message 
 news:fnpr6g$4sb$1 digitalmars.com...
 I've already found it. But, for example, you can use opCall with classes. 
 Why opCall isn't also explained in "Classes"?
Because it's only with structs does "static opCall" have any special meaning. They are structs' version of constructors. With classes, "static opCall" isn't anything special, it just lets you call the class type as if it were a function.
So it is used by the compiler. :-) The compiler doesn't ever use it.
 
 Another example: I want to see how to implement opApply correctly. I must 
 enter "Statements", go to the "foreach" section, and there it is. I know 
 this because I tried several links before I found it. Is there an index of 
 keywords and concepts? It would be much simple to find something with it.
Try the wiki? Click the comments button at the top-right. In fact, on the "operator overloading" page the very first thing in the wiki is "why isn't opApply listed and where is it documented?" ;) I know, not exactly what you were looking for, but the wiki often provides extra, more intelligent links between sections of the docs.
Thanks, I'll use that if I think something is in a page but it's not.
Jan 30 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ary Borenszweig" <ary esperanto.org.ar> wrote in message 
news:fnq28g$mrh$1 digitalmars.com...

 So it is used by the compiler. :-)
Yes :P but not in the way that it is for structs.
Jan 30 2008
prev sibling parent reply "Saaa" <empty needmail.com> writes:
 S s = 3;    // sets s.a to 3
 S t = s;    // sets t.a to 3, S.opCall(s) is not called
opCall is used like this: S s = S(3); S t = S(s); I don't know why your first statement works. I think it's because S is just a wrapper for an int, so an int can be implicitly casted to S.
the first statement works cause he created an opCall for S to be called with just an int. the second statement doesn't call S opCall(S) cause a struct assign expression just performs a memory copy when S t = s; to have the second opCall called u would have to write what Ary suggested: S t = S(s);
Should I see this as the memory copy having a preference above the opCall(S) or, calling opCall(int) like S s=3 as being a special case? Because syntactically there isn't any difference: 3 is of type int thus invoking opCall(int) s is of type S thus invoking ... opCall(S) :)
Jan 31 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Saaa" <empty needmail.com> wrote in message 
news:fnscv9$15aa$1 digitalmars.com...
 Should I see this as the memory copy having a preference above the 
 opCall(S)
 or, calling opCall(int) like S s=3 as being a special case?
Yes and yes. In other words -- you can overload construction, but you can't overload copying. Something planned for D2 is to allow overloading copying as well.
Jan 31 2008
parent "Saaa" <empty needmail.com> writes:
I love being right :D
Thanks.

 Yes and yes.  In other words -- you can overload construction, but you 
 can't overload copying.
(I find this sentence a lot more useful than the explaination on the website)
 Something planned for D2 is to allow overloading copying as well.
 
Jan 31 2008