This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: values types for Java


Adam Megacz wrote:

Hrm, can this do stack-allocated arrays?

Not as is, no.


There are two separate issues:
* Arrays whose elements are value types.  I touch in this in my
proposal.  I think this is relatively straightforward in
principle, but there are of course implementation issues.
* Fixed-length arrays that are themselves value types and so can be
stack-allocated (or be embedded fields or be passed by value).
That has other complications that I haven't discussed.  First of:
how do you indicate that an aarray should be stack-allocated or
a value-type?  The solutions I mentioned for value classes don't
work very well, but you could use an annotation for a specific
value or field.

I think stack-allocated arrays have limited usefulness.
You only want to so it for smallish arrays.

It might be useful as an optimization, without programmer
declaration.  I.e. automatically optimize:
  int[] iarr = new int[10];
  ... use iarr ...
when iarr doesn't "escape".  (The next issue is of course relevant.)

Also, is there any equivalent for C#/.NET pass-struct-by-reference?

No. That's a complete separate (orthogonal) issue. A feature of my Value Types proposal is that programs using them are valid portable Java programs - it is just a set of conventions so that a compiler can implement things more efficiently.

You could do something similar using generic wrapper types.
I.e. you can write portable Java programs, but an extended
compiler can generate more efficient code.

/** A variable that can be passed by reference.
  * Cannot be returned from a method.
  * Can be optimized to same as plain T with compiler support. */

public final class Variable<T>
{
  T value;
  public Variable(T init) { value = init; }
  public T get() { return value; }
  public void set(T x) { this.value = x; }
}

class Example
{
  public void meth(Object in_arg, Variable<Object> out_arg)
  { out_arg.set(in_arg); }

final Variable<Object> var = new Variable(null);

  public void use() { meth("foo", var); }
};

A Variable<T> can be declared as a field, a local variable or a
method parameter, but not a return type. (One could probably have
arrays of Variable<T>, but that is an exercise I haven't explored.)
In all cases the field/variable/parameter must be final (or at
least the compile will complain it is assigned to).  A field or
local variable can be initialized using a new-expression:
  final Variable<TYPE> VAR = new Variable<TYPE>(INIT_VALUE);

An optimer will implement this as if it was:
  TYPE VAR_PLAIN = INIT_VALUE;
and calls to VAR.get() and VAR.set(VALUE) will be optimized
to VAR_PLAIN and VAR_PLAIN=(VALUE).

You can also initialize a Variable with another Variable:
  final Variable<TYPE> VAR2 = VAR;
In that case VAR2 is an alias for VAR, and it is implemented
like a C++ reference: TYPE& VAR2 = VAR.

A formal parameter Variable<T> PARM is also implemented as
C++'s T& PARM.  (If T is a class name, the C++ translation
is T*&, since it's a reference to a pointer to a T.)  Calling
get and set on PARM is translated to getting and setting the
references variable.
--
	--Per Bothner
per@bothner.com   http://per.bothner.com/



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]