Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
November 12, 2019 Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
I am using this function to sleep, but I want a simple Alias. How can I alias this? // Function sleep(int) void sleep(int seconds){ Thread.sleep(dur!("seconds")( seconds )); } sleep(1); // Using function. |
November 12, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Tuesday, November 12, 2019 2:24:54 PM MST Marcone via Digitalmars-d-learn wrote:
> I am using this function to sleep, but I want a simple Alias. How can I alias this?
>
> // Function sleep(int)
> void sleep(int seconds){
> Thread.sleep(dur!("seconds")( seconds ));
> }
>
> sleep(1); // Using function.
An alias just gives a different name for a symbol. It can't pass arguments for you or call a function and pass its result to another. So, while you could create an alias for Thread.sleep, you'd have to call it exactly like you'd call Thread.sleep - just with a different name. If you want to do something like have it accept an int instead of a Duration, then you need a wrapper function like you're already doing.
Now, in core.time, dur!"seconds" has an alias named seconds, so if you're
simply looking to reduce how much typing you're doing, you could have
Thread.sleep(seconds(42)), or if you aliased Thread.sleep to sleep, you
could do sleep(seconds(42)), but you can't do something like sleep(42)
without a wrapper function.
In any case, I'd suggest that you avoid passing around naked integers as time values. Thread.sleep takes a Duration specifically because it makes for clearer code and is less error-prone than using a naked integer (since a naked integer has no units).
- Jonathan M Davis
|
November 12, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Tuesday, 12 November 2019 at 21:24:54 UTC, Marcone wrote:
> I am using this function to sleep, but I want a simple Alias. How can I alias this?
>
> // Function sleep(int)
> void sleep(int seconds){
> Thread.sleep(dur!("seconds")( seconds ));
> }
>
> sleep(1); // Using function.
You can do this:
import std.functional;
import core.time;
import core.thread;
alias sleep = compose!(Thread.sleep, dur!("seconds"));
void main()
{
sleep(10);
}
|
November 12, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Tuesday, 12 November 2019 at 22:26:48 UTC, Daniel Kozak wrote:
> On Tuesday, 12 November 2019 at 21:24:54 UTC, Marcone wrote:
>> I am using this function to sleep, but I want a simple Alias. How can I alias this?
>>
>> // Function sleep(int)
>> void sleep(int seconds){
>> Thread.sleep(dur!("seconds")( seconds ));
>> }
>>
>> sleep(1); // Using function.
>
> You can do this:
>
> import std.functional;
> import core.time;
> import core.thread;
>
> alias sleep = compose!(Thread.sleep, dur!("seconds"));
>
> void main()
> {
> sleep(10);
> }
Daniel Kozak Thank you very much! It works very well!
|
November 12, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | On Tuesday, 12 November 2019 at 22:26:48 UTC, Daniel Kozak wrote: > On Tuesday, 12 November 2019 at 21:24:54 UTC, Marcone wrote: >> I am using this function to sleep, but I want a simple Alias. How can I alias this? >> >> // Function sleep(int) >> void sleep(int seconds){ >> Thread.sleep(dur!("seconds")( seconds )); >> } >> >> sleep(1); // Using function. > > You can do this: > > import std.functional; > import core.time; > import core.thread; > > alias sleep = compose!(Thread.sleep, dur!("seconds")); > > void main() > { > sleep(10); > } Can you make Alias for: task!func().executeInNewThread(); Thank you! |
November 13, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Tue, Nov 12, 2019 at 11:50 PM Marcone via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > > > Can you make Alias for: > task!func().executeInNewThread(); > > Thank you! AFAIK that is not possible without some wrapper because executeInNewThread is member function of Task so it will need this reference for object |
November 13, 2019 Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int )); | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On 11/12/19 4:24 PM, Marcone wrote:
> I am using this function to sleep, but I want a simple Alias. How can I alias this?
>
> // Function sleep(int)
> void sleep(int seconds){
> Thread.sleep(dur!("seconds")( seconds ));
> }
>
> sleep(1); // Using function.
Thread.sleep(1.seconds); // not that bad imo.
You can also alias sleep into your namespace to avoid having to type Thread:
alias sleep = Thread.sleep;
sleep(1.seconds);
-Steve
|
Copyright © 1999-2021 by the D Language Foundation