gpt4 book ai didi

ponylang - 是否可以修改函数参数? (就像 C++ 中的 &)

转载 作者:行者123 更新时间:2023-12-03 06:22:18 28 4
gpt4 key购买 nike

 actor Test
fun foo(a: U32) =>
a = a + 1

我想要test.foo(a)修改a。这可能吗?谢谢

最佳答案

您只能在类级别修改var。这是故意的,因为参与者不喜欢就地更新——它确实与无锁并发不太兼容。

默认情况下,函数具有 box 功能,这意味着该函数操作的数据是只读的。为了确保函数可以改变数据,需要声明该方法fun ref

actor Main
var i: U32 = 0

fun ref foo() =>
i = i + 1

new create(env: Env) =>
env.out.print(i.string())
foo()
env.out.print(i.string())

Playground

关于ponylang - 是否可以修改函数参数? (就像 C++ 中的 &),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53914880/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com