This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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: Beyond property initialization to code block initialization #1243
Comments
@softwaremills C# has the concept of object initializers, maybe this concept can be expanded. So it would be something like this:
|
@eyalsk That might get most of the way there. I like having the named reference to be able to have the full capability of blocks, like being able to, say, pass the For instance, here's an actual snippet from my codebase:
|
@ufcpp That is a good point; any proposed changes would need to be compatible with the syntax in records and, in particular, the with-expressions you mention. Good call. This is quite different than records, of course, which are meant to enable good practices in immutable structs. This proposal is meant to refer to taking any expression, providing a block that operates on that expression, with the whole thing evaluating to the original expression. The syntax would ideally be general-purpose syntax that would be very useful for building complex literals. Such things happen a lot in the creation of data structures to be serialized (as in my code snippet above) or, possibly, applied to user interfaces (say, for instance, the creation of React-like virtual stand-in elements). |
Kotlin supports a similar construct, and has the automatic "it" variable in the callers' context for such closures. I use such a extension for years, but naming it apply, config, utilize .... The mechanism is also very useful as constructor and could replace the object initializer with the advantage to access the methods also. ` class XY
} |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I have found this to be a really useful function:
I generally use it when I want to do some initialization without assigning a variable, and that initialization doesn't come in the form of setting a property:
That does work, but it annoys me that it's setting up a bunch of closure objects and functions behind the scenes for something that should be pretty simple syntactic sugar. Of course, not being a language designer, I have no idea what the proper syntax should be for this sort of thing. I imagine something like this:
I mean, I use my little helper
With()
in places, but I still cringe. And I find this pattern so useful all over the place, especially where you're building some complex data structure and it's not sufficient to use property initialization or collection initialization.The text was updated successfully, but these errors were encountered: