Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 20, 2015 Just one time | ||||
---|---|---|---|---|
| ||||
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? |
October 20, 2015 Re: Just one time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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.
|
October 20, 2015 Re: Just one time | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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?
|
October 20, 2015 Re: Just one time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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:
>> 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?
It should work exactly the same across modules, just one instance per thread. Same on all platforms and compilers.
|
October 20, 2015 Re: Just one time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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
|
October 21, 2015 Re: Just one time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 20 October 2015 at 18:08:33 UTC, Ali Çehreli wrote:
> 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
Nice idea, but I can't "reuse" it :)
|
Copyright © 1999-2021 by the D Language Foundation