Disabling opAssign in a type disabled all the opAssigns of an aliased type?

Steven Schveighoffer schveiguy at gmail.com
Tue Jul 31 20:40:25 UTC 2018


On 7/30/18 2:30 PM, aliak wrote:
> Is this a bug?
> 
> If not is there a workaround?
> 
> I would like for the alias this to function as a normal A type unless B 
> specifically disables certain features, but it seems weird that 
> disabling one opAssign disables all of them inside the aliases type but 
> not in the aliasing type?
> 
> 
> struct A {
>      void opAssign(int) {}
> }
> struct B {
>      A a;
>      alias a this;
>      @disable void opAssign(float);
> }
> 
> void main() {
>      B b;
>      b = 3;
> }
> 
> Error: function `onlineapp.B.opAssign` is not callable because it is 
> annotated with @disable
> 

OK, so one thing to learn in D, you can't hijack stuff. When you 
override a function, you have to override ALL the overloads.

What you have done is defined opAssign as ONLY accepting a float, and 
then disabling any calls to it.

This is obfuscated somewhat by the fact that 3 can be a float as well. 
So even if you didn't disable the opAssign(float), it would still call 
that function.

You can get around it by defining a forwarding function for the 
overloads you want to let through:

@disable void opAssign(float);
auto opAssign(int x) { return a.opAssign(x); }

-Steve


More information about the Digitalmars-d-learn mailing list