如何将爆炸字符添加到函子中?

发布于 2025-02-02 22:07:24 字数 285 浏览 3 评论 0原文

按照惯例,我们将一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2025-02-09 22:07:24

这里的呼叫与您的变量的名称相同。因此,如果您希望它包含一个,您将必须命名您的变量f!

julia> f! = Foo(1)  # f.a = 1
Foo(1)

julia> f!(4)
4

字符没有任何神奇的这只是标识符的一部分。因此,您必须将 放在内部的实际名称,恰好就像您对功能一样。

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 variable f!:

julia> f! = Foo(1)  # f.a = 1
Foo(1)

julia> f!(4)
4

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文