February 17, 2008
But if I use a ref or a pointer, the function is allowed to change the
variable.
Using 'in' will make a copy?
I thought ref was just a renaming of  'inout'.
But there is no way to only pass a reference without giving the function
write permission?

I sent a lot of big arrays in structs, so I think this is kind of crucial for me :/

> Use ref or pointer, whatever suits you best.  Usually ref is preferable because it makes no difference at the call site:
>
> void func(ref S s){
> ..
> }
>
> void main (){
> S t;
> func(t);  // only a pointer is actually passed
> ..
> }
>
> If you use a pointer though, you must explicitly pass an address of your struct:
>
> void func(S* s){
> ..
> }
>
> void main (){
> S t;
> func(&t); // explicit address expression
> ..
> }
>
> -- 
> SnakE


February 17, 2008
Saaa wrote:
> But if I use a ref or a pointer, the function is allowed to change the variable.
> Using 'in' will make a copy?

For D1, in is the same as nothing at all.  In D2 I think in is the same as 'invariant'.  So yeh, it will make a copy.

> I thought ref was just a renaming of  'inout'.
> But there is no way to only pass a reference without giving the function write permission?
> 
> I sent a lot of big arrays in structs, so I think this is kind of crucial for me :/

No way in D1.  Yes way in D2 (const ref or invariant ref should work in latest DMD).

--bb
February 17, 2008
"Bill Baxter" <dnewsgroup@billbaxter.com> wrote in message news:fp9ih7$29av$2@digitalmars.com...
> Saaa wrote:
>> But if I use a ref or a pointer, the function is allowed to change the
>> variable.
>> Using 'in' will make a copy?
>
> For D1, in is the same as nothing at all.  In D2 I think in is the same as 'invariant'.  So yeh, it will make a copy.
>
>> I thought ref was just a renaming of  'inout'.
>> But there is no way to only pass a reference without giving the function
>> write permission?
>>
>> I sent a lot of big arrays in structs, so I think this is kind of crucial for me :/
>
> No way in D1.  Yes way in D2 (const ref or invariant ref should work in latest DMD).

Thanks! This is really good to know. Now I understand why they changed inout
to ref.
This will greatly optimize my program as I use a LOT of big static arrays in
structs.
I'll use ref from now on for every passing of one of those :)


February 17, 2008
On Sun, 17 Feb 2008 15:32:43 +0100, Saaa <empty@needmail.com> wrote:

> Making it:
> It's a global variable that can be accessed/used only inside, the set of
> names (variables, etc) that you can see and use from the code inside the
> function, aka namespace of that function.
>
> Does it contradict with:
> A static variable inside a function (which I did not cover in my last
> post), will keep its value after the function has exited, and even when
> it's being called again.
>
> I can't tell :D

Both things are correct, only different wordings. I don't like calling static locals for global variables, for to me that implies them being available globally, which they are not. They do however share other features with global variables (namely retaining their value after the function has exited).

Capiche?
1 2 3
Next ›   Last »