Changeset 499

Show
Ignore:
Timestamp:
11/18/07 23:52:19 (1 year ago)
Author:
andrei
Message:

Added enforce signature taking an exception

Files:

Legend:

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

    r394 r499  
    3737 
    3838module std.contracts; 
     39import std.conv; 
    3940 
    4041/* 
     
    7172 */ 
    7273 
    73 T enforce(T)(T value, lazy string msg = "") 
     74T enforce(T)(T value, lazy string msg = "Enforcement error") 
    7475{ 
    7576    if (value) return value; 
    7677    throw new Exception(msg); 
     78} 
     79 
     80// T enforce(T, S...)(T value, lazy S msgs) 
     81// { 
     82//     if (value) return value; 
     83//     string msg; 
     84//     foreach (m; msgs) 
     85//     { 
     86//         msg ~= to!(string)(m); 
     87//     } 
     88//     throw new Exception(msg); 
     89// } 
     90 
     91/** 
     92 * If $(D_PARAM value) is nonzero, returns it. Otherwise, throws 
     93 * $(D_PARAM ex). 
     94 * Example: 
     95 * ---- 
     96 * auto f = enforce(fopen("data.txt")); 
     97 * auto line = readln(f); 
     98 * enforce(line.length, new IOException); // expect a non-empty line 
     99 * ---- 
     100 */ 
     101 
     102T enforce(T)(T value, lazy Exception ex) 
     103{ 
     104    if (value) return value; 
     105    throw ex(); 
    77106} 
    78107