Reuse of variables referencing const objects
kovrov+digitalmars at gmail.com
Mon Mar 9 14:48:06 PDT 2009
On 3/9/2009 8:50 PM, Chris Nicholson-Sauls wrote:
> While not strictly intuitive, you could do this:
> auto var = Rebindable!(const Foo)(new Foo);
> assert(var.opDot !is null);
> As 'opDot' returns the wrapped object (with const intact). The downside
> to that, however, is that it won't work in those cases where
> Rebindable's template parameter was mutable, as then it simply aliases
> it. This shouldn't be a problem in general use, though. Only in generic
> code, which could try to check for Rebindable.
Thanks Chris, this approach indeed works, its a shame I haven't figured
this on my own.
I wonder if it's proper usage of opDot.. In general, is it safe for
client code to rely on implementation of opDot and call it directly?
And in this particular case we rely on the fact that Rebindable uses
opDot to forward calls.
Slightly off-topic... In Python world (and I guess any other dynamic
language) it is not valid to make assumptions based on implementation.
Only safe way to use "foreign code" (a library, framework) is to follow
documentation. That way authors of libraries are free to change their
code as long as it comply with documented behavior.
More information about the Digitalmars-d-learn