digitalmars.D.learn - ref is pointer sugar?
- Pluto (8/8) Aug 06 2010 Are these equivalent?
- Steven Schveighoffer (12/20) Aug 06 2010 They are pretty much equivalent. I think the code generated actually wi...
- Pluto (5/29) Aug 06 2010 I read it on the function page of D1.
- Pluto (2/19) Aug 06 2010 Where is this in the spec? I can't find it. Still used to ->
- Jacob Carlborg (4/23) Aug 06 2010 http://www.digitalmars.com/d/1.0/class.html section "Fields".
- Pluto (3/25) Aug 06 2010 And this is more generally also applicable to dereferencing pointers.
Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s); If so, why is it stated that ref is very rarely used? It looks like something I would use a lot with structures.
Aug 06 2010
On Fri, 06 Aug 2010 08:51:31 -0400, Pluto <pluto planets.not> wrote:Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s);They are pretty much equivalent. I think the code generated actually will be exactly the same. However, the compiler treats ref differently than pointers. For example, ref is allowed in the safe subset of D, and pointers are not. Note that you do not need to dereference pointers to access their pointed-to members. i.e.: void f2(S* s){s.x++;}If so, why is it stated that ref is very rarely used? It looks like something I would use a lot with structures.What? Where does it say that? That's very wrong, ref is used everywhere. If not explicitly, at least every struct member function passes 'this' by ref. -Steve
Aug 06 2010
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleOn Fri, 06 Aug 2010 08:51:31 -0400, Pluto <pluto planets.not> wrote:I read it on the function page of D1. "out is rare enough, and ref even rarer, to attach the keywords to them and leave in as the default. " I would think out to be more rare then ref.Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s);They are pretty much equivalent. I think the code generated actually will be exactly the same. However, the compiler treats ref differently than pointers. For example, ref is allowed in the safe subset of D, and pointers are not. Note that you do not need to dereference pointers to access their pointed-to members. i.e.: void f2(S* s){s.x++;}If so, why is it stated that ref is very rarely used? It looks like something I would use a lot with structures.What? Where does it say that? That's very wrong, ref is used everywhere. If not explicitly, at least every struct member function passes 'this' by ref. -Steve
Aug 06 2010
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articleOn Fri, 06 Aug 2010 08:51:31 -0400, Pluto <pluto planets.not> wrote:Where is this in the spec? I can't find it. Still used to ->Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s);They are pretty much equivalent. I think the code generated actually will be exactly the same. However, the compiler treats ref differently than pointers. For example, ref is allowed in the safe subset of D, and pointers are not. Note that you do not need to dereference pointers to access their pointed-to members. i.e.: void f2(S* s){s.x++;}
Aug 06 2010
On 2010-08-06 15:33, Pluto wrote:== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articlehttp://www.digitalmars.com/d/1.0/class.html section "Fields". -- /Jacob CarlborgOn Fri, 06 Aug 2010 08:51:31 -0400, Pluto<pluto planets.not> wrote:Where is this in the spec? I can't find it. Still used to ->Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s);They are pretty much equivalent. I think the code generated actually will be exactly the same. However, the compiler treats ref differently than pointers. For example, ref is allowed in the safe subset of D, and pointers are not. Note that you do not need to dereference pointers to access their pointed-to members. i.e.: void f2(S* s){s.x++;}
Aug 06 2010
== Quote from Jacob Carlborg (doob me.com)'s articleOn 2010-08-06 15:33, Pluto wrote:And this is more generally also applicable to dereferencing pointers. Isn't that showing a bit of a gap in the specs? (bug report?)== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s articlehttp://www.digitalmars.com/d/1.0/class.html section "Fields".On Fri, 06 Aug 2010 08:51:31 -0400, Pluto<pluto planets.not> wrote:Where is this in the spec? I can't find it. Still used to ->Are these equivalent? S s;//struct void f(ref S s){s.x++;} f(s); void f2(S* s){(*s).x++;} f2(&s);They are pretty much equivalent. I think the code generated actually will be exactly the same. However, the compiler treats ref differently than pointers. For example, ref is allowed in the safe subset of D, and pointers are not. Note that you do not need to dereference pointers to access their pointed-to members. i.e.: void f2(S* s){s.x++;}
Aug 06 2010