return the other functions of the void main()

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 9 04:25:28 PDT 2015


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


More information about the Digitalmars-d-learn mailing list