News for hackers. Stuff that matters.
Re:PHP 8.4 by Drew_ on Thursday November 21, 2024 @06:15PM (#42209674)
C# has the same thing as Properties, but the backing value is set explicitly as a separate variable:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

Re:PHP 8.4 by wvenable on Thursday November 21, 2024 @06:34PM (#42209785)
There are many times I would love to have an automatic backing field in C#. C# will do that if you use the default setter but in cases where that's not possible I dislike having to both declare a separate field and having it available to the rest of the class outside of the setter.
Re:PHP 8.4 by bwoebi on Thursday November 21, 2024 @06:38PM (#42209808)
C# 13 has this via the field keyword (as preview feature): https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...

It's pretty similar to what PHP provides here, except that PHP uses "$this->propname =" and C# uses "field =".

Edit: As someone involved in the RFC, it's somewhat funny, because we considered a special variable like "$field" or "$value" too magic, and C# does just that with a field keyword.

Re:PHP 8.4 by kroltan on Thursday November 21, 2024 @08:42PM (#42210425)
The `field` keyword also already existed in C#, to add attributes to the backing field of automatic properties, so I think the argument was easier there.

I used it in Unity projects to have serialized/inspectable values exposed through properties:

    [field: SerializeField]
    public int MyProperty { get; private set; }
Re:PHP 8.4 by Spivak on Friday November 22, 2024 @12:49AM (#42211474)
I think it being a keyword does change how magic the feature feels. "$field" and "$this->name" make me think that this behavior is playing by the normal rules of the language but is implemented using some trickery to edit the local scope of this function to add or modify variables. When it's a keyword it says this is a special form and that this is a feature of the language itself.
Re:PHP 8.4 by neonsunset on Thursday November 21, 2024 @06:37PM (#42209797)
https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs...

edit: the properties reference now documents the 'field' keyword too

Re:PHP 8.4 by wvenable on Thursday November 21, 2024 @06:40PM (#42209819)
That might be all the motivation I need to go move to .NET 9.
Re:PHP 8.4 by runevault on Friday November 22, 2024 @12:17AM (#42211378)
.NET 9 has so much good stuff in it. Thing that I randomly realized I wanted only to find out it works in 9 is dictionary access with "related" types (so like if you have a ReadOnlySpan<char> you can use that to find a value from a string in the dictionary via a value type created off the dict).