On Mon, 01 Jun 2009 16:01:04 -0400, Witold Baryluk <bary...@smp.if.uj.edu.pl> wrote:

Dnia 2009-05-31, nie o godzinie 15:36 -0400, Jarrett Billingsley pisze:
On Sun, May 31, 2009 at 3:26 PM, Witold Baryluk
<bary...@smp.if.uj.edu.pl> wrote:
>
> Horrible.
>
> How to ensure constness of data, and still have possibility of changing references of local variables?

Rebindable.

http://www.digitalmars.com/d/2.0/phobos/std_typecons.html#Rebindable

Thanks. I was already thinking about implementing something like this.
It is very thin, and probably doesn't eat even single byte more than
original reference.

So generally we need to cheat: union with const and non-const version +
opAssign/opDot, and some hidden casts. If everybody is doing this, why
not.

Only one problem is that i need to make some wrappers for it:

alias Rebindable!(C) CC;


first try:

auto c1 = CC(new C(1));
auto c2 = CC(new C(2, c1)); // oops doesn't work
c2 = c2.b();

second try:

auto c1 = CC(new C(1));
auto c2 = CC(new C(2, c1.opDot())); // ok, works
c2 = c2.b();


define some function on original data:

int something(in C c) {
        return c.a;
}

something(c2); // oops, doesn't work

something(c2.opDot()); // ok, works



So generally now i need to overload all my functions to support also
Rebindable!(C), where I will unwrap object and call original function?
The same with constructors.

Can't be this done more simpler?

As I remember there was something like opCast (for explicit casts)?
Maybe Rebindable should have it casting to original type (with const)?


You are running into limitations that are planned to be fixed.

For example, rebindable probably shouldn't use opDot anymore... it should use alias this.

With opDot, you don't have implicit casting back to the original type. But alias this provides that, not sure if aliasing a union member has been tested...

Also, I believe Rebindable!(const C) is what you really want (I've argued in the past that Rebindable should just assume that it's type should be const). Rebindable!(T) is just an alias to T if T is not const, which is IMO absolutely useless.

Another thing I just noticed, which probably should be fixed, there is an alias for get which gets the original item. get's a pretty common member name, I don't think it should be overridden by Rebindable.

In fact, I think rebindable needs almost a rewrite with the recent developments of D2.

The goal of Rebindable is to transparently implement the sort of "tail-const" behavior you want without any of the pain you are currently experiencing. If it doesn't work seamlessly (except for where you wish to explicitly define "this is a rebindable reference"), then it's not finished.

Andrei?

-Steve

Reply via email to