什么是“传名"?它是如何工作的? [英] What is "pass-by-name" and how does it work exactly?

查看:59
本文介绍了什么是“传名"?它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查过维基百科并用谷歌搜索过,但我仍然无法理解 ALGOL 60 中按名称传递的工作原理.

I've checked Wikipedia and googled but I still can't wrap my mind around how pass-by-name works in ALGOL 60.

推荐答案

我在 按名称传递参数.本质上,在将实际参数以文本方式替换到函数体中之后,函数体在调用时被解释.在这个意义上,评估方法类似于 C 预处理器宏的评估方法.

I found a good explanation at Pass-By-Name Parameter Passing. Essentially, the body of a function is interpreted at call time after textually substituting the actual parameters into the function body. In this sense the evaluation method is similar to that of C preprocessor macros.

通过将实际参数代入函数体,函数体既可以读写给定的参数.从这个意义上说,评估方法类似于传递引用.不同之处在于,由于通过名称传递参数在函数内部求值,诸如 a[i] 之类的参数取决于 的当前值i 在函数内部,而不是在调用函数之前引用 a[i] 处的值.

By substituting the actual parameters into the function body, the function body can both read and write the given parameters. In this sense the evaluation method is similar to pass-by-reference. The difference is that since with pass-by-name the parameter is evaluated inside the function, a parameter such as a[i] depends on the current value of i inside the function, rather than referring to the value at a[i] before the function was called.

我上面链接的页面有更多示例,说明按名称传递既有用又危险的地方.通过名称传递实现的技术如今在很大程度上已被其他更安全的技术(例如通过引用和 lambda 函数)所取代.

The page I linked above has some more examples of where pass-by-name is both useful, and dangerous. The techniques made possible by the pass-by-name are largely superseded today by other, safer techniques such as pass-by-reference and lambda functions.

这篇关于什么是“传名"?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆