You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
andrej.mitrovich (@AndrejMitrovic) commented on 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 failureenforceEx!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 boolint z = enforceEx!Exception(returnValid() != -1);assert(z == 5); // now this fails
}
-----
admin (@Dav1dde) reported this on 2013-07-08T18:01:27Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=10576
CC List
Description
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.The text was updated successfully, but these errors were encountered: