Why can't we make reference variables?
Jonathan M Davis
jmdavisProg at gmx.com
Tue Aug 28 18:28:35 PDT 2012
On Wednesday, August 29, 2012 02:21:28 Tommi wrote:
> Foreach loops can make reference variables, and function calls
> can do it for the parameters passed in. So, my question is,
> wouldn't it be better if we could, in general, make reference
> variables?
Not going to happen. Unfortunately though, I don't remember all of Walter's
reasons for it, so I can't really say why (partly due to complications it
causes in the language, I think, but I don't know). Use a pointer,
std.typecons.RefCounted, a class, or make your struct a reference type (which
would probably mean having the data held in a separate struct with a pointer
to it in the outer struct). It's really not hard to have a type which is a
reference type if that's what you really want. You just can't declare a ref to
a variable as a local variable.
And really, the only two differences between using a pointer and being able to
directly declare a reference like you can in C++ is the fact that a pointer
can be null and that operations which don't use . require that you dereference
the pointer first (e.g. ==). So, while there may be cases where being able to
do something like
ref var = otherVar;
would be nice, it really doesn't buy you all that much.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list