digitalmars.D.learn - Just one time
- Andrea Fontana (25/25) Oct 20 2015 It happens I need to perform an operation just one time (inside a
- John Colvin (7/32) Oct 20 2015 I think that's ok, because each unique (i.e. with a different
- Andrea Fontana (6/9) Oct 20 2015 This is the kind of thing I'm interested in.
- John Colvin (3/12) Oct 20 2015 It should work exactly the same across modules, just one instance
- =?UTF-8?Q?Ali_=c3=87ehreli?= (21/23) Oct 20 2015 An idea that uses a function pointer where the first step does its task
- Andrea Fontana (2/26) Oct 21 2015 Nice idea, but I can't "reuse" it :)
It happens I need to perform an operation just one time (inside a function, a loop...) I wonder if doing this it's a good idea or not. bool isFirstTime(alias T)() { static val = true; if (val) { val = false; return true; } return false; } So: void my_func() { if (isFirstTime!my_func) writeln("First call!"); else writeln("Just another call"); } myfunc(); // Print First call! myfunc(); // Print Just another call; Or (for example inside a for loop): assert(isFirstTime!"blah" == true); assert(isFirstTime!"blah" == false); Does it work as expected?
Oct 20 2015
On Tuesday, 20 October 2015 at 15:48:47 UTC, Andrea Fontana wrote:It happens I need to perform an operation just one time (inside a function, a loop...) I wonder if doing this it's a good idea or not. bool isFirstTime(alias T)() { static val = true; if (val) { val = false; return true; } return false; } So: void my_func() { if (isFirstTime!my_func) writeln("First call!"); else writeln("Just another call"); } myfunc(); // Print First call! myfunc(); // Print Just another call; Or (for example inside a for loop): assert(isFirstTime!"blah" == true); assert(isFirstTime!"blah" == false); Does it work as expected?I think that's ok, because each unique (i.e. with a different parameter) instantiation of isFirstTime will have its own static val. Be aware that there will be one instance of val per thread, so you are detecting the first run in each thread, not in the program overall.
Oct 20 2015
On Tuesday, 20 October 2015 at 15:55:47 UTC, John Colvin wrote:Be aware that there will be one instance of val per thread, so you are detecting the first run in each thread, not in the program overall.This is the kind of thing I'm interested in. What happens if I call isFirstTime!"test" across different modules? Does instatiation works in the same way for all compilers/platform? Other things to consider?
Oct 20 2015
On Tuesday, 20 October 2015 at 16:01:58 UTC, Andrea Fontana wrote:On Tuesday, 20 October 2015 at 15:55:47 UTC, John Colvin wrote:It should work exactly the same across modules, just one instance per thread. Same on all platforms and compilers.Be aware that there will be one instance of val per thread, so you are detecting the first run in each thread, not in the program overall.This is the kind of thing I'm interested in. What happens if I call isFirstTime!"test" across different modules? Does instatiation works in the same way for all compilers/platform? Other things to consider?
Oct 20 2015
On 10/20/2015 08:48 AM, Andrea Fontana wrote:It happens I need to perform an operation just one time (inside a function, a loop...)An idea that uses a function pointer where the first step does its task and then sets the stage for the following steps: import std.stdio; import std.range; import std.algorithm; void firstStep() { writeln("First call!"); takeAStep = &followingSteps; } void followingSteps() { writeln("Just another call"); } auto takeAStep = &firstStep; void main() { 3.iota.each!(_ => takeAStep()); } First call! Just another call Just another call Ali
Oct 20 2015
On Tuesday, 20 October 2015 at 18:08:33 UTC, Ali Çehreli wrote:On 10/20/2015 08:48 AM, Andrea Fontana wrote:Nice idea, but I can't "reuse" it :)It happens I need to perform an operation just one time (inside a function, a loop...)An idea that uses a function pointer where the first step does its task and then sets the stage for the following steps: import std.stdio; import std.range; import std.algorithm; void firstStep() { writeln("First call!"); takeAStep = &followingSteps; } void followingSteps() { writeln("Just another call"); } auto takeAStep = &firstStep; void main() { 3.iota.each!(_ => takeAStep()); } First call! Just another call Just another call Ali
Oct 21 2015