Scala 可以通过引用调用吗? [英] Can Scala call by reference?

查看:45
本文介绍了Scala 可以通过引用调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Scala 支持来自 ALGOL 的按名称调用,我想我明白这意味着什么,但是 Scala 可以像 C#、VB.NET 和 C++ 一样执行按引用调用吗?我知道 Java 不能进行按引用调用,但我不确定这种限制是否仅仅是由于语言或 JVM.

I know that Scala supports call-by-name from ALGOL, and I think I understand what that means, but can Scala do call-by-reference like C#, VB.NET, and C++ can? I know that Java cannot do call-by-reference, but I'm unsure if this limitation is solely due to the language or also the JVM.

当您想将庞大的数据结构传递给方法,但又不想复制它时,这会很有用.在这种情况下,通过引用调用似乎是完美的.

This would be useful when you want to pass an enormous data structure to a method, but you don't want to make a copy of it. Call-by-reference seems perfect in this case.

推荐答案

Java 和 Scala 都专门使用按值调用,除了值是原始值或指向对象的指针.如果您的对象包含可变字段,那么这与按引用调用之间的实质性区别很小.

Java and Scala both use call by value exclusively, except that the value is either a primitive or a pointer to an object. If your object contains mutable fields, then there is very little substantive difference between this and call by reference.

因为您总是传递指向对象的指针而不是对象本身,所以您不必重复复制一个巨大的对象.

Since you are always passing pointers to objects not the objects themselves, you don't have the problem of having to repeatedly copy a giant object.

顺便说一句,Scala 的按名称调用是使用按值调用实现的,值是一个(指向 a 的)函数对象,返回表达式的结果.

Incidentally, Scala's call by name is implemented using call by value, with the value being a (pointer to a) function object that returns the result of the expression.

这篇关于Scala 可以通过引用调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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