www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Alias sleep(int) for Thread.sleep(dur!("seconds")( int ));

reply Marcone <marcone email.com> writes:
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.
Nov 12 2019
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Nov 12 2019
prev sibling next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
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); }
Nov 12 2019
next sibling parent Marcone <marcone email.com> writes:
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!
Nov 12 2019
prev sibling parent reply Marcone <marcone email.com> writes:
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!
Nov 12 2019
parent Daniel Kozak <kozzi11 gmail.com> writes:
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
Nov 12 2019
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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
Nov 13 2019