View mode: basic / threaded / horizontal-split · Log in · Help
September 06, 2011
What does ref means
Hi guys,

I'm a bit confused what exactly ref means and in which cases I definitely need this keyword.


Thanks for any reply!
September 06, 2011
Re: What does ref means
malio:

> I'm a bit confused what exactly ref means and in which cases I definitely need this keyword.

ref is not too much hard to understand. This is a simple usage example:


import std.stdio;
void inc(ref int x) {
   x++;
}
void main() {
   int x = 10;
   writeln(x);
   inc(x);
   writeln(x);
}


It is almost syntax sugar for:

import std.stdio;
void inc(int* x) {
   (*x)++;
}
void main() {
   int x = 10;
   writeln(x);
   inc(&x);
   writeln(x);
}


But a pointer can be null too.

Beside allowing that mutation of variables, in D you are allowed to use "const ref" too (or immutable ref), this is useful if your value is many bytes long, to avoid a costly copy:

import std.stdio;
struct Foo { int[100] a; }
void showFirst(const ref Foo f) {
   writeln(f.a[0]);
}
void main() {
   Foo f;
   f.a[] = 5;
   showFirst(f);
}


Another use for ref is on the return argument:

import std.stdio;
struct Foo {
   double[3] a;
   ref double opIndex(size_t i) {
       return a[i];
   }
}
void main() {
   Foo f;
   f.a[] = 5;
   writeln(f.a);
   f[1] = 10;
   writeln(f.a);
}

Bye,
bearophile
September 06, 2011
Re: What does ref means
== Auszug aus bearophile (bearophileHUGS@lycos.com)'s Artikel
> malio:
> > I'm a bit confused what exactly ref means and in which cases I definitely need this keyword.
> ref is not too much hard to understand. This is a simple usage example:
> import std.stdio;
> void inc(ref int x) {
>     x++;
> }
> void main() {
>     int x = 10;
>     writeln(x);
>     inc(x);
>     writeln(x);
> }
> It is almost syntax sugar for:
> import std.stdio;
> void inc(int* x) {
>     (*x)++;
> }
> void main() {
>     int x = 10;
>     writeln(x);
>     inc(&x);
>     writeln(x);
> }
> But a pointer can be null too.
> Beside allowing that mutation of variables, in D you are allowed to use "const ref" too (or immutable ref), this is useful if your value is many bytes long,
to avoid a costly copy:
> import std.stdio;
> struct Foo { int[100] a; }
> void showFirst(const ref Foo f) {
>     writeln(f.a[0]);
> }
> void main() {
>     Foo f;
>     f.a[] = 5;
>     showFirst(f);
> }
> Another use for ref is on the return argument:
> import std.stdio;
> struct Foo {
>     double[3] a;
>     ref double opIndex(size_t i) {
>         return a[i];
>     }
> }
> void main() {
>     Foo f;
>     f.a[] = 5;
>     writeln(f.a);
>     f[1] = 10;
>     writeln(f.a);
> }
> Bye,
> bearophile

Okay, thanks bearophile. But I currently doesn't exactly understand what's the difference between "ref" and "const ref"/"immutable ref". If "ref" is syntactic
sugar for pointers only (like your first example), does it also create a copy of the parameters which are marked as "ref"? I thought that pointers (and in
this context also "ref") avoid the creation of costly copies?!?

Thanks!
September 06, 2011
Re: What does ref means
malio:

> Okay, thanks bearophile. But I currently doesn't exactly understand what's the difference between "ref" and "const ref"/"immutable ref". If "ref" is syntactic
> sugar for pointers only (like your first example), does it also create a copy of the parameters which are marked as "ref"? I thought that pointers (and in
> this context also "ref") avoid the creation of costly copies?!?

"ref" just passes a reference to something, so it doesn't perform copies.

"const ref" or "immutable ref" just means that you can't change the value (with the usual semantic differences between const and immutable, that are both transitive).

For the programmer that reads your code, "ref" means the function you have written will usually modify the given argument, while "const ref" means it will not modify it.

Bye,
bearophile
September 06, 2011
Re: What does ref means
On Tue, 06 Sep 2011 05:28:22 -0400, malio <youdontwanttoknow@unknown.com>  
wrote:

> Hi guys,
>
> I'm a bit confused what exactly ref means and in which cases I  
> definitely need this keyword.

ref is simple.  It's a pointer, but without the messy pointer syntax.   
These two programs are exactly the same (will generate the same code):

void foo(int *i)
{
   *i = 5;
}

void main()
{
   int x = 2;
   foo(&x);
}

----------------------

void foo(ref int i)
{
   i = 5;
}

void main()
{
   int x = 2;
   foo(x); // note, there's no need to use &, the compiler does it for you
}

-Steve
September 06, 2011
Re: What does ref means
== Auszug aus Steven Schveighoffer (schveiguy@yahoo.com)'s Artikel
> On Tue, 06 Sep 2011 05:28:22 -0400, malio <youdontwanttoknow@unknown.com>
> wrote:
> > Hi guys,
> >
> > I'm a bit confused what exactly ref means and in which cases I
> > definitely need this keyword.
> ref is simple.  It's a pointer, but without the messy pointer syntax.
> These two programs are exactly the same (will generate the same code):
> void foo(int *i)
> {
>     *i = 5;
> }
> void main()
> {
>     int x = 2;
>     foo(&x);
> }
> ----------------------
> void foo(ref int i)
> {
>     i = 5;
> }
> void main()
> {
>     int x = 2;
>     foo(x); // note, there's no need to use &, the compiler does it for you
> }
> -Steve

> malio:
> > Okay, thanks bearophile. But I currently doesn't exactly understand what's
the difference between "ref" and "const ref"/"immutable ref". If "ref" is
syntactic
> > sugar for pointers only (like your first example), does it also create a
copy of the parameters which are marked as "ref"? I thought that pointers (and
in
> > this context also "ref") avoid the creation of costly copies?!?
> "ref" just passes a reference to something, so it doesn't perform copies.
> "const ref" or "immutable ref" just means that you can't change the value
(with the usual semantic differences between const and immutable, that are both
transitive).
> For the programmer that reads your code, "ref" means the function you have
written will usually modify the given argument, while "const ref" means it will
not modify it.
> Bye,
> bearophile

Ah, okay - thanks in advance bearophile and Steve :)
September 07, 2011
Re: What does ref means
On 06/09/2011 12:00, bearophile wrote:
> malio:
> 
>> Okay, thanks bearophile. But I currently doesn't exactly understand
>> what's the difference between "ref" and "const ref"/"immutable
>> ref". If "ref" is syntactic sugar for pointers only (like your
>> first example), does it also create a copy of the parameters which
>> are marked as "ref"? I thought that pointers (and in this context
>> also "ref") avoid the creation of costly copies?!?
> 
> "ref" just passes a reference to something, so it doesn't perform
> copies.
> 
> "const ref" or "immutable ref" just means that you can't change the
> value (with the usual semantic differences between const and
> immutable, that are both transitive).

So if a parameter is immutable (without ref) the compiler could infer a
ref to avoid copy because it can't be modified?

> For the programmer that reads your code, "ref" means the function you
> have written will usually modify the given argument, while "const
> ref" means it will not modify it.
September 07, 2011
Re: What does ref means
On Wed, 07 Sep 2011 20:50:04 +0200, Johannes Totz <johannes@jo-t.de> wrote:

> On 06/09/2011 12:00, bearophile wrote:
>> malio:
>>
>>> Okay, thanks bearophile. But I currently doesn't exactly understand
>>> what's the difference between "ref" and "const ref"/"immutable
>>> ref". If "ref" is syntactic sugar for pointers only (like your
>>> first example), does it also create a copy of the parameters which
>>> are marked as "ref"? I thought that pointers (and in this context
>>> also "ref") avoid the creation of costly copies?!?
>>
>> "ref" just passes a reference to something, so it doesn't perform
>> copies.
>>
>> "const ref" or "immutable ref" just means that you can't change the
>> value (with the usual semantic differences between const and
>> immutable, that are both transitive).
>
> So if a parameter is immutable (without ref) the compiler could infer a
> ref to avoid copy because it can't be modified?

Theoretically at least. I don't believe such an optimization is actually
performed in current D compilers.


-- 
  Simen
September 07, 2011
Re: What does ref means
On 09/07/2011 08:50 PM, Johannes Totz wrote:
> On 06/09/2011 12:00, bearophile wrote:
>> malio:
>>
>>> Okay, thanks bearophile. But I currently doesn't exactly understand
>>> what's the difference between "ref" and "const ref"/"immutable
>>> ref". If "ref" is syntactic sugar for pointers only (like your
>>> first example), does it also create a copy of the parameters which
>>> are marked as "ref"? I thought that pointers (and in this context
>>> also "ref") avoid the creation of costly copies?!?
>>
>> "ref" just passes a reference to something, so it doesn't perform
>> copies.
>>
>> "const ref" or "immutable ref" just means that you can't change the
>> value (with the usual semantic differences between const and
>> immutable, that are both transitive).
>
> So if a parameter is immutable (without ref) the compiler could infer a
> ref to avoid copy because it can't be modified?

In theory it could. I don't think the current D compilers do that. To 
allow it, clear rules would have to be fixed when to apply the 
optimization and when not to apply the optimization.
(that is not an issue if the compiler compiles the whole project in one 
pass though.)

But probably having to write 'ref' yourself to make calls faster is good 
enough, it also does not influence inline assembly in strange and 
non-visible ways.


>
>> For the programmer that reads your code, "ref" means the function you
>> have written will usually modify the given argument, while "const
>> ref" means it will not modify it.
Top | Discussion index | About this forum | D home