Alias this and std.traits.isArray

Jonathan M Davis jmdavisProg at gmx.com
Sat Jan 5 16:38:47 PST 2013


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


More information about the Digitalmars-d mailing list