digitalmars.D.learn - Getting around the lack of foreach in CTFE
- Colin Grogan (6/6) Mar 28 2014 I'm interested to hear peoples methods for getting around the
- bearophile (7/12) Mar 28 2014 That's a good strategy.
- Adam D. Ruppe (5/7) Mar 28 2014 What, exactly are you trying to do? foreach does work in CTFE,
- Dicebot (26/33) Mar 28 2014 Looks like you mix the terminology here. CTFE is "Compile-Time
- Colin Grogan (14/49) Mar 28 2014 Yeah, I am mixing the two up. Not sure what I was thinking
- monarch_dodra (6/17) Mar 28 2014 Did you try looking into "std.getopt"? It's pretty much the same
- Colin Grogan (12/32) Mar 28 2014 I did, and use it a good bit.
I'm interested to hear peoples methods for getting around the lack of foreach loops while using CTFE? Currently, I've been using a lot of recursive templates, but its beginning to give me a headache and I'd like to see if there's better ways around it. On a related note, is there plans to add foreach support to CTFE?
Mar 28 2014
Colin Grogan:Currently, I've been using a lot of recursive templates, but its beginning to give me a headache and I'd like to see if there's better ways around it.That's a good strategy. In some cases you can use Iota: https://d.puremagic.com/issues/show_bug.cgi?id=4085On a related note, is there plans to add foreach support to CTFE?Yes, it's being worked on now by Timon. See the latest DIP. Bye, bearophile
Mar 28 2014
On Friday, 28 March 2014 at 11:59:47 UTC, Colin Grogan wrote:I'm interested to hear peoples methods for getting around the lack of foreach loops while using CTFE?What, exactly are you trying to do? foreach does work in CTFE, though it will need to be wrapped in helper function that returns a regular value which is sometimes enough to get a lot done and sometimes not.
Mar 28 2014
On Friday, 28 March 2014 at 11:59:47 UTC, Colin Grogan wrote:I'm interested to hear peoples methods for getting around the lack of foreach loops while using CTFE? Currently, I've been using a lot of recursive templates, but its beginning to give me a headache and I'd like to see if there's better ways around it. On a related note, is there plans to add foreach support to CTFE?Looks like you mix the terminology here. CTFE is "Compile-Time Function Evaluation" and means exactly that - interpretation of normal D function during compile-time. Of course, you can use foreach as usual inside those. You post implies that you in fact ask about generic compile-time algorithms over compile-time argument lists, which is _not_ the same as CTFE. Indeed, lack of static declarative foreach causes lot of pain during meta-programming. Relatively common pattern to avoid recursive template horror is to use private CTFE function inside the template: template Stuff(T...) { private string doStuff() { string result; foreach (Index, Arg; T) { // compute value or generate D code } return result; } enum Stuff = doStuff(); // ..or for complex code-generating things: mixin(doStuff()); }
Mar 28 2014
On Friday, 28 March 2014 at 13:49:59 UTC, Dicebot wrote:On Friday, 28 March 2014 at 11:59:47 UTC, Colin Grogan wrote:Yeah, I am mixing the two up. Not sure what I was thinking before! Thanks for clarifying. Im trying to parse command line args and then build a struct that will, at run-time, hold the data the user passed in via command line args. Very similar to Pythons docopt utility -> https://github.com/docopt/docopt Up till now, I've been using templates to do all of the work, but now that I realise I can do it all with functions I might try that. Then use a mixin template to create the struct with whatever data is required. Thanks. I'm sure I'll be back to bombard ye with questions soon!I'm interested to hear peoples methods for getting around the lack of foreach loops while using CTFE? Currently, I've been using a lot of recursive templates, but its beginning to give me a headache and I'd like to see if there's better ways around it. On a related note, is there plans to add foreach support to CTFE?Looks like you mix the terminology here. CTFE is "Compile-Time Function Evaluation" and means exactly that - interpretation of normal D function during compile-time. Of course, you can use foreach as usual inside those. You post implies that you in fact ask about generic compile-time algorithms over compile-time argument lists, which is _not_ the same as CTFE. Indeed, lack of static declarative foreach causes lot of pain during meta-programming. Relatively common pattern to avoid recursive template horror is to use private CTFE function inside the template: template Stuff(T...) { private string doStuff() { string result; foreach (Index, Arg; T) { // compute value or generate D code } return result; } enum Stuff = doStuff(); // ..or for complex code-generating things: mixin(doStuff()); }
Mar 28 2014
On Friday, 28 March 2014 at 14:42:54 UTC, Colin Grogan wrote:Im trying to parse command line args and then build a struct that will, at run-time, hold the data the user passed in via command line args. Very similar to Pythons docopt utility -> https://github.com/docopt/docopt Up till now, I've been using templates to do all of the work, but now that I realise I can do it all with functions I might try that. Then use a mixin template to create the struct with whatever data is required. Thanks. I'm sure I'll be back to bombard ye with questions soon!Did you try looking into "std.getopt"? It's pretty much the same thing. It doesn't quite do the "useage/help" thing automatically though (yet). But, IMO, that's trivial to implement. It's the parsing of arguments you don't want to rewrite. That's complicated.
Mar 28 2014
On Friday, 28 March 2014 at 15:39:48 UTC, monarch_dodra wrote:On Friday, 28 March 2014 at 14:42:54 UTC, Colin Grogan wrote:I did, and use it a good bit. I'm kind of doing this as an exercise in learning, though I do think the using docopt is a bit easier than using std.getopt, so it may be of use once its done too. The trouble I find with getopt is that you have to write a section at the start of your main function every time, and then you need to write help text aswell. This is all in one, and hopefully will be called with one function. What I'm working towards is: auto parsed = docopt!helpText(args); Anyway, this is a bit off topic :)Im trying to parse command line args and then build a struct that will, at run-time, hold the data the user passed in via command line args. Very similar to Pythons docopt utility -> https://github.com/docopt/docopt Up till now, I've been using templates to do all of the work, but now that I realise I can do it all with functions I might try that. Then use a mixin template to create the struct with whatever data is required. Thanks. I'm sure I'll be back to bombard ye with questions soon!Did you try looking into "std.getopt"? It's pretty much the same thing. It doesn't quite do the "useage/help" thing automatically though (yet). But, IMO, that's trivial to implement. It's the parsing of arguments you don't want to rewrite. That's complicated.
Mar 28 2014