digitalmars.D.learn - method has no return statement with switch
- crimaniak (39/40) Nov 06 2015 Hi!
- Adam D. Ruppe (2/4) Nov 06 2015 Try inserting assert(0); instead of a dummy return.
- crimaniak (2/6) Nov 06 2015 Done, no difference.
- BBaz (23/32) Nov 06 2015 Wow, that impossible. You switch is well final.
- tcak (5/11) Nov 06 2015 Because the "switch" is marked as "final", eventually one of
- BBaz (3/8) Nov 06 2015 You posted the wrong code sample: your code shows doRealRequest
- crimaniak (3/11) Nov 07 2015 Yes! It's just my inattention. Thanks!
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 --versionDMD64 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
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
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:Done, no difference.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
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: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 ?!On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:Done, no difference.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
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
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
On Saturday, 7 November 2015 at 06:02:49 UTC, BBaz wrote:On Saturday, 7 November 2015 at 00:21:57 UTC, crimaniak wrote:Yes! It's just my inattention. Thanks! p.s. Don't program at 3 am[...] 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 07 2015