Champion: Readonly members on structs (16.3, Core 3) #1710
Description
Readonly Instance Methods
- Proposed: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/readonly-instance-members.md
- Prototype: Done
- Implementation: Done
- Specification: Not Started
Summary
Provide a way to specify individual instance members on a struct do not modify state, in the same way that readonly struct
specifies no instance members modify state.
It is worth noting that readonly instance member
!= pure instance member
. A pure
instance member guarantees no state will be modified. A readonly
instance member only guarantees that instance state will not be modified.
All instance members on a readonly struct
could be considered implicitly readonly instance members
. Explicit readonly instance members
declared on non-readonly structs would behave in the same manner. For example, they would still create hidden copies if you called an instance member (on the current instance or on a field of the instance) which was itself not-readonly.
Motivation
Today, users have the ability to create readonly struct
types which the compiler enforces that all fields are readonly (and by extension, that no instance members modify the state). However, there are some scenarios where you have an existing API that exposes accessible fields or that has a mix of mutating and non-mutating members. Under these circumstances, you cannot mark the type as readonly
(it would be a breaking change).
This normally doesn't have much impact, except in the case of in
parameters. With in
parameters for non-readonly structs, the compiler will make a copy of the parameter for each instance member invocation, since it cannot guarantee that the invocation does not modify internal state. This can lead to a multitude of copies and worse overall performance than if you had just passed the struct directly by value. For an example, see this code on sharplab
Some other scenarios where hidden copies can occur include static readonly fields
and literals
. If they are supported in the future, blittable constants
would end up in the same boat; that is they all currently necessitate a full copy (on instance member invocation) if the struct is not marked readonly
.
Design
Allow a user to specify that an instance member is, itself, readonly
and does not modify the state of the instance (with all the appropriate verification done by the compiler, of course). For example:
public struct Vector2
{
public float x;
public float y;
public readonly float GetLengthReadonly()
{
return MathF.Sqrt(LengthSquared);
}
public float GetLength()
{
return MathF.Sqrt(LengthSquared);
}
public readonly float GetLengthIllegal()
{
var tmp = MathF.Sqrt(LengthSquared);
x = tmp; // Compiler error, cannot write x
y = tmp; // Compiler error, cannot write y
return tmp;
}
public float LengthSquared
{
readonly get
{
return (x * x) +
(y * y);
}
}
}
public static class MyClass
{
public static float ExistingBehavior(in Vector2 vector)
{
// This code causes a hidden copy, the compiler effectively emits:
// var tmpVector = vector;
// return tmpVector.GetLength();
//
// This is done because the compiler doesn't know that `GetLength()`
// won't mutate `vector`.
return vector.GetLength();
}
public static float ReadonlyBehavior(in Vector2 vector)
{
// This code is emitted exactly as listed. There are no hidden
// copies as the `readonly` modifier indicates that the method
// won't mutate `vector`.
return vector.GetLengthReadonly();
}
}
Readonly can be applied to property accessors to indicate that this
will not be mutated in the accessor.
public int Prop1
{
readonly get
{
return this._store["Prop1"];
}
readonly set
{
this._store["Prop1"] = value;
}
}
When readonly
is applied to the property syntax, it means that all accessors are readonly
.
public readonly int Prop2
{
get
{
return this._store["Prop2"];
}
set
{
this._store["Prop2"] = value;
}
}
Readonly can only be applied to accessors which do not mutate the containing type.
public int Prop3
{
readonly get
{
return this._prop3;
}
set
{
this._prop3 = value;
}
}
Readonly can be applied to some auto-implemented properties, but it won't have a meaningful effect. The compiler will treat all auto-implemented getters as readonly whether or not the readonly
keyword is present.
// Allowed
public readonly int Prop4 { get; }
public int Prop5 { readonly get; }
public int Prop6 { readonly get; set; }
// Not allowed
public readonly int Prop7 { get; set; }
public int Prop8 { get; readonly set; }
Readonly can be applied to manually-implemented events, but not field-like events. Readonly cannot be applied to individual event accessors (add/remove).
// Allowed
public readonly event Action<EventArgs> Event1
{
add { }
remove { }
}
// Not allowed
public readonly event Action<EventArgs> Event2;
public event Action<EventArgs> Event3
{
readonly add { }
readonly remove { }
}
public static readonly event Event4
{
add { }
remove { }
}
Some other syntax examples:
- Expression bodied members:
public readonly float ExpressionBodiedMember => (x * x) + (y * y);
- Generic constraints:
public static readonly void GenericMethod<T>(T value) where T : struct { }
The compiler would emit the instance member, as usual, and would additionally emit a compiler recognized attribute indicating that the instance member does not modify state. This effectively causes the hidden this
parameter to become in T
instead of ref T
.
This would allow the user to safely call said instance method without the compiler needing to make a copy.
The restrictions would include:
- The
readonly
modifier cannot be applied to static methods, constructors or destructors. - The
readonly
modifier cannot be applied to delegates. - The
readonly
modifier cannot be applied to members of class or interface.
Drawbacks
Same drawbacks as exist with readonly struct
methods today. Certain code may still cause hidden copies.
Notes
Using an attribute or another keyword may also be possible.
This proposal is somewhat related to (but is more a subset of) functional purity
and/or constant expressions
, both of which have had some existing proposals.
LDM history:
Activity