Re: Argument scope
On 12/5/2010 11:32 PM, Mike Schilling wrote:
"Lew" <noone@lewscanon.com> wrote in message
news:idhr76$j6b$1@news.albasani.net...
BGB wrote:
admittedly, I would also like get/set support and structs, ... (if/when
What do you mean by "structs"?
I'd guess it's a C or C++-like struct: an object all of whose fields are
public. (In C++, that may be a default rather than a uniform rule. I
honestly don???t recall.)
except, in this case, I was thinking more C# style struct semantics, as
these would likely be a better fit for Java.
so, the basic description is, it is like a class, except:
it is pass-by-value vs pass-by-reference;
inheritance is not supported;
methods may be supported;
....
an example:
struct Foo
{
public double x, y;
}
Foo a=new Foo();
Foo b;
a.x=3;
a.y=4;
b=a;
b.x=5;
now: a.x=3, a.y=4, b.x=5, b.y=4.
mostly, structs would allow potentially higher performance with some
operations (particularly vector math), since the use of pass-by-value
semantics allows the compiler and VM to more accurately know the
lifespan of objects (vs with classes where the lifespan is potentially
unbounded, and the VM can't statically optimize for the lifetime of any
object which may be potentially captured externally during its lifetime).
there is no need to handle the possibility of capture with a struct,
since it is always passed by value, so anything captured in a variable
is necessarily a copy.
C# also supports passing-by-reference with structs, but at this point
one almost may as well just use classes. in this case, I will also
ignore the possibility of supporting pointers (these would complicate
matters and constrain the implementation).
similarly, properties:
class Bar extends Object
{
private double x, y;
public double X {
get { return x; }
set { x=value; }
}
public double Y {
get { return y; }
set { y=value; }
}
}
which could internally expand to something like:
class Bar extends Object
{
private double x, y;
public double get_X() { return x; }
public void set_X(double value) { x=value; }
public double get_Y() { return y; }
public void set_Y(double value) { y=value; }
}
the main difference is that one can access a field differently:
Bar obj;
double x;
....
x=obj.X;
obj.X=3;
vs, say:
x=obj.getX();
obj.setX(3);
yes... properties are basically just syntax sugar...