www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Voldemort structs no longer work?

reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
With latest git dmd:

	auto makeVoldemort(int x) {
		struct Voldemort {
			 property int value() { return x; }
		}
		return Voldemort();
	}
	void main() {
		auto v = makeVoldemort();
		writeln(v.value);
	}

Compile error:

	test.d(3): Error: function test.makeVoldemort.Voldemort.value cannot access
frame of function test.makeVoldemort

Changing 'struct' to 'class' works. Is this deliberate, or is it a bug?
It is certainly inconsistent with Walter's article on Voldemort types,
which uses structs as examples.


T

-- 
You have to expect the unexpected. -- RL
Dec 15 2012
next sibling parent "Iain Buclaw" <ibuclaw ubuntu.com> writes:
On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:
 With latest git dmd:

 	auto makeVoldemort(int x) {
 		struct Voldemort {
 			 property int value() { return x; }
 		}
 		return Voldemort();
 	}
 	void main() {
 		auto v = makeVoldemort();
 		writeln(v.value);
 	}

 Compile error:

 	test.d(3): Error: function test.makeVoldemort.Voldemort.value 
 cannot access frame of function test.makeVoldemort

 Changing 'struct' to 'class' works. Is this deliberate, or is 
 it a bug?
 It is certainly inconsistent with Walter's article on Voldemort 
 types,
 which uses structs as examples.


 T

Pretty certain it's deliberate. No closure is created for nested structs to access it's parent, complying with it's POD behaviour. Regards, Iain.
Dec 15 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:
 On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:
 With latest git dmd:
 	auto makeVoldemort(int x) {
 	
 		struct Voldemort {
 		
 			 property int value() { return x; }
 		
 		}
 		return Voldemort();
 	
 	}
 	void main() {
 	
 		auto v = makeVoldemort();
 		writeln(v.value);
 	
 	}
 
 Compile error:
 	test.d(3): Error: function test.makeVoldemort.Voldemort.value
 
 cannot access frame of function test.makeVoldemort
 
 Changing 'struct' to 'class' works. Is this deliberate, or is
 it a bug?
 It is certainly inconsistent with Walter's article on Voldemort
 types,
 which uses structs as examples.
 
 
 T

Pretty certain it's deliberate. No closure is created for nested structs to access it's parent, complying with it's POD behaviour.

static nested structs don't have access to their outer scopes. Non-static structs do. This reeks of a bug. - Jonathan M Davis
Dec 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Dec 15, 2012 at 11:31:22AM -0800, Jonathan M Davis wrote:
 On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:
 On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:
 With latest git dmd:
 	auto makeVoldemort(int x) {
 	
 		struct Voldemort {
 		
 			 property int value() { return x; }
 		
 		}
 		return Voldemort();
 	
 	}
 	void main() {
 	
 		auto v = makeVoldemort();
 		writeln(v.value);
 	
 	}
 
 Compile error:
 	test.d(3): Error: function test.makeVoldemort.Voldemort.value
 
 cannot access frame of function test.makeVoldemort
 
 Changing 'struct' to 'class' works. Is this deliberate, or is it a
 bug?  It is certainly inconsistent with Walter's article on
 Voldemort types, which uses structs as examples.



 Pretty certain it's deliberate.  No closure is created for nested
 structs to access it's parent, complying with it's POD behaviour.

static nested structs don't have access to their outer scopes. Non-static structs do. This reeks of a bug.

Found the reference in TDPL, 7.1.9 (p.263): Nested structs embed the magic "frame pointer" that allows them to access outer values such as a and b in the example above. [...] If you want to define a nested struct without that baggage, just prefix struct with static in the definition of Local, which makes Local a regular struct and consequently prevents it from accessing a and b. Ironically enough, Andrei in the subsequent paragraph discourages the use of such nested structs, whereas Walter's article promotes the use of such Voldemort types as a "happy discovery". :) Anyway, filed a bug: http://d.puremagic.com/issues/show_bug.cgi?id=9162 T -- Nobody is perfect. I am Nobody. -- pepoluan, GKC forum
Dec 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Dec 15, 2012 at 11:45:10AM -0800, H. S. Teoh wrote:
[...]
 Found the reference in TDPL, 7.1.9 (p.263):
 
 	Nested structs embed the magic "frame pointer" that allows them
 	to access outer values such as a and b in the example above.
 	[...] If you want to define a nested struct without that
 	baggage, just prefix struct with static in the definition of
 	Local, which makes Local a regular struct and consequently
 	prevents it from accessing a and b.
 
 Ironically enough, Andrei in the subsequent paragraph discourages the
 use of such nested structs, whereas Walter's article promotes the use of
 such Voldemort types as a "happy discovery". :)
 
 Anyway, filed a bug:
 
 	http://d.puremagic.com/issues/show_bug.cgi?id=9162

Also, according to http://dlang.org/struct.html: A nested struct is a struct that is declared inside the scope of a function or a templated struct that has aliases to local functions as a template argument. Nested structs have member functions. It has access to the context of its enclosing scope (via an added hidden field). T -- If you compete with slaves, you become a slave. -- Norbert Wiener
Dec 15 2012
prev sibling next sibling parent Iain Buclaw <ibuclaw ubuntu.com> writes:
--047d7bf0effe6a259304d0e991e4
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 15 December 2012 19:45, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:

 On Sat, Dec 15, 2012 at 11:31:22AM -0800, Jonathan M Davis wrote:
 On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:
 On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:
 With latest git dmd:
   auto makeVoldemort(int x) {

           struct Voldemort {

                    property int value() { return x; }

           }
           return Voldemort();

   }
   void main() {

           auto v =3D makeVoldemort();
           writeln(v.value);

   }

 Compile error:
   test.d(3): Error: function test.makeVoldemort.Voldemort.value

 cannot access frame of function test.makeVoldemort

 Changing 'struct' to 'class' works. Is this deliberate, or is it a
 bug?  It is certainly inconsistent with Walter's article on
 Voldemort types, which uses structs as examples.



 Pretty certain it's deliberate.  No closure is created for nested
 structs to access it's parent, complying with it's POD behaviour.

static nested structs don't have access to their outer scopes. Non-static structs do. This reeks of a bug.

Found the reference in TDPL, =A77.1.9 (p.263): Nested structs embed the magic "frame pointer" that allows them to access outer values such as a and b in the example above. [...] If you want to define a nested struct without that baggage, just prefix struct with static in the definition of Local, which makes Local a regular struct and consequently prevents it from accessing a and b. Ironically enough, Andrei in the subsequent paragraph discourages the use of such nested structs, whereas Walter's article promotes the use of such Voldemort types as a "happy discovery". :) Anyway, filed a bug: http://d.puremagic.com/issues/show_bug.cgi?id=3D9162

--=20 Iain Buclaw *(p < e ? p++ : p) =3D (c & 0x0f) + '0'; --047d7bf0effe6a259304d0e991e4 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_extra"><div class=3D"gmail_quote">On 15 December 2012 1= 9:45, H. S. Teoh <span dir=3D"ltr">&lt;<a href=3D"mailto:hsteoh quickfur.at= h.cx" target=3D"_blank">hsteoh quickfur.ath.cx</a>&gt;</span> wrote:<br><bl= ockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-lef= t:1px solid rgb(204,204,204);padding-left:1ex"> <div class=3D"im">On Sat, Dec 15, 2012 at 11:31:22AM -0800, Jonathan M Davi= s wrote:<br> &gt; On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:<br> &gt; &gt; On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:<= br> &gt; &gt; &gt; With latest git dmd:<br> &gt; &gt; &gt; =A0 auto makeVoldemort(int x) {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 struct Voldemort {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 property int value() { = return x; }<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 }<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 return Voldemort();<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 }<br> &gt; &gt; &gt; =A0 void main() {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 auto v =3D makeVoldemort();<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 writeln(v.value);<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 }<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; Compile error:<br> &gt; &gt; &gt; =A0 test.d(3): Error: function test.makeVoldemort.Voldemort.= value<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; cannot access frame of function test.makeVoldemort<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; Changing &#39;struct&#39; to &#39;class&#39; works. Is this = deliberate, or is it a<br> &gt; &gt; &gt; bug? =A0It is certainly inconsistent with Walter&#39;s artic= le on<br> &gt; &gt; &gt; Voldemort types, which uses structs as examples.<br> </div>[...]<br> <div class=3D"im">&gt; &gt; Pretty certain it&#39;s deliberate. =A0No closu= re is created for nested<br> &gt; &gt; structs to access it&#39;s parent, complying with it&#39;s POD be= haviour.<br> &gt;<br> &gt; static nested structs don&#39;t have access to their outer scopes.<br> &gt; Non-static structs do. This reeks of a bug.<br> </div>[...]<br> <br> Found the reference in TDPL, =A77.1.9 (p.263):<br> <br> =A0 =A0 =A0 =A0 Nested structs embed the magic &quot;frame pointer&quot; th= at allows them<br> =A0 =A0 =A0 =A0 to access outer values such as a and b in the example above= .<br> =A0 =A0 =A0 =A0 [...] If you want to define a nested struct without that<br=

f<br> =A0 =A0 =A0 =A0 Local, which makes Local a regular struct and consequently<= br> =A0 =A0 =A0 =A0 prevents it from accessing a and b.<br> <br> Ironically enough, Andrei in the subsequent paragraph discourages the<br> use of such nested structs, whereas Walter&#39;s article promotes the use o= f<br> such Voldemort types as a &quot;happy discovery&quot;. :)<br> <br> Anyway, filed a bug:<br> <br> =A0 =A0 =A0 =A0 <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D= 9162" target=3D"_blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D916= 2</a><br> <span class=3D""><font color=3D"#888888"><br></font></span></blockquote></d= iv><br>If it is one, it&#39;s a bug in FuncDeclaration::getLevel.<br><br cl= ear=3D"all"><br>-- <br>Iain Buclaw<br><br>*(p &lt; e ? p++ : p) =3D (c &amp= ; 0x0f) + &#39;0&#39;;<br> </div> --047d7bf0effe6a259304d0e991e4--
Dec 15 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, December 15, 2012 11:45:10 H. S. Teoh wrote:
 Ironically enough, Andrei in the subsequent paragraph discourages the
 use of such nested structs, whereas Walter's article promotes the use of
 such Voldemort types as a "happy discovery". :)

No, the real irony is that it's Andrei who promoted them in the first place. :) We _are_ finding some serious issues with them though (e.g. they don't work with init and apparently can't work with init), and there has been some discussion of ditching them due to such issues, but no consensus has been reached on that. - Jonathan M Davis
Dec 15 2012
prev sibling next sibling parent Iain Buclaw <ibuclaw ubuntu.com> writes:
--047d7bf0effe59739804d0e9a64f
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 15 December 2012 19:58, Iain Buclaw <ibuclaw ubuntu.com> wrote:

 On 15 December 2012 19:45, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:

 On Sat, Dec 15, 2012 at 11:31:22AM -0800, Jonathan M Davis wrote:
 On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:
 On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:
 With latest git dmd:
   auto makeVoldemort(int x) {

           struct Voldemort {

                    property int value() { return x; }

           }
           return Voldemort();

   }
   void main() {

           auto v =3D makeVoldemort();
           writeln(v.value);

   }

 Compile error:
   test.d(3): Error: function test.makeVoldemort.Voldemort.value

 cannot access frame of function test.makeVoldemort

 Changing 'struct' to 'class' works. Is this deliberate, or is it a
 bug?  It is certainly inconsistent with Walter's article on
 Voldemort types, which uses structs as examples.



 Pretty certain it's deliberate.  No closure is created for nested
 structs to access it's parent, complying with it's POD behaviour.

static nested structs don't have access to their outer scopes. Non-static structs do. This reeks of a bug.

Found the reference in TDPL, =A77.1.9 (p.263): Nested structs embed the magic "frame pointer" that allows them to access outer values such as a and b in the example above. [...] If you want to define a nested struct without that baggage, just prefix struct with static in the definition of Local, which makes Local a regular struct and consequently prevents it from accessing a and b. Ironically enough, Andrei in the subsequent paragraph discourages the use of such nested structs, whereas Walter's article promotes the use of such Voldemort types as a "happy discovery". :) Anyway, filed a bug: http://d.puremagic.com/issues/show_bug.cgi?id=3D9162


struct Voldemort does not represent itself as a nested type. --=20 Iain Buclaw *(p < e ? p++ : p) =3D (c & 0x0f) + '0'; --047d7bf0effe59739804d0e9a64f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_extra"><div class=3D"gmail_quote">On 15 December 2012 1= 9:58, Iain Buclaw <span dir=3D"ltr">&lt;<a href=3D"mailto:ibuclaw ubuntu.co= m" target=3D"_blank">ibuclaw ubuntu.com</a>&gt;</span> wrote:<br><blockquot= e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s= olid rgb(204,204,204);padding-left:1ex"> <div class=3D"gmail_extra"><div><div class=3D"h5"><div class=3D"gmail_quote= ">On 15 December 2012 19:45, H. S. Teoh <span dir=3D"ltr">&lt;<a href=3D"ma= ilto:hsteoh quickfur.ath.cx" target=3D"_blank">hsteoh quickfur.ath.cx</a>&g= t;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-= left:1px solid rgb(204,204,204);padding-left:1ex"> <div>On Sat, Dec 15, 2012 at 11:31:22AM -0800, Jonathan M Davis wrote:<br> &gt; On Saturday, December 15, 2012 19:50:34 Iain Buclaw wrote:<br> &gt; &gt; On Saturday, 15 December 2012 at 18:38:29 UTC, H. S. Teoh wrote:<= br> &gt; &gt; &gt; With latest git dmd:<br> &gt; &gt; &gt; =A0 auto makeVoldemort(int x) {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 struct Voldemort {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 property int value() { = return x; }<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 }<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 return Voldemort();<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 }<br> &gt; &gt; &gt; =A0 void main() {<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 auto v =3D makeVoldemort();<br> &gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 writeln(v.value);<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; =A0 }<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; Compile error:<br> &gt; &gt; &gt; =A0 test.d(3): Error: function test.makeVoldemort.Voldemort.= value<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; cannot access frame of function test.makeVoldemort<br> &gt; &gt; &gt;<br> &gt; &gt; &gt; Changing &#39;struct&#39; to &#39;class&#39; works. Is this = deliberate, or is it a<br> &gt; &gt; &gt; bug? =A0It is certainly inconsistent with Walter&#39;s artic= le on<br> &gt; &gt; &gt; Voldemort types, which uses structs as examples.<br> </div>[...]<br> <div>&gt; &gt; Pretty certain it&#39;s deliberate. =A0No closure is created= for nested<br> &gt; &gt; structs to access it&#39;s parent, complying with it&#39;s POD be= haviour.<br> &gt;<br> &gt; static nested structs don&#39;t have access to their outer scopes.<br> &gt; Non-static structs do. This reeks of a bug.<br> </div>[...]<br> <br> Found the reference in TDPL, =A77.1.9 (p.263):<br> <br> =A0 =A0 =A0 =A0 Nested structs embed the magic &quot;frame pointer&quot; th= at allows them<br> =A0 =A0 =A0 =A0 to access outer values such as a and b in the example above= .<br> =A0 =A0 =A0 =A0 [...] If you want to define a nested struct without that<br=

f<br> =A0 =A0 =A0 =A0 Local, which makes Local a regular struct and consequently<= br> =A0 =A0 =A0 =A0 prevents it from accessing a and b.<br> <br> Ironically enough, Andrei in the subsequent paragraph discourages the<br> use of such nested structs, whereas Walter&#39;s article promotes the use o= f<br> such Voldemort types as a &quot;happy discovery&quot;. :)<br> <br> Anyway, filed a bug:<br> <br> =A0 =A0 =A0 =A0 <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D= 9162" target=3D"_blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D916= 2</a><br> <span><font color=3D"#888888"><br></font></span></blockquote></div><br></di= v></div>If it is one, it&#39;s a bug in FuncDeclaration::getLevel.<span cla= ss=3D""><font color=3D"#888888"><br></font></span><br clear=3D"all"></div><= /blockquote> </div><br>And if it isn&#39;t FuncDeclaration::getLevel(), then it is a bug= because the struct Voldemort does not represent itself as a nested type.<b= r><br>-- <br>Iain Buclaw<br><br>*(p &lt; e ? p++ : p) =3D (c &amp; 0x0f) + = &#39;0&#39;;<br> </div> --047d7bf0effe59739804d0e9a64f--
Dec 15 2012
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Dec 15, 2012 at 12:02:16PM -0800, Jonathan M Davis wrote:
 On Saturday, December 15, 2012 11:45:10 H. S. Teoh wrote:
 Ironically enough, Andrei in the subsequent paragraph discourages
 the use of such nested structs, whereas Walter's article promotes
 the use of such Voldemort types as a "happy discovery". :)

No, the real irony is that it's Andrei who promoted them in the first place. :)

Heh. :)
 We _are_ finding some serious issues with them though (e.g. they don't
 work with init and apparently can't work with init), and there has
 been some discussion of ditching them due to such issues, but no
 consensus has been reached on that.

Hmm, that's true. They can't possibly work with init: if you do something like: auto func(int x) { struct V { property int value() { return x; } } return V(); } auto v = func(123); auto u = v.init; // <-- what happens here? writeln(u.value); // <-- and here? It seems that we'd have to make .init illegal on these Voldemort structs. But that breaks consistency with the rest of the language that every type must have an .init value. Alternatively, .init can implicitly create a context where the value of x is set to int.init. But this is unnecessary (it's supposed to be a *Voldemort* type!) and very ugly (why are we creating a function's local context when it isn't even being called?), not to mention useless (u.value will return a nonsensical value). Or, perhaps a less intrusive workaround, is to have .init set the hidden context pointer to null, and you'll get a null deference when accessing u.value. Which is not pretty either, since no pointers or references are apparent in V.value; it's implicit. It seems that the only clean way to do this is to use a class instead of a struct, since the .init value will conveniently just be null, thereby sidestepping the problem. T -- If creativity is stifled by rigid discipline, then it is not true creativity.
Dec 15 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, December 15, 2012 12:18:21 H. S. Teoh wrote:
 It seems that the only clean way to do this is to use a class instead of
 a struct, since the .init value will conveniently just be null, thereby
 sidestepping the problem.

That would incur unnecessary overhead and probably break all kinds of code, because they're then reference types instead of value types, and a _lot_ of code doesn't use save propperly. If structs can't do what we need as Voldemort types, it's just better to make it so that they're not Voldemort types. Voldemort types are a cute idea, and in principle are great, but I don't think that it's worth fighting to keep them if they have problems. - Jonathan M Davis
Dec 15 2012
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Saturday, 15 December 2012 at 21:10:19 UTC, Jonathan M Davis 
wrote:
 On Saturday, December 15, 2012 12:18:21 H. S. Teoh wrote:
 It seems that the only clean way to do this is to use a class 
 instead of
 a struct, since the .init value will conveniently just be 
 null, thereby
 sidestepping the problem.

That would incur unnecessary overhead and probably break all kinds of code, because they're then reference types instead of value types, and a _lot_ of code doesn't use save propperly. If structs can't do what we need as Voldemort types, it's just better to make it so that they're not Voldemort types. Voldemort types are a cute idea, and in principle are great, but I don't think that it's worth fighting to keep them if they have problems. - Jonathan M Davis

I always found them inconsistent with the behavior they have in classes (where no outer pointer is created). This is a lot of work to do in the standard lib however.
Dec 15 2012
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Dec 15, 2012 at 01:09:33PM -0800, Jonathan M Davis wrote:
 On Saturday, December 15, 2012 12:18:21 H. S. Teoh wrote:
 It seems that the only clean way to do this is to use a class
 instead of a struct, since the .init value will conveniently just be
 null, thereby sidestepping the problem.

That would incur unnecessary overhead and probably break all kinds of code, because they're then reference types instead of value types, and a _lot_ of code doesn't use save propperly. If structs can't do what we need as Voldemort types, it's just better to make it so that they're not Voldemort types. Voldemort types are a cute idea, and in principle are great, but I don't think that it's worth fighting to keep them if they have problems.

Well, the current way many Phobos ranges work, they're kind of pseudo-Voldemort types (except they don't actually need/use the context pointer): auto cycle(R)(R range) { struct CycleImpl { R r; this(R _r) { r = _r; } ... // range methods } return CycleImpl(range); } auto r = cycle(myRange); While it's true that you can write: typeof(cycle(myRange)) s; and thereby break encapsulation, if someone is desperate enough to do such things they probably have a good reason for it, and they should be able to deal with the consequences too. But anyway, thinking a bit more about the .init problem, couldn't we just say that .init is not accessible outside the scope of the function that defines the type, and therefore you cannot declare a variable of that type (using typeof or whatever other workaround) without also assigning it to an already-initialized instance of the type? This way, the type still has an .init, except that it's only accessible inside the function itself. Or are there unintended consequences here? T -- It is impossible to make anything foolproof because fools are so ingenious. -- Sammy
Dec 15 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/15/2012 10:44 PM, H. S. Teoh wrote:
 ...
 This way, the type still has an .init, except that it's only accessible
 inside the function itself. Or are there unintended consequences here?

Lazy initialization of a member of such a type would require unsafe language features and not work in CTFE.
Dec 15 2012
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, December 15, 2012 13:44:13 H. S. Teoh wrote:
 But anyway, thinking a bit more about the .init problem, couldn't we
 just say that .init is not accessible outside the scope of the function
 that defines the type, and therefore you cannot declare a variable of
 that type (using typeof or whatever other workaround) without also
 assigning it to an already-initialized instance of the type?
 
 This way, the type still has an .init, except that it's only accessible
 inside the function itself. Or are there unintended consequences here?

That's pretty much how it is now. The problem is all of the stuff that requires .init. For instance, lots of template constraints and static if use .init to check stuff about a type. Not having an .init gets in the way of a lot of template metaprogramming. And sometimes, you have to have .init or some things are impossible. For instance, takeNone will attempt to return an empty range of the given type (something which some algorithms need, or they won't work), and if the range doesn't have init or slicing, then it's forced to return the result of takeExactly, which is then a different range type. For some stuff, that's fine, for other stuff, that renders it unusable. I think that the only place that that affects Phobos at the moment is that you lose an optimization path in one of find's overloads, but I've been in other situations where I've had to make a range empty without changing its type and popping all of the elements off was unacceptable, and Voldemort types make that impossible. We may be able to work around enough of the problems caused by a lack of a usable init property to be able to continue to use Voldemort types, but some issues can't be fixed with them (like that of takeNone), and until we do find a solution for even the problems that we can fix, the lack of an init property tends to be crippling for metaprogramming. - Jonathan M Davis
Dec 15 2012
prev sibling next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 12/15/2012 10:36 AM, H. S. Teoh wrote:
 With latest git dmd:

 	auto makeVoldemort(int x) {
 		struct Voldemort {
 			 property int value() { return x; }
 		}
 		return Voldemort();
 	}
 	void main() {
 		auto v = makeVoldemort();
 		writeln(v.value);
 	}

 Compile error:

 	test.d(3): Error: function test.makeVoldemort.Voldemort.value cannot access
frame of function test.makeVoldemort

This compiles if the property is elided. Definitely a bug.
Dec 15 2012
prev sibling next sibling parent "Rob T" <rob ucora.com> writes:
Good finds.

The definition of a "nested struct" is not consistently or well 
defined, so there's no wonder it's not working as anyone expects.

--rt
Dec 15 2012
prev sibling next sibling parent "js.mdnq" <js_adddot+mdng gmail.com> writes:
On Saturday, 15 December 2012 at 20:20:10 UTC, H. S. Teoh wrote:
 On Sat, Dec 15, 2012 at 12:02:16PM -0800, Jonathan M Davis 
 wrote:
 On Saturday, December 15, 2012 11:45:10 H. S. Teoh wrote:
 Ironically enough, Andrei in the subsequent paragraph 
 discourages
 the use of such nested structs, whereas Walter's article 
 promotes
 the use of such Voldemort types as a "happy discovery". :)

No, the real irony is that it's Andrei who promoted them in the first place. :)

Heh. :)
 We _are_ finding some serious issues with them though (e.g. 
 they don't
 work with init and apparently can't work with init), and there 
 has
 been some discussion of ditching them due to such issues, but 
 no
 consensus has been reached on that.

Hmm, that's true. They can't possibly work with init: if you do something like: auto func(int x) { struct V { property int value() { return x; } } return V(); } auto v = func(123); auto u = v.init; // <-- what happens here? writeln(u.value); // <-- and here? It seems that we'd have to make .init illegal on these Voldemort structs. But that breaks consistency with the rest of the language that every type must have an .init value. Alternatively, .init can implicitly create a context where the value of x is set to int.init. But this is unnecessary (it's supposed to be a *Voldemort* type!) and very ugly (why are we creating a function's local context when it isn't even being called?), not to mention useless (u.value will return a nonsensical value). Or, perhaps a less intrusive workaround, is to have .init set the hidden context pointer to null, and you'll get a null deference when accessing u.value. Which is not pretty either, since no pointers or references are apparent in V.value; it's implicit. It seems that the only clean way to do this is to use a class instead of a struct, since the .init value will conveniently just be null, thereby sidestepping the problem. T

auto func(int x) { struct V { int value() { return x; } property V init() { return this; } } return V(); } auto v = func(123); auto u = v.init; // <-- what happens here? auto x = u.value; writeln(x); If you add an init property then it is not null. I'm not sure if this is the correct behavior as I just jumped into this discussion and I'm not sure if I understand exactly what the problem with these types of structs are. (as far as I can tell they simply let you pass "sets" of data from a function rather easily while also keeping the details hidden(impossible to directly instantiate the struct since it is hidden inside the func) In any case, when stepping through the code above, I noticed that init being called but resulting in u.value throwing a null exception. so I added an init property to V and then got the expected behavior. Probably too easy to be the correct solution but who knows ;)
Dec 16 2012
prev sibling next sibling parent "js.mdnq" <js_adddot+mdng gmail.com> writes:
As an alternative possible solution,

The following allows efficient nested structs in classes:

http://dpaste.dzfl.pl/64025e0a

It is very similar to the idea of voldemort structs except that 
instead of outer being a function it is a class.

Therefore, it makes sense that one could simply use functors in 
replace of the functions to get the same behavior(I'm not sure if 
D supports functors yet as I haven't got that far but I imagine 
it does).

To be useful, of course, one would have to use static functors so 
instantiation would not be necessary and direct replacement could 
be observed only if functor notation is identical to function 
notation.
Dec 16 2012
prev sibling parent kenji hara <k.hara.pg gmail.com> writes:
--f46d04016c2d6f440404d10222c5
Content-Type: text/plain; charset=UTF-8

I have recently added a note about "init property is sometimes unsafe".

https://github.com/D-Programming-Language/d-programming-language.org/pull/201


To reduce the risk, I have proposed an enhancement for .init property usage.

http://d.puremagic.com/issues/show_bug.cgi?id=8752

Kenji Hara

2012/12/16 js.mdnq <js_adddot+mdng gmail.com>

 On Saturday, 15 December 2012 at 20:20:10 UTC, H. S. Teoh wrote:

 On Sat, Dec 15, 2012 at 12:02:16PM -0800, Jonathan M Davis wrote:

 On Saturday, December 15, 2012 11:45:10 H. S. Teoh wrote:
 Ironically enough, Andrei in the subsequent paragraph > discourages
 the use of such nested structs, whereas Walter's article > promotes
 the use of such Voldemort types as a "happy discovery". :)

No, the real irony is that it's Andrei who promoted them in the first place. :)

Heh. :) We _are_ finding some serious issues with them though (e.g. they don't
 work with init and apparently can't work with init), and there has
 been some discussion of ditching them due to such issues, but no
 consensus has been reached on that.

Hmm, that's true. They can't possibly work with init: if you do something like: auto func(int x) { struct V { property int value() { return x; } } return V(); } auto v = func(123); auto u = v.init; // <-- what happens here? writeln(u.value); // <-- and here? It seems that we'd have to make .init illegal on these Voldemort structs. But that breaks consistency with the rest of the language that every type must have an .init value. Alternatively, .init can implicitly create a context where the value of x is set to int.init. But this is unnecessary (it's supposed to be a *Voldemort* type!) and very ugly (why are we creating a function's local context when it isn't even being called?), not to mention useless (u.value will return a nonsensical value). Or, perhaps a less intrusive workaround, is to have .init set the hidden context pointer to null, and you'll get a null deference when accessing u.value. Which is not pretty either, since no pointers or references are apparent in V.value; it's implicit. It seems that the only clean way to do this is to use a class instead of a struct, since the .init value will conveniently just be null, thereby sidestepping the problem. T

auto func(int x) { struct V { int value() { return x; } property V init() { return this; } } return V(); } auto v = func(123); auto u = v.init; // <-- what happens here? auto x = u.value; writeln(x); If you add an init property then it is not null. I'm not sure if this is the correct behavior as I just jumped into this discussion and I'm not sure if I understand exactly what the problem with these types of structs are. (as far as I can tell they simply let you pass "sets" of data from a function rather easily while also keeping the details hidden(impossible to directly instantiate the struct since it is hidden inside the func) In any case, when stepping through the code above, I noticed that init being called but resulting in u.value throwing a null exception. so I added an init property to V and then got the expected behavior. Probably too easy to be the correct solution but who knows ;)

--f46d04016c2d6f440404d10222c5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div>I have recently added a note about &quot;init property is sometimes un= safe&quot;.<br></div><div><br></div><div><a href=3D"https://github.com/D-Pr= ogramming-Language/d-programming-language.org/pull/201">https://github.com/= D-Programming-Language/d-programming-language.org/pull/201</a>=C2=A0<br> </div><div class=3D"gmail_extra"><br></div><div class=3D"gmail_extra">To re= duce the risk, I have proposed an enhancement for .init property usage.</di= v><div class=3D"gmail_extra"><br></div><div class=3D"gmail_extra"><a href= =3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D8752">http://d.puremagi= c.com/issues/show_bug.cgi?id=3D8752</a>=C2=A0<br> <br>Kenji Hara<br><br><div class=3D"gmail_quote">2012/12/16 js.mdnq <span d= ir=3D"ltr">&lt;<a href=3D"mailto:js_adddot+mdng gmail.com" target=3D"_blank= ">js_adddot+mdng gmail.com</a>&gt;</span><br><blockquote class=3D"gmail_quo= te" style=3D"margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:= 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-= style:solid;padding-left:1ex"> On Saturday, 15 December 2012 at 20:20:10 UTC, H. S. Teoh wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=3D"i= m"> On Sat, Dec 15, 2012 at 12:02:16PM -0800, Jonathan M Davis wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex"> On Saturday, December 15, 2012 11:45:10 H. S. Teoh wrote:<br> &gt; Ironically enough, Andrei in the subsequent paragraph &gt; discourages= <br> &gt; the use of such nested structs, whereas Walter&#39;s article &gt; prom= otes<br> &gt; the use of such Voldemort types as a &quot;happy discovery&quot;. :)<b= r> <br> No, the real irony is that it&#39;s Andrei who promoted them in the first<b= r> place. :)<br> </blockquote> <br></div> Heh. :)<div class=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;= margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color= :rgb(204,204,204);border-left-style:solid;padding-left:1ex"> We _are_ finding some serious issues with them though (e.g. they don&#39;t<= br> work with init and apparently can&#39;t work with init), and there has<br> been some discussion of ditching them due to such issues, but no<br> consensus has been reached on that.<br> </blockquote></div> [...]<div class=3D"im"><br> <br> Hmm, that&#39;s true. They can&#39;t possibly work with init: if you do<br> something like:<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto func(int x) {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 struct V {<br></div=
<div class=3D"im">

=A0 =C2=A0 property int value() { return x; }<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br></div><div cla= ss=3D"im"> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return V();<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto v =3D func(123);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto u =3D v.init; =C2=A0 =C2=A0 =C2=A0 =C2=A0/= / &lt;-- what happens here?<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 writeln(u.value); =C2=A0 =C2=A0 =C2=A0 // &lt;-= - and here?<br> <br> It seems that we&#39;d have to make .init illegal on these Voldemort<br> structs. But that breaks consistency with the rest of the language that<br> every type must have an .init value.<br> <br> Alternatively, .init can implicitly create a context where the value of<br> x is set to int.init. But this is unnecessary (it&#39;s supposed to be a<br=

l<br> context when it isn&#39;t even being called?), not to mention useless<br> (u.value will return a nonsensical value).<br> <br> Or, perhaps a less intrusive workaround, is to have .init set the hidden<br=

<br> u.value. Which is not pretty either, since no pointers or references are<br=

<br></div><div class=3D"im"> It seems that the only clean way to do this is to use a class instead of<br=

sidestepping the problem.<br> <br> <br> T<br> </div></blockquote> <br><div class=3D"im"> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto func(int x) {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 struct V {<br></div=

=A0 =C2=A0 int value() { return x; }<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 property V init() { return this; }<div class=3D"im"><br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br> <br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return V();<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto v =3D func(123);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto u =3D v.init; =C2=A0 =C2=A0 =C2=A0 =C2=A0/= / &lt;-- what happens here?<br></div> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto x =3D u.value;<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 writeln(x);<br> <br> If you add an init property then it is not null. I&#39;m not sure if this i= s the correct behavior as I just jumped into this discussion and I&#39;m no= t sure if I understand exactly what the problem with these types of structs= are. (as far as I can tell they simply let you pass &quot;sets&quot; of da= ta from a function rather easily while also keeping the details hidden(impo= ssible to directly instantiate the struct since it is hidden inside the fun= c)<br> <br> In any case, when stepping through the code above, I noticed that init bein= g called but resulting in u.value throwing a null exception. so I added an = init property to V and then got the expected behavior. Probably too easy to= be the correct solution but who knows ;)<br> </blockquote></div><br></div> --f46d04016c2d6f440404d10222c5--
Dec 16 2012