www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10576] New: enforce/enforceEx overload for returntype

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10576

           Summary: enforce/enforceEx overload for returntype
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: admin dav1d.de



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 08 2013
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10576


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



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: -------
Jul 08 2013