Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-13 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-12 Thread Daniel Kozak via Digitalmars-d-learn
On Tue, Nov 12, 2019 at 11:50 PM Marcone via Digitalmars-d-learn
 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


Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-12 Thread Marcone via Digitalmars-d-learn

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!


Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-12 Thread Marcone via Digitalmars-d-learn

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!


Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-12 Thread Daniel Kozak via Digitalmars-d-learn

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);
}


Re: Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

2019-11-12 Thread Jonathan M Davis via Digitalmars-d-learn
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