October 31, 2007
"Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:fgb48g$2rqq$1@digitalmars.com...
> "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.
>

Oh and I guess I should mention, struct to struct assignment is always defined as a bit copy.  You can't intercept it in any way.


November 01, 2007
>> 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.
>>
Yeah, this makes sense. I will use literals nevertheless cause I can't afford to check values etc. This vector class is supposed to be used in a ray tracing application.

> Oh and I guess I should mention, struct to struct assignment is always defined as a bit copy.  You can't intercept it in any way. 
> 
Thanks for the info.
November 01, 2007
Hoenir wrote:
>> 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?

That gets you a pointer.  You don't need & to pass a value type to a 'ref' parameter.
November 01, 2007
> That gets you a pointer.  You don't need & to pass a value type to a 'ref' parameter.

No I meant the argument passing:

void foo(Struct& S)
{...}
November 01, 2007
Hoenir wrote:
>> That gets you a pointer.  You don't need & to pass a value type to a 'ref' parameter.
> 
> No I meant the argument passing:
> 
> void foo(Struct& S)
> {...}

In D that's written:

void foo(ref Struct S)
{...}

And called like:

Struct s;
foo(s);

--bb
November 01, 2007
> In D that's written:
> 
> void foo(ref Struct S)
> {...}
> 
> And called like:
> 
> Struct s;
> foo(s);
> 
> --bb

Ok, but ref is an alias for inout atm. And I think "in" is implicitly used if nothing other is specified, isn't it?
So how would I pass a struct by reference but without being able to write (const & in C++)? and does this make sense(why is struct a value type)?

Please excuse my noob questions. :-)
November 01, 2007
"Hoenir" <mrmocool@gmx.de> wrote in message news:fgcaem$2rju$1@digitalmars.com...
>> In D that's written:
>>
>> void foo(ref Struct S)
>> {...}
>>
>> And called like:
>>
>> Struct s;
>> foo(s);
>>
>> --bb
>
> Ok, but ref is an alias for inout atm. And I think "in" is implicitly used
> if nothing other is specified, isn't it?
> So how would I pass a struct by reference but without being able to write
> (const & in C++)? and does this make sense(why is struct a value type)?
>
> Please excuse my noob questions. :-)

Your question was answered in the very first reply to your OP, by Derek:

void foo(const ref S s)
{

}

And it was also mentioned that 'const ref' crashes the compiler.


November 02, 2007
Hoenir wrote:
>> In D that's written:
>>
>> void foo(ref Struct S)
>> {...}
>>
>> And called like:
>>
>> Struct s;
>> foo(s);
>>
>> --bb
> 
> Ok, but ref is an alias for inout atm. And I think "in" is implicitly used if nothing other is specified, isn't it?

Yes.

> So how would I pass a struct by reference but without being able to write (const & in C++)? and does this make sense(why is struct a value type)?
> 
> Please excuse my noob questions. :-)

Sorry, I'm using D1.x.  What you ask is not possible with D1.x.
I expect as others have said that "const ref" will do it in D2.x as soon as that stops crashing the compiler.

--bb
1 2 3
Next ›   Last »