Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 1706

Show
Ignore:
Timestamp:
07/01/10 18:54:26 (14 years ago)
Author:
kyllingstad
Message:

std.contracts.enforce() now puts the file name and line number in the appropriate fields instead of in the message string.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/phobos/std/contracts.d

    r1499 r1706  
    8888 
    8989T enforce(T, string file = __FILE__, int line = __LINE__) 
    9090(T value, scope void delegate() dg) 
    9191{ 
    9292    if (!value) dg(); 
    9393    return value; 
    9494} 
    9595 
    9696private void bailOut(string file, int line, in char[] msg) 
    9797{ 
    98     throw new Exception(text(file, '(', line, "): ", 
    99                     msg ? msg : "Enforcement failed")); 
     98    throw new Exception(msg ? msg.idup : "Enforcement failed", file, line); 
     99
     100 
     101unittest 
     102
     103    assert (enforce(123) == 123); 
     104 
     105    try 
     106    { 
     107        enforce(false, "error"); 
     108        assert (false); 
     109    } 
     110    catch (Exception e) 
     111    { 
     112        assert (e.msg == "error"); 
     113        assert (e.file == __FILE__); 
     114        assert (e.line == __LINE__-7); 
     115    } 
    100116} 
    101117 
    102118/** 
    103119 * If $(D_PARAM value) is nonzero, returns it. Otherwise, throws 
    104120 * $(D_PARAM ex). 
    105121 * Example: 
    106122 * ---- 
    107123 * auto f = enforce(fopen("data.txt")); 
    108124 * auto line = readln(f); 
    109125 * enforce(line.length, new IOException); // expect a non-empty line