如何扩展Java以引入通过引用传递? [英] How can you extend Java to introduce passing by reference?

查看:79
本文介绍了如何扩展Java以引入通过引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java是按值传递的。如何将语言修改为介绍通过引用传递(或一些等效行为)?

Java is pass-by-value. How could you modify the language to introduce passing by reference (or some equivalent behavior)?

举例如

public static void main(String[] args) {
    String variable = "'previous String reference'";
    passByReference(ref variable);
    System.out.println(variable); // I want this to print 'new String reference'
}

public static void passByReference(ref String someString) {
    someString = "'new String reference'";
}

哪个(没有 ref )汇编到以下字节码

which (without the ref) compiles to the following bytecode

  public static void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String 'previous String reference'
       2: astore_1
       3: aload_1
       4: invokestatic  #3                  // Method passByReference:(Ljava/lang/String;)V
       7: return

  public static void passByReference(java.lang.String);
    Code:
       0: ldc           #4                  // String 'new String reference'
       2: astore_0
       3: return

3处的代码:从变量变量将引用加载到堆栈

我正在考虑的一种可能性是让编译器确定方法是通过引用传递的,可能还有 ref ,并更改方法以接受Holder对象,该对象存储与变量相同的引用。当方法完成,并且可能更改持有者中的引用时,调用者方的值上的变量将替换为持有者引用的值。

One possibility I'm considering is to have the compiler determine a method is pass by reference, possibly with ref, and change the method to accept a Holder object which stores the same reference as our variable. When the method completes, and possibly changes that reference in the holder, the variable on the caller side's value is replaced with the holder reference's value.

它应该编译成相当于这个

It should compile to an equivalent of this

public static void main(String[] args) {
    String variable = "'previous String reference'";
    Holder holder = Holder.referenceOf(variable);
    passByReference2(holder);
    variable = (String) holder.getReference(); // I don't think this cast is necessary in bytecode
    System.out.println(variable);
}

public static void passByReference(Holder someString) {
    someString.setReference("'new String reference'");
}

其中持有人可能类似于

public class Holder {
    Object reference;
    private Holder (Object reference) {
        this.reference = reference;
    }
    public Object getReference() {
        return this.reference;
    }
    public void setReference(Object reference) {
        this.reference = reference;
    }
    public static Holder referenceOf(Object reference) {
        return new Holder(reference);
    }
}

哪里可以失败或者你怎么能改善它?

Where can this fail or how could you improve it?

推荐答案

回答你的问题:


哪里可以失败?

Where can this fail?




  1. 最终变量和枚举常量

  2. '特殊'引用,例如 this

  3. 从方法调用返回的引用,或使用<$ c $构建内联c> new

  4. 文字(字符串,整数等)

  1. Final variables and enum constants
  2. 'Special' references such as this
  3. References that are returned from method calls, or constructed inline using new
  4. Literals (Strings, integers, etc.)

......可能还有其他人。基本上,只有参数源是非最终字段或局部变量时,您的 ref 关键字才可用。当与 ref 一起使用时,任何其他来源都应该生成编译错误。

...and possibly others. Basically, your ref keyword must only be usable if the parameter source is a non-final field or local variable. Any other source should generate a compilation error when used with ref.

(1)的示例:

final String s = "final";
passByReference(ref s);  // Should not be possible

(2)的一个例子:

passByReference(ref this);  // Definitely impossible

(3)的例子:

passByReference(ref toString());  // Definitely impossible
passByReference(ref new String("foo"));  // Definitely impossible

(4)的例子:

passByReference(ref "literal");  // Definitely impossible

然后有一些赋值表达式,在我看来就像一个判断调用:

And then there are assignment expressions, which seem to me like something of a judgement call:

String s;
passByReference(ref (s="initial"));  // Possible, but does it make sense?

你的语法需要 ref 方法定义和方法调用的关键字。我认为方法定义就足够了。

It's also a little strange that your syntax requires the ref keyword for both the method definition and the method invocation. I think the method definition would be sufficient.

这篇关于如何扩展Java以引入通过引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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