Error: cannot return non-void from void function
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Nov 27 13:14:57 PST 2014
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
More information about the Digitalmars-d-learn
mailing list