Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: ref property with both ref getter and setter #607

Open
BreyerW opened this issue May 19, 2017 · 0 comments
Open

Proposal: ref property with both ref getter and setter #607

BreyerW opened this issue May 19, 2017 · 0 comments

Comments

@BreyerW
Copy link

BreyerW commented May 19, 2017

Currently, you cannot declare something like this:

public ref int intTest{
     ref get{ return ref testIntField; }
     ref set{ 
             testField=value;
             OnPropertyChanged(ref value);
     }
}

And for good reason: there is no way to easily disambiguate Object.testInt=8; //ref getter of setter? both are valid

However, recently there is work to enable ref readonly. This feature should allow to disambiguate ref getter and ref setter like this:

public ref int intTest{
     ref readonly get{ return ref testIntField; }
     ref set{ 
             testField=value;
             OnPropertyChanged(ref value);
     }
}

Then in my previous example situation become clear: Object.testInt=8; //nope, cannot write to getter so resolve to ref setter, if present, error otherwise.

Caveats:

  1. This example wont work as-is: Object.testInt=8;. Normally setter would be converted to something like this: set_intTest(ref int value). This means passing standalone values like 8 wont work without workaround.

One possible is to generate temporary variable and pass that variable.

  1. There will be no way to mutate this property indirectly. Something like this
ref var ourInt =ref Object.intTest;
ourInt=6; //Nope, cannot write since readonly ref

Object.intTest=6; //however this should be still possible

wont work.

Possible Workaround: forget about ref benefits and allow only non-ref setter or allow non-ref setter as alternative option to developer (so responsibility is moved to developer).

Maybe someone have another idea?

Things to consider

Allow in for setter?

Use case

I believe it will mainly have certain benefits to game developers. Consider Entity object that consist of Position, Rotation and Scale + all Matrixes:

class Entity{

   public Vector3 Position{

      ref readonly get{
            return ref position;
      }
      ref set{
            position=value;
            OnTransformChanged(ref value);
      }
   }
   //repeat above pattern for rotation, scale and matrixes
}

As you can see this would yield massive performance boost in certain scenarios (especially for matrixes, where its normal to have 16 fields) while preserving ability to reliably fire events when one of the properties change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant