我可以在功能上连接数字和字符串吗?
我正在尝试创建一个在字符串中嵌入数字的纯函数。明显的连接方法不起作用:
pure string foo(immutable int bar)
{
return "Number: " ~ bar; // Error: strings and ints are incompatible.
return "Number: " ~ to!string(bar); // Error: to() is impure.
}
是否有一种干净、实用的方法来连接数字和字符串?我想避免编写自己的串联或转换函数,但如果必须的话我会这样做。
I am trying to make a pure function that embeds a number in a string. The obvious concatenation methods do not work:
pure string foo(immutable int bar)
{
return "Number: " ~ bar; // Error: strings and ints are incompatible.
return "Number: " ~ to!string(bar); // Error: to() is impure.
}
Is there a clean, functional way to concatenate a number and string? I would like to avoid writing my own concatenation or conversion function, but I will if I have to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是 to! 的一个长期存在的问题。 (请参阅此错误报告。)
据我所知,有Phobos 中没有匹配的纯函数。恐怕你只能靠自己了。
从OP编辑:我使用了像这样的函数将
uints
转换为字符串
。This seems to be a long-standing problem with to!. (See this bugreport.)
As far as I can tell, there are no matching pure functions in Phobos. I am afraid you are on your own.
Edit from the OP: I used a function like this one to convert
uints
tostrings
.