www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alias this and std.traits.isArray

reply David <d dav1d.de> writes:
This code worked with dmd 2.060:

import std.stdio;
import std.traits;

struct OhWhy(S) {
    S[] arr;
    alias arr this;
}

void main() {
	static assert(isArray!(OhWhy!(float)));
	
}

But fails with dmd 2.061:
ss.d(10): Error: static assert  (isArray!(OhWhy!(float))) is false

(same happens with alias this to static arrays and isArray/isStaticArray)


Is this intended or a regression? If latter, I'll submit a bug-ticket.
Jan 05 2013
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 5 January 2013 at 14:43:49 UTC, David wrote:
 This code worked with dmd 2.060:

 import std.stdio;
 import std.traits;

 struct OhWhy(S) {
     S[] arr;
     alias arr this;
 }

 void main() {
 	static assert(isArray!(OhWhy!(float)));
 	
 }

 But fails with dmd 2.061:
 ss.d(10): Error: static assert  (isArray!(OhWhy!(float))) is 
 false

 (same happens with alias this to static arrays and 
 isArray/isStaticArray)


 Is this intended or a regression? If latter, I'll submit a 
 bug-ticket.

All traits in std trait of the type were explicitly changed to return true only if *the exact type* meets the traits requirement. The rationale is simply tha OhWhy isn't an array. It can be implicitly cast to array, but it isn't an array. This is problematic when instanciating template functions with automatic type inference: when you write "myTemplateFunction(myOhWhy)", then you will instanciate "myTemplateFuction!OhWhy" when what you may have wanted was to actually call "myTemplateFunction!(S[])". The new "isArray" definition protects from that.
Jan 05 2013
parent reply David <d dav1d.de> writes:
Thanks for your answers, but:

 Exactly. The std.traits traits are testing for exact types. If you want to 
 test for implicit conversion, then use the : operator. e.g.
 
     static assert(is(OhWhy!float : float[]));

"OyWhy" is a templated struct for a reason, I have no idea which type of array is "alias this'ed" is there a way I can check if it's implicitly convertable to any array type with some `is` magic I am not aware of?
Jan 06 2013
parent David <d dav1d.de> writes:
 Like this?
 
 struct MyType(Elem)
 {
     Elem[] inner;
     alias inner this;
 }
 
 void main()
 {
     static assert(is(MyType!float _ : T[], T));
 }
 

And how does that work for a static array?
Jan 09 2013
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, January 05, 2013 22:57:51 monarch_dodra wrote:
 On Saturday, 5 January 2013 at 14:43:49 UTC, David wrote:
 This code worked with dmd 2.060:
 
 import std.stdio;
 import std.traits;
 
 struct OhWhy(S) {
 
     S[] arr;
     alias arr this;
 
 }
 
 void main() {
 
 	static assert(isArray!(OhWhy!(float)));
 
 }
 
 But fails with dmd 2.061:
 ss.d(10): Error: static assert  (isArray!(OhWhy!(float))) is
 false
 
 (same happens with alias this to static arrays and
 isArray/isStaticArray)
 
 
 Is this intended or a regression? If latter, I'll submit a
 bug-ticket.

All traits in std trait of the type were explicitly changed to return true only if *the exact type* meets the traits requirement. The rationale is simply tha OhWhy isn't an array. It can be implicitly cast to array, but it isn't an array. This is problematic when instanciating template functions with automatic type inference: when you write "myTemplateFunction(myOhWhy)", then you will instanciate "myTemplateFuction!OhWhy" when what you may have wanted was to actually call "myTemplateFunction!(S[])". The new "isArray" definition protects from that.

Exactly. The std.traits traits are testing for exact types. If you want to test for implicit conversion, then use the : operator. e.g. static assert(is(OhWhy!float : float[])); - Jonathan M Davis
Jan 05 2013
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--20cf30334c5b9d9bf704d29ecc98
Content-Type: text/plain; charset=UTF-8

On Sun, Jan 6, 2013 at 1:36 PM, David <d dav1d.de> wrote:

 Thanks for your answers, but:

 Exactly. The std.traits traits are testing for exact types. If you want

 test for implicit conversion, then use the : operator. e.g.

     static assert(is(OhWhy!float : float[]));

"OyWhy" is a templated struct for a reason, I have no idea which type of array is "alias this'ed" is there a way I can check if it's implicitly convertable to any array type with some `is` magic I am not aware of?

struct MyType(Elem) { Elem[] inner; alias inner this; } void main() { static assert(is(MyType!float _ : T[], T)); } --20cf30334c5b9d9bf704d29ecc98 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sun, Jan 6, 2013 at 1:36 PM, David <s= pan dir=3D"ltr">&lt;<a href=3D"mailto:d dav1d.de" target=3D"_blank">d dav1d= .de</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"mar= gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Thanks for your answers, but:<br> <div class=3D"im"><br> &gt; Exactly. The std.traits traits are testing for exact types. If you wan= t to<br> &gt; test for implicit conversion, then use the : operator. e.g.<br> &gt;<br> &gt; =C2=A0 =C2=A0 static assert(is(OhWhy!float : float[]));<br> <br> </div>&quot;OyWhy&quot; is a templated struct for a reason, I have no idea = which type of<br> array is &quot;alias this&#39;ed&quot; is there a way I can check if it&#39= ;s implicitly<br> convertable to any array type with some `is` magic I am not aware of?<br> <br> </blockquote></div><br><div>Like this?</div><div><br></div><div><div><div>s= truct MyType(Elem)</div><div>{</div><div>=C2=A0 =C2=A0 Elem[] inner;</div><= div>=C2=A0 =C2=A0 alias inner this;</div><div>}</div><div><br></div><div>vo= id main()</div> <div>{</div><div>=C2=A0 =C2=A0 static assert(is(MyType!float _ : T[], T));<= /div><div>}</div></div></div><div><br></div> --20cf30334c5b9d9bf704d29ecc98--
Jan 06 2013
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Wed, Jan 9, 2013 at 10:57 PM, David <d dav1d.de> wrote:
 Like this?

 struct MyType(Elem)
 {
     Elem[] inner;
     alias inner this;
 }

 void main()
 {
     static assert(is(MyType!float _ : T[], T));
 }

And how does that work for a static array?

For a static array, the generic type is T[n], for some T and a size_t n. So: static assert(is(MyType!float _ : T[n], T, size_t n));
Jan 09 2013