www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - method has no return statement with switch

reply crimaniak <crimaniak gmail.com> writes:
Hi!

I have the error message:
source/url.cache.d(20,16): Error: function 
url.Cache.UrlCache.doRequest has no return statement, but is 
expected to return a value of type string

Inserting dummy return statement doesn't help. final switch / 
switch with default - no matter.

As I understand compiler must detect when end of function is 
unreachable (and in fact it detects it - see comment about return 
""; line) and do not try to check for return value. Is this my or 
compiler's error here?

dmd --version
DMD64 D Compiler v2.069.0 Copyright (c) 1999-2015 by Digital Mars written by Walter Bright [code] module url.Cache; import std.conv; import core.exception; import mysql.d; import std.digest.md; import std.net.curl; enum Method { GET="GET", POST="POST" } class UrlCache { // ... public string doRealRequest(string url, Method method) { final switch(method) { case Method.GET: return std.net.curl.get!AutoProtocol(url).text; case Method.POST: return std.net.curl.post(url, []).text; } // return ""; // produces 'statement is not reachable' warning, don't fix the problem } // ... } [/code]
Nov 06 2015
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 Inserting dummy return statement doesn't help. final switch / 
 switch with default - no matter.
Try inserting assert(0); instead of a dummy return.
Nov 06 2015
parent reply crimaniak <crimaniak gmail.com> writes:
On Saturday, 7 November 2015 at 00:27:02 UTC, Adam D. Ruppe wrote:
 On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 Inserting dummy return statement doesn't help. final switch / 
 switch with default - no matter.
Try inserting assert(0); instead of a dummy return.
Done, no difference.
Nov 06 2015
parent BBaz <bb.temp gmx.com> writes:
On Saturday, 7 November 2015 at 00:30:29 UTC, crimaniak wrote:
 On Saturday, 7 November 2015 at 00:27:02 UTC, Adam D. Ruppe 
 wrote:
 On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 Inserting dummy return statement doesn't help. final switch / 
 switch with default - no matter.
Try inserting assert(0); instead of a dummy return.
Done, no difference.
Wow, that impossible. You switch is well final. here DMD 2.068, linux x86_64 the folling compiles and runs: ---- enum Method { GET="GET", POST="POST" } class UrlCache { public string doRealRequest(string url, Method method) { final switch(method) { case Method.GET: return std.net.curl.get!AutoProtocol(url).idup; case Method.POST: return std.net.curl.post(url, []).idup; } } } void main() {auto test = new UrlCache;} ---- are you sure that the error you get doesnt come from another location ?!
Nov 06 2015
prev sibling next sibling parent tcak <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 Hi!

 I have the error message:
 source/url.cache.d(20,16): Error: function 
 url.Cache.UrlCache.doRequest has no return statement, but is 
 expected to return a value of type string

 [...]
Because the "switch" is marked as "final", eventually one of cases will be followed. Because both cases have a "return" point, code will never get out of switch statement. So the compiler acts correctly.
Nov 06 2015
prev sibling parent reply BBaz <bb.temp gmx.com> writes:
On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 [...]
 url.Cache.UrlCache.doRequest has no return statement, but is 
 expected to return a value of type string
 [...]
 	public string doRealRequest(string url, Method method)
You posted the wrong code sample: your code shows doRealRequest but the message is about doRequest !
Nov 06 2015
parent crimaniak <crimaniak gmail.com> writes:
On Saturday, 7 November 2015 at 06:02:49 UTC, BBaz wrote:
 On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:
 [...]
 url.Cache.UrlCache.doRequest has no return statement, but is 
 expected to return a value of type string
 [...]
 	public string doRealRequest(string url, Method method)
You posted the wrong code sample: your code shows doRealRequest but the message is about doRequest !
Yes! It's just my inattention. Thanks! p.s. Don't program at 3 am
Nov 07 2015