Jump to page: 1 2
Thread overview
return the other functions of the void main()
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
Rikki Cattermole
Apr 09, 2015
John Colvin
Apr 09, 2015
Rikki Cattermole
Apr 09, 2015
John Colvin
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
Jack Applegame
Apr 09, 2015
bearophile
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
John Colvin
Apr 09, 2015
Marc Schütz
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
John Colvin
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
Dennis Ritchie
Apr 09, 2015
Dennis Ritchie
Apr 10, 2015
Ivan Kazmenko
April 09, 2015
Hi,
Is it allowed in D similar designs?

void main() {
	import std.stdio;
	return writeln("Hello, world!");
}
April 09, 2015
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));
April 09, 2015
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.
April 09, 2015
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
April 09, 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");
}
April 09, 2015
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.
April 09, 2015
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.
April 09, 2015
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.
April 09, 2015
> 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");
}
April 09, 2015
Jack Applegame:

>   writeln(a.find(4).empty ? "No" : "Yes");

canFind?

Bye,
bearophile
« First   ‹ Prev
1 2