www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - return the other functions of the void main()

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Hi,
Is it allowed in D similar designs?

void main() {
	import std.stdio;
	return writeln("Hello, world!");
}
Apr 09 2015
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 9/04/2015 11:03 p.m., Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
      import std.stdio;
      return writeln("Hello, world!");
 }
Sure when: import std.traits : ReturnType; import std.stdio : writeln; static assert(is(ReturnType!writeln == int));
Apr 09 2015
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 9 April 2015 at 11:07:05 UTC, Rikki Cattermole wrote:
 On 9/04/2015 11:03 p.m., Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
     import std.stdio;
     return writeln("Hello, world!");
 }
Sure when: import std.traits : ReturnType; import std.stdio : writeln; static assert(is(ReturnType!writeln == int));
You might wanna check that :p
Apr 09 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 9/04/2015 11:22 p.m., John Colvin wrote:
 On Thursday, 9 April 2015 at 11:07:05 UTC, Rikki Cattermole wrote:
 On 9/04/2015 11:03 p.m., Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
     import std.stdio;
     return writeln("Hello, world!");
 }
Sure when: import std.traits : ReturnType; import std.stdio : writeln; static assert(is(ReturnType!writeln == int));
You might wanna check that :p
No no, its valid D code. After all, as long as the compiler doesn't assert, its perfectly valid. I was documenting the usage of return for main function's when the return type of it is int.
Apr 09 2015
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 9 April 2015 at 11:38:26 UTC, Rikki Cattermole wrote:
 On 9/04/2015 11:22 p.m., John Colvin wrote:
 On Thursday, 9 April 2015 at 11:07:05 UTC, Rikki Cattermole 
 wrote:
 On 9/04/2015 11:03 p.m., Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
    import std.stdio;
    return writeln("Hello, world!");
 }
Sure when: import std.traits : ReturnType; import std.stdio : writeln; static assert(is(ReturnType!writeln == int));
You might wanna check that :p
I was documenting the usage of return for main function's when the return type of it is int.
Ok... not sure how anyone was supposed to know that. The example given used void main. Also, the return type of writeln is void, so it's doubly confusing.
Apr 09 2015
prev sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 11:07:05 UTC, Rikki Cattermole wrote:
 Sure when:

 import std.traits : ReturnType;
 import std.stdio : writeln;

 static assert(is(ReturnType!writeln == int));
Thanks. On Thursday, 9 April 2015 at 11:09:43 UTC, John Colvin wrote:
 Yes, because writeln returns nothing, but why would you do 
 that? Just put the return on the next line, it's more readable. 
 Or, in the example above, just omit it entirely as the return 
 is implicit.
I quite often have to write similar designs: ----- import std.stdio; void main() { auto a = [ 1, 2, 3, 4, 5 ]; foreach (e; a) { if (e == 4) { writeln("Yes"); return; } } writeln("No"); } ----- But is not it easier to write :) import std.stdio; void main() { auto a = [ 1, 2, 3, 4, 5 ]; foreach (e; a) { if (e == 4) { return writeln("Yes"); } } writeln("No"); }
Apr 09 2015
parent reply "Jack Applegame" <japplegame gmail.com> writes:
 I quite often have to write similar designs:

 -----
 import std.stdio;

 void main() {

 	auto a = [ 1, 2, 3, 4, 5 ];

 	foreach (e; a) {
 		if (e == 4) {
 			writeln("Yes");
 			return;
 		}
 	}
 	
 	writeln("No");
 }
 -----

 But is not it easier to write :)

 import std.stdio;

 void main() {

 	auto a = [ 1, 2, 3, 4, 5 ];

 	foreach (e; a) {
 		if (e == 4) {
 			return writeln("Yes");
 		}
 	}
 	
 	writeln("No");
 }
import std.stdio; import std.algorithm; import std.array; void main() { auto a = [ 1, 2, 3, 4, 5 ]; writeln(a.find(4).empty ? "No" : "Yes"); }
Apr 09 2015
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Jack Applegame:

   writeln(a.find(4).empty ? "No" : "Yes");
canFind? Bye, bearophile
Apr 09 2015
prev sibling parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 12:57:26 UTC, Jack Applegame wrote:
 I quite often have to write similar designs:

 -----
 import std.stdio;

 void main() {

 	auto a = [ 1, 2, 3, 4, 5 ];

 	foreach (e; a) {
 		if (e == 4) {
 			writeln("Yes");
 			return;
 		}
 	}
 	
 	writeln("No");
 }
 -----

 But is not it easier to write :)

 import std.stdio;

 void main() {

 	auto a = [ 1, 2, 3, 4, 5 ];

 	foreach (e; a) {
 		if (e == 4) {
 			return writeln("Yes");
 		}
 	}
 	
 	writeln("No");
 }
import std.stdio; import std.algorithm; import std.array; void main() { auto a = [ 1, 2, 3, 4, 5 ]; writeln(a.find(4).empty ? "No" : "Yes"); }
import std.stdio; void main() { foreach (...) { foreach (...) { ... if (...) { ... return writeln("Yes"); } ... } ... } ... writeln("No"); } No design can be completely arbitrary: ----- import std.stdio; void main() { foreach (...) { foreach (...) { ... if (...) { ... return writeln("Yes"); } ... } ... } ... writeln("No"); }
Apr 09 2015
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 9 April 2015 at 11:04:00 UTC, Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
 	import std.stdio;
 	return writeln("Hello, world!");
 }
Yes, because writeln returns nothing, but why would you do that? Just put the return on the next line, it's more readable. Or, in the example above, just omit it entirely as the return is implicit.
Apr 09 2015
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Thursday, 9 April 2015 at 11:09:43 UTC, John Colvin wrote:
 On Thursday, 9 April 2015 at 11:04:00 UTC, Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
 	import std.stdio;
 	return writeln("Hello, world!");
 }
Yes, because writeln returns nothing, but why would you do that? Just put the return on the next line, it's more readable. Or, in the example above, just omit it entirely as the return is implicit.
It's useful when writing generic wrappers, where you just want to return whatever the wrapped function returns and don't want to treat void functions differently. I wouldn't use it in normal code, because it can be confusing, as `return` usually indicates that a value is indeed returned.
Apr 09 2015
prev sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 11:04:00 UTC, Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
 	import std.stdio;
 	return writeln("Hello, world!");
 }
It seems that you can not do so because writeln() something back, which leads to RUNTIME_ERROR in DMD 2.066.1: OK: ----- import std.conv; import std.stdio; import std.string; void main() { int n = readln.strip.to!int; string s = readln.strip; foreach (from; 0 .. n) foreach (jump; 1 .. n) { bool ok; foreach (i; 0 .. 5) { int pos = from + i * jump; if (pos >= n || s[pos] != '*') { ok = true; break; } } if (!ok) return write("yes"); } write("no"); } ----- http://codeforces.ru/contest/526/submission/10641745?locale=en RUNTIME_ERROR: ----- import std.conv; import std.stdio; import std.string; void main() { int n = readln.strip.to!int; string s = readln.strip; foreach (from; 0 .. n) foreach (jump; 1 .. n) { bool ok; foreach (i; 0 .. 5) { int pos = from + i * jump; if (pos >= n || s[pos] != '*') { ok = true; break; } } if (!ok) return writeln("yes"); } writeln("no"); } ----- http://codeforces.ru/contest/526/submission/10641695?locale=en
Apr 09 2015
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 9 April 2015 at 16:02:01 UTC, Dennis Ritchie wrote:
 On Thursday, 9 April 2015 at 11:04:00 UTC, Dennis Ritchie wrote:
 Hi,
 Is it allowed in D similar designs?

 void main() {
 	import std.stdio;
 	return writeln("Hello, world!");
 }
It seems that you can not do so because writeln() something back, which leads to RUNTIME_ERROR in DMD 2.066.1: OK: ----- import std.conv; import std.stdio; import std.string; void main() { int n = readln.strip.to!int; string s = readln.strip; foreach (from; 0 .. n) foreach (jump; 1 .. n) { bool ok; foreach (i; 0 .. 5) { int pos = from + i * jump; if (pos >= n || s[pos] != '*') { ok = true; break; } } if (!ok) return write("yes"); } write("no"); } ----- http://codeforces.ru/contest/526/submission/10641745?locale=en RUNTIME_ERROR: ----- import std.conv; import std.stdio; import std.string; void main() { int n = readln.strip.to!int; string s = readln.strip; foreach (from; 0 .. n) foreach (jump; 1 .. n) { bool ok; foreach (i; 0 .. 5) { int pos = from + i * jump; if (pos >= n || s[pos] != '*') { ok = true; break; } } if (!ok) return writeln("yes"); } writeln("no"); } ----- http://codeforces.ru/contest/526/submission/10641695?locale=en
Try running your code somewhere where you can actually see the output properly. RUNTIME_ERROR isn't something I recognise from D.
Apr 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 16:55:00 UTC, John Colvin wrote:
 Try running your code somewhere where you can actually see the 
 output properly. RUNTIME_ERROR isn't something I recognise from 
 D.
Operates a code normally, but still gives the error: http://ideone.com/kDHMk5
Apr 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 17:08:44 UTC, Dennis Ritchie wrote:
 Operates a code normally, but still gives the error:
 http://ideone.com/kDHMk5
I think it has something to do with Vindovs :) ----- http://dpaste.dzfl.pl/4c5bb9dd0ffa
Apr 09 2015
parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Thursday, 9 April 2015 at 17:38:42 UTC, Dennis Ritchie wrote:
 I think it has something to do with Vindovs :)
*Windows
Apr 09 2015
parent "Ivan Kazmenko" <gassa mail.ru> writes:
On Thursday, 9 April 2015 at 17:39:43 UTC, Dennis Ritchie wrote:
 On Thursday, 9 April 2015 at 17:38:42 UTC, Dennis Ritchie wrote:
 I think it has something to do with Vindovs :)
*Windows
Indeed, you can test it on Windows locally by running a .cmd file such as: ----- a.exe echo %ERRORLEVEL% ----- With dmd 2.066 (as on Codeforces), it gives me non-zero exit code. With dmd 2.067.0, the return code seems to always be zero.
Apr 10 2015