www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static arrays at runtime with templates ?

reply Emil <emilper gmail.com> writes:
I tried this on a whim and it appears to work:

auto static_array(T, size_t data_size)()
{
     T[data_size] data;
     return data;
}

void main() {
     import std.stdio: writeln;
     import std.traits: isStaticArray;

     auto data = static_array!(int, 5)(3);
     writeln(data);
     static assert(__traits(isStaticArray, data));

     auto data_longer = static_array!(int, 6)(2);
     writeln(data_longer);
     static assert(__traits(isStaticArray, data_longer));
}


Is this for real, static arrays at runtime without manually 
allocating memory ? Is this legitimate or should I expect 
problems ?
Feb 03 2019
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 3 February 2019 at 16:33:48 UTC, Emil wrote:
 I tried this on a whim and it appears to work:

 auto static_array(T, size_t data_size)()
 {
     T[data_size] data;
     return data;
 }
This is actually already in the standard library: https://dlang.org/phobos/std_array.html#staticArray
Feb 03 2019
parent Emil <emilper gmail.com> writes:
On Sunday, 3 February 2019 at 16:36:30 UTC, Paul Backus wrote:
...
 This is actually already in the standard library:

 https://dlang.org/phobos/std_array.html#staticArray
thank you either I already read that and forgot or finally I'm starting to get the feel of the language :)
Feb 03 2019
prev sibling parent reply XavierAP <n3minis-git yahoo.es> writes:
On Sunday, 3 February 2019 at 16:33:48 UTC, Emil wrote:
 Is this for real, static arrays at runtime without manually 
 allocating memory ? Is this legitimate or should I expect 
 problems ?
Static arrays are always allocated at run-time. It's the size of the array that must be known at compile-time (in this case via a template parameter). What's the advantage (or the essential difference) of auto data = static_array!(int, 5); instead of int[5] data; ? Just asking ;) but it's good to play. What does not compile on my end are the run-time parameters (3) and (2)...?
Feb 04 2019
parent reply Emil <emilper gmail.com> writes:
On Monday, 4 February 2019 at 16:08:38 UTC, XavierAP wrote:
 Static arrays are always allocated at run-time. It's the size 
 of the array that must be known at compile-time (in this case 
 via a template parameter).

 What's the advantage (or the essential difference) of
     auto data = static_array!(int, 5);
 instead of
     int[5] data;
 ? Just asking ;) but it's good to play.

 What does not compile on my end are the run-time parameters (3) 
 and (2)...?
you are correct ... 24 hours of being very satisfied with myself were good, but also good thing I did not quit and start looking for a D job Can std.array.staticArray build static arrays with size known only at run time ? Now that I am not overcome with enthousiasm it looks like it too needs to know the size.
Feb 04 2019
next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Feb 04, 2019 at 07:14:38PM +0000, Emil via Digitalmars-d-learn wrote:
[...]
 Can std.array.staticArray build static arrays with size known only at
 run time ? Now that I am not overcome with enthousiasm it looks like
 it too needs to know the size.
That's not possible, because the size of a static array is part of its type, meaning it must be known at compile-time. Perhaps what you're looking for is alloca (and possibly a slice over that)? T -- In a world without fences, who needs Windows and Gates? -- Christian Surchi
Feb 04 2019
prev sibling parent reply XavierAP <n3minis-git yahoo.es> writes:
On Monday, 4 February 2019 at 19:14:38 UTC, Emil wrote:
 Can std.array.staticArray build static arrays with size known 
 only at run time ? Now that I am not overcome with enthousiasm 
 it looks like it too needs to know the size.
A static array's size must be known (or computed) at compile time, by definition... There can be no exception; staticArray() gets or infers the size from its template/compile-time arguments. For one, the trivial example on dlang.org/phobos auto a = [0, 1].staticArray; static assert(is(typeof(a) == int[2])); is equivalent to int[2] a = [0, 1]; static assert(is(typeof(a) == int[2])); Is it better? Depends on.....? No it really isn't... However one of the possibilities of D is the ability to generate and execute quite some code at compile time by means of meta-programming; and I guess here's where staticArray() may end up being useful enough to have merited introduction into the std library; not for trivial uses where a straightforward T[n] declaration is preferable, being possible... If there is something you want to solve in a program of yours, there may be another way, as H.S. Teoh suggests. If you're just trying out stuff it's also fine, :) there's still more.
Feb 04 2019
next sibling parent Emil <emilper gmail.com> writes:
On Monday, 4 February 2019 at 22:48:10 UTC, XavierAP wrote:
...
 If there is something you want to solve in a program of yours, 
 there may be another way, as H.S. Teoh suggests. If you're just 
 trying out stuff it's also fine, :) there's still more.
Since August last year I try to read or write a bit of D code every day. I'm still trying out stuff and going through the books. In this case I was way too excited about passing both types and data to a template and having it compile the first time I tried, and jumped to conclusions. Thank you for pointing where my confusion was.
Feb 05 2019
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Monday, 4 February 2019 at 22:48:10 UTC, XavierAP wrote:
 However one of the possibilities of D is the ability to 
 generate and execute quite some code at compile time by means 
 of meta-programming; and I guess here's where staticArray() may 
 end up being useful enough to have merited introduction into 
 the std library; not for trivial uses where a straightforward 
 T[n] declaration is preferable, being possible...
The main benefit of staticArray is that it allows the length of the array to be inferred from the initializer (like in C).
Feb 05 2019