October 30, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan Reed | > I think the opCall needs to be declared static.
>
opCall is used here as a "constructor" for that struct. Thus declaring it static doesn't make any sense to me. Are you sure it must be static?
|
October 30, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | Hoenir wrote:
>> I think the opCall needs to be declared static.
>>
> opCall is used here as a "constructor" for that struct. Thus declaring it static doesn't make any sense to me. Are you sure it must be static?
Right, it's used as a constructor. That's the point: it has to be callable without having an instance of the struct laying around - hence static.
With a non-static opCall, you can use an instance of the struct as a callable entity. Static opCall lets you use the name of the struct itself as a callable entity.
Thanks,
Nathan Reed
|
October 30, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | "Hoenir" <mrmocool@gmx.de> wrote in message news:fg7o1e$2lhs$1@digitalmars.com... >> I think the opCall needs to be declared static. >> > opCall is used here as a "constructor" for that struct. Thus declaring it static doesn't make any sense to me. Are you sure it must be static? This is the stupid compromise that we got instead of struct ctors in D1. Use a static opCall, Walter says, it'll get optimized out. That's great, but it makes it impossible to initialize an instance of a struct on the heap without factoring out the initialization to _another_ function, and it's just another stupid inconsistency which we'll have to live with even though D2 will have struct ctors. *sigh* You cacn read all about it in "Dynamic Initializaion of Structs" here: http://www.digitalmars.com/d/1.0/struct.html |
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | What does this mean exactly ? initialize an instance of a struct on the heap without factoring out the initialization to _another_ function |
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
> What does this mean exactly ?
>
> initialize an instance of a struct on the heap
> without factoring out the initialization to _another_ function
You can say
MyStruct *x = new MyStruct;
But even with a static opCall defined, this doesn't work
MyStruct *x = new MyStruct(a,b,c);
You have to do something like:
MyStruct *x = new MyStruct;
*x = MyStruct(a,b,c);
Or that's what I guess he means, at least. I haven't actually tried the code above to see what it will do.
--bb
|
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | "Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fg8mpg$15gl$1@digitalmars.com... > Saaa wrote: >> What does this mean exactly ? >> >> initialize an instance of a struct on the heap >> without factoring out the initialization to _another_ function > > You can say > MyStruct *x = new MyStruct; > > But even with a static opCall defined, this doesn't work > MyStruct *x = new MyStruct(a,b,c); > > You have to do something like: > MyStruct *x = new MyStruct; > *x = MyStruct(a,b,c); > > Or that's what I guess he means, at least. I haven't actually tried the code above to see what it will do. > > --bb I was thinking more along the lines of struct S { int x, y; void init(int x, int y) { this.x = x; this.y = y; } static S opCall(int x, int y) { S s; s.init(x, y); return s; } } .. // Stack S s = S(3, 4); // Heap S* t = new S; t.init(5, 6); |
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley |
>> Saaa wrote:
>>> What does this mean exactly ?
>>>
>>> initialize an instance of a struct on the heap
>>> without factoring out the initialization to _another_ function
>>
>> You can say
>> MyStruct *x = new MyStruct;
>>
>> But even with a static opCall defined, this doesn't work
>> MyStruct *x = new MyStruct(a,b,c);
>>
>> You have to do something like:
>> MyStruct *x = new MyStruct;
>> *x = MyStruct(a,b,c);
>>
>> Or that's what I guess he means, at least. I haven't actually tried the code above to see what it will do.
>>
>> --bb
>
> I was thinking more along the lines of
>
> struct S
> {
> int x, y;
>
> void init(int x, int y)
> {
> this.x = x;
> this.y = y;
> }
>
> static S opCall(int x, int y)
> {
> S s;
> s.init(x, y);
> return s;
> }
> }
>
> ..
>
> // Stack
> S s = S(3, 4);
>
> // Heap
> S* t = new S;
> t.init(5, 6);
>
Ah, I see what you mean.
making your own _new would fix this, right? But yeah I think a constructor
would be much better ;)
I always put everything on the stack for speed purposes. I now that I think
about it, I also never throw any struct array away.
I try to allocate all necessary memory at the beginning of my programs.
Ticks are allot more scarce than memory in my programs :D
Anyway thanks.
|
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | > You can read all about it in "Dynamic Initializaion of Structs" here: http://www.digitalmars.com/d/1.0/struct.html
>
Thanks a lot for that link!
Though I don't really get the purpose of opCall. For normal member initialization struct literals are completely sufficient. opCall would just make sense as a copy constructor, but this does not work.
"
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"
|
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nathan Reed | > Object types in D are indeed reference types > http://www.digitalmars.com/d/1.0/struct.html tells the following: "Whereas classes are reference types, structs are value types." so would I have to use the & operator? |
October 31, 2007 Re: still confused about call by reference | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hoenir | "Hoenir" <mrmocool@gmx.de> wrote in message news:fgb3h9$2q19$1@digitalmars.com... >> You can read all about it in "Dynamic Initializaion of Structs" here: http://www.digitalmars.com/d/1.0/struct.html > Thanks a lot for that link! > Though I don't really get the purpose of opCall. For normal member > initialization struct literals are completely sufficient. opCall would > just make sense as a copy constructor, but this does not work. Struct literals were added after static opCall was 'blessed', so static opCall was the only way to fly for a while. Even then, the dynamic struct literals use a completely different syntax from the static struct initializers (another big *sigh*). But it's still useful to have a constructor function to i.e. check valid values, perform preprocessing on the values, fill in other members based on values that you give, etc. |
Copyright © 1999-2021 by the D Language Foundation