| | 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 | |
|---|
| | 102 | T enforce(T)(T value, lazy Exception ex) |
|---|
| | 103 | { |
|---|
| | 104 | if (value) return value; |
|---|
| | 105 | throw ex(); |
|---|