www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: cannot return non-void from void function

reply "Suliman" <evermind live.ru> writes:
auto parseConfig()
{
	
auto lines = File(txtlinks, "r").byLine;
	return lines;
}

Error: cannot return non-void from void function

I can't understand the reasons of the error;

Can I return auto from function?
Nov 27 2014
parent reply "Suliman" <evermind live.ru> writes:
Full function look like this:

auto parseConfig()
{
	auto config = Ini.Parse(getcwd ~ "\\" ~ "config.ini");
	string txtlinks = getcwd ~ "\\" ~ config.getKey("input_links");
	if(!exists(txtlinks))
	{
		writeln("Can't find input file with list of links.");
		return;
	}
	auto lines = File(txtlinks, "r").byLine;
	return lines;

}
Nov 27 2014
next sibling parent "Graham Fawcett" <fawcett uwindsor.ca> writes:
On Thursday, 27 November 2014 at 13:07:59 UTC, Suliman wrote:
 Full function look like this:

 auto parseConfig()
 {
 	auto config = Ini.Parse(getcwd ~ "\\" ~ "config.ini");
 	string txtlinks = getcwd ~ "\\" ~ config.getKey("input_links");
 	if(!exists(txtlinks))
 	{
 		writeln("Can't find input file with list of links.");
 		return;
 	}
 	auto lines = File(txtlinks, "r").byLine;
 	return lines;

 }
You have two return statements in your function. Each of them returns a result of a different type (the first one returns a "void" result). That's not allowed. Instead of writing "Can't find input file" and then returning, consider throwing an exception. Then you only have one return statement, and one return type. Graham
Nov 27 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 27 Nov 2014 13:07:58 +0000
Suliman via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 Full function look like this:
=20
 auto parseConfig()
 {
 	auto config =3D Ini.Parse(getcwd ~ "\\" ~ "config.ini");
 	string txtlinks =3D getcwd ~ "\\" ~ config.getKey("input_links");
 	if(!exists(txtlinks))
 	{
 		writeln("Can't find input file with list of links.");
 		return;
 	}
 	auto lines =3D File(txtlinks, "r").byLine;
 	return lines;
=20
 }
ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both "nothing" and "something". your first `return;` should either return something, or must be changed to throwing some exception.
Nov 27 2014
parent reply "Suliman" <evermind live.ru> writes:
 ah, that's it! as spec says, D determines function return value 
 from
 the first 'return' statement it seen. in your case this is 
 `return;`,
 so function return type is determined to be `void`.

 if you doing `auto` functions, try to arrange your code so the 
 first
 `return` returning the actual value.

 besides, your code is wrong anyway, 'cause you can't have 
 function that
 returns both "nothing" and "something". your first `return;` 
 should
 either return something, or must be changed to throwing some 
 exception.
How I can terminate program? First return I used to terminate app if config file is exists.
Nov 27 2014
next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 27 Nov 2014 17:22:35 +0000
Suliman via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

=20
 ah, that's it! as spec says, D determines function return value=20
 from
 the first 'return' statement it seen. in your case this is=20
 `return;`,
 so function return type is determined to be `void`.

 if you doing `auto` functions, try to arrange your code so the=20
 first
 `return` returning the actual value.

 besides, your code is wrong anyway, 'cause you can't have=20
 function that
 returns both "nothing" and "something". your first `return;`=20
 should
 either return something, or must be changed to throwing some=20
 exception.
=20 How I can terminate program? First return I used to terminate app=20 if config file is exists.
throw an exception. uncatched exception will terminate your program.
Nov 27 2014
prev sibling parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
On Thursday, 27 November 2014 at 17:22:36 UTC, Suliman wrote:
 ah, that's it! as spec says, D determines function return 
 value from
 the first 'return' statement it seen. in your case this is 
 `return;`,
 so function return type is determined to be `void`.

 if you doing `auto` functions, try to arrange your code so the 
 first
 `return` returning the actual value.

 besides, your code is wrong anyway, 'cause you can't have 
 function that
 returns both "nothing" and "something". your first `return;` 
 should
 either return something, or must be changed to throwing some 
 exception.
How I can terminate program? First return I used to terminate app if config file is exists.
You can use this: auto parseConfig() { string txtlinks = buildPath(getcwd,"notexist"); if(exists(txtlinks)) { auto lines = File(txtlinks, "r").byLine; return lines; } writeln("Can't find input file with list of links."); return typeof(return)(); } it works somehow, but it is not good way how to do it. Other way is use exit if(!exists(txtlinks)) { import core.runtime; import std.c.process; writeln("Can't find input file with list of links."); Runtime.terminate(); exit(1); } But best way is use throw exception if(!exists(txtlinks)) { throw new Exception("Can't find input file with list of links."); } and catch it somewhere from calling side
Nov 27 2014
next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 27 Nov 2014 21:14:57 +0000
Daniel Kozak via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 		import core.runtime;
 		import std.c.process;
 		writeln("Can't find input file with list of links.");
 		Runtime.terminate();
 		exit(1);
please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them "don't do what i just wrote". ;-)
Nov 27 2014
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
Dne Thu, 27 Nov 2014 22:21:52 +0100 ketmar via Digitalmars-d-learn  
<digitalmars-d-learn puremagic.com> napsal(a):

 On Thu, 27 Nov 2014 21:14:57 +0000
 Daniel Kozak via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 		import core.runtime;
 		import std.c.process;
 		writeln("Can't find input file with list of links.");
 		Runtime.terminate();
 		exit(1);
please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them "don't do what i just wrote". ;-)
I know, I just can't help myself :).
Nov 27 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 27 Nov 2014 22:25:10 +0100
Daniel Kozak via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 Dne Thu, 27 Nov 2014 22:21:52 +0100 ketmar via Digitalmars-d-learn =20
 <digitalmars-d-learn puremagic.com> napsal(a):
=20
 On Thu, 27 Nov 2014 21:14:57 +0000
 Daniel Kozak via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 		import core.runtime;
 		import std.c.process;
 		writeln("Can't find input file with list of links.");
 		Runtime.terminate();
 		exit(1);
please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them "don't do what i just wrote". ;-)
=20 I know, I just can't help myself :).
me too sometimes. taking into accout that "don't try this at home" warning is never working makes it even funnier. ;-)
Nov 27 2014