-
Notifications
You must be signed in to change notification settings - Fork 1k
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] Allow constructors to invoke methods that modify readonly fields #1871
Comments
All methods are implicitly private, so it should instead be unable to be public. |
I believe it's been previously mentioned that the complexity of the required flow analysis for this is likely to be prohibitive - especially once you start considering lambdas (esp variable capturing), async/await, versioning, and so on. FWIW, I've used tuples as workarounds to good effect, e.g.: public class Machine
{
private readonly int _value;
private readonly string _name;
public Machine(int minutes)
{
(_value, _name) = Configure(minutes);
}
private static (int value, string name) Configure(int minutes)
{
return (minutes, minutes.ToString());
}
} |
Private methods can still be accessed by other types. That seems... dangerous :) |
I imagine a possible implementation for a feature like this would be that the For example: public class Machine
{
private readonly int timeOnline;
private readonly long secondsUntilExpiration;
private readonly string name;
public Machine(Func<Configuration> configFunc)
{
SetConfiguration(configFunc());
}
initonly void SetConfiguration(Configuration config)
{
timeOnline = config.TimeOnline;
name = config.Name ?? "Default Machine";
AdditionalStuff(config);
}
initonly void AdditionalStuff(Configuration config)
{
timeOnline = config.Online > 100 ? 100 : 5;
name = config.Name ?? "Default Machine";
}
} could be "compiled down" to: public class Machine
{
private readonly int timeOnline;
private readonly long secondsUntilExpiration;
private readonly string name;
public Machine(Func<Configuration> configFunc)
{
SetConfiguration(configFunc(), ref timeOnline, ref name);
}
private void SetConfiguration(Configuration config, ref int timeOnline, ref string name)
{
timeOnline = config.TimeOnline;
name = config.Name ?? "Default Machine";
AdditionalStuff(config, ref timeOnline, ref name);
}
private void AdditionalStuff(Configuration config, ref int timeOnline, ref string name)
{
timeOnline = config.Online > 100 ? 100 : 5;
name = config.Name ?? "Default Machine";
}
} I've taken a few liberties with the proposed syntax. I've changed This is also completely safe/verifiable as the |
As a fan of pure methods, I really dislike features like this as they encourage what I see as bad practice: accessing fields directly from within arbitrary methods. I appreciate that others will disagree with this stance, but this is my view on it and I strongly believe it's the right view too. So I would tackle this issue completely differently: public class Machine
{
private readonly int _timeOnline;
private readonly long _secondsUntilExpiration;
private readonly string _name;
public Machine(Func<Configuration> configFunc)
{
(_timeOnline, _name) = SetConfiguration(configFunc());
}
private static (int timeonline, string name) SetConfiguration(Configuration config)
{
var timeOnline = config.TimeOnline;
var name = config.Name ?? "Default Machine";
return (timeOnline, name);
}
} That way, constructors can call other methods, and fields can be marked as readonly and updated via the use of those methods, all without having to come up with some solution to allowing methods to write to readonly fields. No language change needed, just a change in how folk use the language. |
Closing as duplicate of #348 |
Sometimes when initializing a class, the setting of readonly variables can make constructors lengthy or hamper readability.
This proposal aims to improve the readability of constructor code by introducing a modifier keyword to a method called
initonly
.Requirements
initonly
Current workarounds
There aren't any really great workarounds right now that I can think of, except one.
out params
You can specify parameters, but this looks kind of hackish.
Samples
Single method invocation
Multiple method invocations
Advantages
Disadvantages
The text was updated successfully, but these errors were encountered: