digitalmars.D.learn - What does ref means
- malio (3/3) Sep 06 2011 Hi guys,
- bearophile (52/53) Sep 06 2011 ref is not too much hard to understand. This is a simple usage example:
- malio (6/59) Sep 06 2011 Okay, thanks bearophile. But I currently doesn't exactly understand what...
- bearophile (6/9) Sep 06 2011 "ref" just passes a reference to something, so it doesn't perform copies...
- malio (10/47) Sep 06 2011 the difference between "ref" and "const ref"/"immutable ref". If "ref" i...
- Johannes Totz (3/21) Sep 07 2011 So if a parameter is immutable (without ref) the compiler could infer a
- Simen Kjaeraas (5/23) Sep 07 2011 Theoretically at least. I don't believe such an optimization is actually
- Timon Gehr (9/30) Sep 07 2011 In theory it could. I don't think the current D compilers do that. To
- Steven Schveighoffer (24/27) Sep 06 2011 ref is simple. It's a pointer, but without the messy pointer syntax.
Hi guys, I'm a bit confused what exactly ref means and in which cases I definitely need this keyword. Thanks for any reply!
Sep 06 2011
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
Sep 06 2011
== Auszug aus bearophile (bearophileHUGS lycos.com)'s Artikelmalio:to avoid a costly copy: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,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, bearophileOkay, 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!
Sep 06 2011
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
Sep 06 2011
== Auszug aus Steven Schveighoffer (schveiguy yahoo.com)'s ArtikelOn 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 } -Stevemalio:the difference between "ref" and "const ref"/"immutable ref". If "ref" is syntacticOkay, thanks bearophile. But I currently doesn't exactly understand what'scopy of the parameters which are marked as "ref"? I thought that pointers (and insugar for pointers only (like your first example), does it also create a(with the usual semantic differences between const and immutable, that are both transitive).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 valueFor the programmer that reads your code, "ref" means the function you havewritten will usually modify the given argument, while "const ref" means it will not modify it.Bye, bearophileAh, okay - thanks in advance bearophile and Steve :)
Sep 06 2011
On 06/09/2011 12:00, bearophile wrote:malio:So if a parameter is immutable (without ref) the compiler could infer a ref to avoid copy because it can't be modified?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.
Sep 07 2011
On Wed, 07 Sep 2011 20:50:04 +0200, Johannes Totz <johannes jo-t.de> wrote:On 06/09/2011 12:00, bearophile wrote:Theoretically at least. I don't believe such an optimization is actually performed in current D compilers. -- Simenmalio:So if a parameter is immutable (without ref) the compiler could infer a ref to avoid copy because it can't be modified?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).
Sep 07 2011
On 09/07/2011 08:50 PM, Johannes Totz wrote:On 06/09/2011 12:00, bearophile wrote: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.malio:So if a parameter is immutable (without ref) the compiler could infer a ref to avoid copy because it can't be modified?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.
Sep 07 2011
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
Sep 06 2011