[Issue 10576] enforce/enforceEx overload for returntype
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jul 8 18:09:21 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10576
Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |andrej.mitrovich at gmail.com
--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-07-08 18:09:20 PDT ---
What David means is he wants the ability to both return the value and throw if
that value is invalid, however simply using (!value) as enforceEx currently
does will not work properly with e.g. signed integers. For example:
-----
import std.exception;
int returnValid() { return 5; }
int returnInvalid() { return -1; }
void main()
{
int x = enforceEx!Exception(returnValid());
assert(x == 5); // ok
// this doesn't throw, but we want -1 to signal failure
enforceEx!Exception(returnInvalid());
// so as a workaround we use the check inline, however..
int y = enforceEx!Exception(returnInvalid() != -1);
// .. it is not useful for the return value since the value becomes a bool
int z = enforceEx!Exception(returnValid() != -1);
assert(z == 5); // now this fails
}
-----
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list