www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic length string array at compile time?

reply Dany12L <dany12719l gmail.com> writes:
Hi, I'd be interested to know if it's possible to have a dynamic 
length array containing strings generated at compile time.
Jun 05 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/5/23 11:45 AM, Dany12L wrote:
 Hi, I'd be interested to know if it's possible to have a dynamic length 
 array containing strings generated at compile time.
 
 
Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it. -Steve
Jun 06 2023
parent reply Dany12L <dany12719l gmail.com> writes:
On Tuesday, 6 June 2023 at 14:26:01 UTC, Steven Schveighoffer 
wrote:
 On 6/5/23 11:45 AM, Dany12L wrote:
 Hi, I'd be interested to know if it's possible to have a 
 dynamic length array containing strings generated at compile 
 time.
 
 
Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it. -Steve
My concern would be to create an immutable array (or other "equivalent") of strings by adding elements to the array at compile time from different modules. I would like to know if it is possible to do something similar
Jun 06 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/6/23 4:38 PM, Dany12L wrote:
 On Tuesday, 6 June 2023 at 14:26:01 UTC, Steven Schveighoffer wrote:
 On 6/5/23 11:45 AM, Dany12L wrote:
 Hi, I'd be interested to know if it's possible to have a dynamic 
 length array containing strings generated at compile time.
Sure. Just do it. Probably the reason nobody answered the question yet is that it trivially works if you try it :) Unless you did try it and it didn't work? In that case, post your code, I'm sure we can fix it.
My concern would be to create an immutable array (or other "equivalent") of strings by adding elements to the array at compile time from different modules.
OK, so you want to affect the same static variable from multiple places? No, that can't happen that way. You can do it in a functional programming fashion, but there can only be one initialization, you can't edit the thing afterwards. So for instance, you can do: ```d immutable string[] strs = mod1.makeStrings() ~ mod2.makeStrings() ~ ...; ``` In a centralized place. -Steve
Jun 06 2023
parent reply Mark Fisher <logicfish gmail.com> writes:
On Wednesday, 7 June 2023 at 00:18:54 UTC, Steven Schveighoffer 
wrote:
 So for instance, you can do:

 ```d
 immutable string[] strs = mod1.makeStrings() ~ 
 mod2.makeStrings() ~ ...;
 ```

 In a centralized place.

 -Steve
Hi, So what if I wanted to build the strings within my library but have the client append their own data, for example if I want to have declarative extensions? Could this be done by using a mixin template to create the strings in the client?
Jun 15 2023
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/15/23 9:59 PM, Mark Fisher wrote:
 Hi,
 So what if I wanted to build the strings within my library but have the 
 client append their own data, for example if I want to have declarative 
 extensions? Could this be done by using a mixin template to create the 
 strings in the client?
Well, as long as it's not *compile time*, but runtime, you can definitely do it. Just use shared static constructors. As far as I know, there's no way to have the compiler build a compile-time list of all compiled modules to be used to build such a thing. You might be able to do it as a pre-build step. One thing you could do is have a convention where some "master" module that knows all of the important modules to includes passes it to some mixin to instantiate. -Steve
Jun 15 2023