Jump to page: 1 2
Thread overview
opCall example
Jan 30, 2008
Saaa
Jan 30, 2008
Ary Borenszweig
Jan 30, 2008
Extrawurst
Searching the digitalmars webpage (was: Re: opCall example)
Jan 30, 2008
Ary Borenszweig
Re: Searching the digitalmars webpage
Jan 30, 2008
Extrawurst
Jan 30, 2008
Ary Borenszweig
Jan 30, 2008
Ary Borenszweig
Jan 31, 2008
Saaa
Jan 31, 2008
Saaa
January 30, 2008
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







January 30, 2008
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.
January 30, 2008

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
January 30, 2008
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?
January 30, 2008
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?
January 30, 2008
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?
January 30, 2008
"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.


January 30, 2008
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.
January 30, 2008
"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.


January 31, 2008
>>> 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) :)


« First   ‹ Prev
1 2