Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enforce/enforceEx overload for returntype #9990

Open
dlangBugzillaToGithub opened this issue Jul 8, 2013 · 1 comment
Open

enforce/enforceEx overload for returntype #9990

dlangBugzillaToGithub opened this issue Jul 8, 2013 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

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.
@dlangBugzillaToGithub
Copy link
Author

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 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
}
-----

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants