digitalmars.D - Alias this and std.traits.isArray
- David (15/15) Jan 05 2013 This code worked with dmd 2.060:
- monarch_dodra (11/29) Jan 05 2013 All traits in std trait of the type were explicitly changed to
- Jonathan M Davis (5/48) Jan 05 2013 Exactly. The std.traits traits are testing for exact types. If you want ...
- David (4/8) Jan 06 2013 "OyWhy" is a templated struct for a reason, I have no idea which type of
- Philippe Sigaud (11/20) Jan 06 2013 Like this?
- David (1/14) Jan 09 2013 And how does that work for a static array?
- Philippe Sigaud (3/17) Jan 09 2013 For a static array, the generic type is T[n], for some T and a size_t n....
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
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
On Saturday, January 05, 2013 22:57:51 monarch_dodra wrote:On Saturday, 5 January 2013 at 14:43:49 UTC, David wrote: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 DavisThis 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
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
On Sun, Jan 6, 2013 at 1:36 PM, David <d dav1d.de> wrote:Thanks for your answers, but:Like this? struct MyType(Elem) { Elem[] inner; alias inner this; } void main() { static assert(is(MyType!float _ : T[], T)); }Exactly. The std.traits traits are testing for exact types. If you wanttotest 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
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
On Wed, Jan 9, 2013 at 10:57 PM, David <d dav1d.de> wrote: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));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