I have another similar example illustrating this problem at semantic level.

import std.stdio, std.typecons;

struct Test
{
        //ref
        Nullable!int prop() @property
        {
                return _value;
        }
        
        private Nullable!int _value = 10;
        
}


void main()
{
        
        auto test = Test();
        
        //This looks like I want to modify property with *nullify*,
//but instead it creates variable that will never be used and modify it
        test.prop.nullify();

        assert( test.prop.isNull ); //This fails
}

So when using with value types I should always look if property is *ref* or not and it's not very good.

I have another question. How could I implement in this example if I uncomment it to call some function after modifying property? Do I need some sort of class wrapper for property or is there some other ideas?


What I expect:

void main()
{
        auto test = Test();
        
        test.prop.nullify();
        //Callback called after this operation

        test.prop = 15;
        //Callback called after this operation too
}

Callback is simple method of Test like:

void callback(Nullable!int value)
{
        if( value.isNull )
                //Do smth
        else if( value <= 0 )
                //Do smth else
        else //Just assign to internal field
                _value = value;
}


So I think it is possible to do this with some class wrapper around Nullable!int that should implement al of it's methods or for example dispatching calls to them via opDispacth. But this approach looks slightly complicated. What if I have complicated interface (for example std.datetime.Date as such property) so I need to reimplement or dispatch a lot of methods.

Could you advise another more simple approach to this problem?

Reply via email to