Replies: 10 comments 33 replies
-
This seems like a letdown. I'd still be repeating the same thing in two places instead of three. I've been looking forward to the day when this repetition would be gone. Today, much of the time that I open a .cs file, the first thing I see is similar to this: class Foo
{
private readonly A a;
private readonly B b;
private readonly C c;
private readonly D d;
public Foo(
A a, // Normally these are not especially short names
B b,
C c,
D d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
// Often further initialization, which is a different conversation
}
} What I was hoping for: class Foo(
A a,
B b,
C c,
D d)
{
// ...
}
If It would be really interesting to see if I decide that I no longer care whether implicitly declared fields are marked What could hurt is no longer noticing if I accidentally assign to a field instead of a local in one of the methods in the class, or extra time reviewing code having to keep in mind that each field is mutable. IDE/ReSharper have nagged to make things readonly right away for so long that I've lost all feeling, so I'm not sure how often these things would bite. |
Beta Was this translation helpful? Give feedback.
-
Is mutability a good default? Here it looks like the field is being mutated, but the constructor parameter is being mutated. And which value of class Foo(int a)
{
private int b = a++;
public int B => a++;
} This could be a lot more subtle with structs. class Foo(SomeStruct s)
{
private int x = s.SomeMutatingMethod();
private int y = s.SomeOtherMutatingMethod();
} |
Beta Was this translation helpful? Give feedback.
-
For single thread code, that's probably true. But for multi-threading code, readonly helps and give much more confidence. Well, I admit modern frameworks did a good job avoiding explicit multi-threading code in most daily work. So let's forget readonly local & parameters for now, but focus on readonly fields. Unlike readonly local & parameter, C# already has readonly fields, for long. Refactor tools have been suggesting and auto-adding |
Beta Was this translation helpful? Give feedback.
-
Until you capture them in a lambda expression in which case they become shared state where I have had to deal with bugs that result from accidental mutation. The team was forced to adopt a breaking change in the behavior of
I'm not 100% sure I grok this statement. If this results in the parameter not being accessible anywhere except field initializers then you would have succeeded in partially mitigating the mutation issue (although not entirely) but at the expense of making primary constructors pretty much entirely useless. Quite a few languages have modifiers to indicate that parameters and/or locals should be readonly, including C++. I don't see why this is remotely controversial. There is value in declaring intent and having it enforced by the compiler even if it doesn't otherwise affect the behavior of the code. |
Beta Was this translation helpful? Give feedback.
-
I can only guess what this is about. Have we given up the runtime flag to opt into having all System.String instances just become UTF-8 under the hood? |
Beta Was this translation helpful? Give feedback.
-
The big difference is that F# record members are immutable (readonly in C# terms) by default. |
Beta Was this translation helpful? Give feedback.
-
I'm not familiar with the term, "attractive nuisance". But a search of the term suggests it's the concept of something being attractive to children that can cause them harm. By that definition, things like mutable by default and classes being unsealed by default are examples of "attractive nuisance". But the above quote seems to suggest some of the LDM see "pit of success" features such as readonly locals as an example of "attractive nuisance", so I'm genuinely confused by this line. Have I misunderstood the term? |
Beta Was this translation helpful? Give feedback.
-
Important note: Please don't take the following too seriously. I find the whole readonly parameters and locals issue just maddening. It's like the LDM is a car manufacturer and the users are asking for the next version of their cars to come with turn signals:
|
Beta Was this translation helpful? Give feedback.
-
I don't have insight into why a Utf8String type would be requested, but I personally think that the internal storage format of a string is an implementation detail that shouldn't be leaked. When working with interop scenarios, I think I would prefer to see something like a [StringFormat(StringFormats.Utf8 | StringFormats.NullTerminated)] applied to returns or method parameters and have the conversion happen automatically and have a "ToBytes" extension method that would take similar parameters to the above attribute. |
Beta Was this translation helpful? Give feedback.
-
I'm interested why the runtime team doesn't accept it. The proposal and experimental implementation have existed for years. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-10-27.md
Agenda
Beta Was this translation helpful? Give feedback.
All reactions