Skip to content

[Proposal]: fieldof() #9031

@RikkiGibson

Description

@RikkiGibson

fieldof()

Summary

Allow direct assignment and use of a property's backing field during construction, without having to invoke the setter, via a new fieldof(Prop) expression.

class C
{
    public C(DataStore store)
    {
        this.store = store;

        // allows giving an initial value for 'this.Prop'
        // without calling 'Store.WritePropToDisk()' thru the setter
        fieldof(this.Prop) = store.ReadPropFromDisk();

        // 'fieldof()' allows general usage of the field from an initialization context, a la writability of 'readonly' fields.
        M(ref fieldof(this.Prop));
    }

    void Method()
    {
        // error: 'fieldof' can only be used during initialization
        fieldof(this.Prop) = "a";
    }

    private DataStore store;

    public string Prop
    {
        get => field;
        set
        {
            if (value != field)
            {
                field = value;
                store.WritePropToDisk(value);
            }
        }
    }
}

Design meetings

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions