D 如何在断言失败时打印更多内容?

D 如何在断言失败时打印更多内容?,d,D,我甚至试过: assert(col + row * Col < Row * Col, "index out of bounds. row: %d col: %d", row, col); assert(col+row*col

我甚至试过:

assert(col + row * Col < Row * Col, "index out of bounds. row: %d  col: %d", row, col);
assert(col+row*col
但是我不能调用
,因为
opIndex
是纯的。我可以暂时从
opIndex
中删除
pure
,但这会触发一长串撤销操作,因为其他pure方法正在调用
opIndex
。无法将
调用到
也消除了创建自己的函数以传递到
断言
的可能性


那么,还有什么可以尝试的呢?我只想在断言失败时打印这些值

目前,如果您想在
函数中转换字符串,您必须自己编写转换函数。一些工作已经投入到像
std.conv.to
pure
这样的函数的制作中,但我们还没有达到它们所处的位置。太多的低级构造仍然不是纯粹的
,即使理论上可以。大量的工作正在投入到为下一版本的dmd(2.059)制作正确的
const
,一些关于
pure
的工作与此同时进行,因为
对象中的某些函数必须是
pure
const
safe
nothrow
。因此,在下一版本中,
std.conv.to
很有可能成为
pure
,用于在字符串之间进行转换。即使它在下一个版本中没有发生,它也会很快发生,因为它确实需要发生(如您的困境所示)。但在那之前,你只能靠自己了

假设所有这些都已整理好,那么为断言创建字符串的最佳方法是

assert(col + row * Col < Row * Col, "index out of bounds" ~ to!string(row)~ " " ~ to!string(col));
但是您必须使用
-debug
进行编译,否则您的断言将无法运行。但无论如何,
debug
将允许您在
pure
函数中放入不纯净的代码以进行调试,并且只要使用了
-debug
标志,它就会被编译,因此它可以作为当前
格式
的缺陷的临时解决办法

assert(col + row * Col < Row * Col, "index out of bounds" ~ to!string(row)~ " " ~ to!string(col));
assert(col + row * col < row * col,
       format("index out of bounds. row: %d  col: %d", row, col));
debug
{
    assert(col + row * col < row * col,
           format("index out of bounds. row: %d  col: %d", row, col));
}