如何将爆炸字符添加到函子中?
按照惯例,我们将一个bang字符!
添加到突变其参数的任何函数名称中,因此,对于以下代码示例,我们是否应该在函数名称中添加!
?
mutable struct Foo
a::Int
end
(foo::Foo)(val) = foo.a = val
f = Foo(1) # f.a = 1
f(10) # f.a = 10
简而言之,是否可以将最后一行称为f!(10)
?我只是好奇。谢谢。
By convention, we add a bang character !
to any function name that mutates its arguments, so for the following code example, should we add a !
to the functor name?
mutable struct Foo
a::Int
end
(foo::Foo)(val) = foo.a = val
f = Foo(1) # f.a = 1
f(10) # f.a = 10
In short, is it possible to call the last line as f!(10)
? I am just curious. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的呼叫与您的变量的名称相同。因此,如果您希望它包含一个
!
,您将必须命名您的变量f!
:!
字符没有任何神奇的这只是标识符的一部分。因此,您必须将!
放在内部的实际名称,恰好就像您对功能一样。The call here is just the same as the name of your variable. So if you want it to contain a
!
, you will have to name your variablef!
:There is nothing magical about the
!
character, it's just part of the identifier. So you have to put the!
inside the actual name, exactly like you do with functions.