mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 21:51:40 +03:00
Turn enforce into an eponymous template + undocument enforceEx
This commit is contained in:
parent
a10c5ce123
commit
611e62c96f
5 changed files with 51 additions and 19 deletions
|
@ -17,7 +17,6 @@ $(TR $(TD Assumptions) $(TD
|
|||
$(TR $(TD Enforce) $(TD
|
||||
$(LREF doesPointTo)
|
||||
$(LREF enforce)
|
||||
$(LREF enforceEx)
|
||||
$(LREF errnoEnforce)
|
||||
))
|
||||
$(TR $(TD Handlers) $(TD
|
||||
|
@ -386,12 +385,17 @@ void assertThrown(T : Throwable = Exception, E)
|
|||
If a delegate is passed, the safety and purity of this function are inferred
|
||||
from `Dg`'s safety and purity.
|
||||
+/
|
||||
T enforce(E : Throwable = Exception, T)(T value, lazy const(char)[] msg = null,
|
||||
string file = __FILE__, size_t line = __LINE__)
|
||||
if (is(typeof({ if (!value) {} })))
|
||||
template enforce(E : Throwable = Exception)
|
||||
if (is(typeof(new E("", __FILE__, __LINE__)) : Throwable) || is(typeof(new E(__FILE__, __LINE__)) : Throwable))
|
||||
{
|
||||
if (!value) bailOut!E(file, line, msg);
|
||||
return value;
|
||||
|
||||
T enforce(T)(T value, lazy const(char)[] msg = null,
|
||||
string file = __FILE__, size_t line = __LINE__)
|
||||
if (is(typeof({ if (!value) {} })))
|
||||
{
|
||||
if (!value) bailOut!E(file, line, msg);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/// ditto
|
||||
|
@ -455,6 +459,15 @@ unittest
|
|||
}
|
||||
}
|
||||
|
||||
/// Alias your own enforce function
|
||||
@safe unittest
|
||||
{
|
||||
import std.conv : ConvException;
|
||||
alias convEnforce = enforce!ConvException;
|
||||
assertNotThrown(convEnforce(true));
|
||||
assertThrown!ConvException(convEnforce(false, "blah"));
|
||||
}
|
||||
|
||||
private void bailOut(E : Throwable = Exception)(string file, size_t line, in char[] msg)
|
||||
{
|
||||
static if (is(typeof(new E(string.init, string.init, size_t.init))))
|
||||
|
@ -584,8 +597,10 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
|
|||
return value;
|
||||
}
|
||||
|
||||
|
||||
// @@@DEPRECATED_2.084@@@
|
||||
/++
|
||||
$(RED Deprecated. Please use $(LREF enforce) instead. This function will be removed 2.084.)
|
||||
|
||||
If $(D !value) is $(D false), $(D value) is returned. Otherwise,
|
||||
$(D new E(msg, file, line)) is thrown. Or if $(D E) doesn't take a message
|
||||
and can be constructed with $(D new E(file, line)), then
|
||||
|
@ -600,6 +615,7 @@ T errnoEnforce(T, string file = __FILE__, size_t line = __LINE__)
|
|||
enforceEx!DataCorruptionException(line.length);
|
||||
--------------------
|
||||
+/
|
||||
//deprecated("Please use enforce instead")
|
||||
template enforceEx(E : Throwable)
|
||||
if (is(typeof(new E("", string.init, size_t.init))))
|
||||
{
|
||||
|
@ -611,7 +627,8 @@ if (is(typeof(new E("", string.init, size_t.init))))
|
|||
}
|
||||
}
|
||||
|
||||
/++ Ditto +/
|
||||
/+ Ditto +/
|
||||
//deprecated("Please use enforce instead")
|
||||
template enforceEx(E : Throwable)
|
||||
if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.init, size_t.init))))
|
||||
{
|
||||
|
@ -623,6 +640,7 @@ if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.i
|
|||
}
|
||||
}
|
||||
|
||||
//deprecated
|
||||
@system unittest
|
||||
{
|
||||
import core.exception : OutOfMemoryError;
|
||||
|
@ -666,6 +684,7 @@ if (is(typeof(new E(string.init, size_t.init))) && !is(typeof(new E("", string.i
|
|||
static assert(!is(typeof(enforceEx!int(true))));
|
||||
}
|
||||
|
||||
//deprecated
|
||||
@safe unittest
|
||||
{
|
||||
alias enf = enforceEx!Exception;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue