Bug 10576 – enforce/enforceEx overload for returntype

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-08T18:01:27Z
Last change time
2024-12-01T16:18:15Z
Assigned to
No Owner
Creator
David
Moved to GitHub: phobos#9990 →

Comments

Comment #0 by admin — 2013-07-08T18:01:27Z
auto foo() { int invalid_value = 3; return 3; } auto result = enforceEx!(Exception, 3)(foo()); Currently this code doesn't work as expected: auto result = enforceEx!Exception(foo() != 3); result would be a boolean instead of the result of 3. If there is an overload which would take either a value to compare to or even a delagate (alias fun?) this would make enforceEx much more useful.
Comment #1 by andrej.mitrovich — 2013-07-08T18:09:20Z
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 } -----
Comment #2 by robert.schadek — 2024-12-01T16:18:15Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9990 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB