Skip to content

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]: Allow Method Calls to follow Property Setters inside Init-Blocks #5727

Closed
najak3d opened this issue Feb 7, 2022 · 1 comment
Closed

Comments

@najak3d
Copy link

najak3d commented Feb 7, 2022

There are plenty of examples where construction of an object involves more than just property settings.

This request is very simple, and tightly scoped, making it an easy addition to compiler logic, and should produce zero confusion for developers who would take advantage of this.

It is a replacement for Fluent-pattern, and can, in most cases, be used entirely in-lieu of Fluent pattern methods (which often require you to write many extension methods, or Builder classes to achieve the simplified notation.

Here's the pattern I am requesting:

new MyType()
{
     Name = "Name",
     Value = value,
     .Clicked += HandleClicked
     .SetProperty("propName", propValue),   // this is for "attached properties" common in UI's (WPF, Xamarin, Avalonia)
     .ApplyStyleByName("styleName"),
     .SetGridLoc(column, row)  // wrapper for another attached property
     .Bind("propName", modelObject, Mode.TwoWay)
     .PrepareForUse() 
     .AddChildren(
             new ChildType()
             {
                    // property setters, followed by method calls
             },
             new ChildType2()
             {
                    // property setters, followed by method calls
             }
     )
}

Note that here I've included "event handlers" in this example, as those are really just calling the "Event.Add()" method, and so qualify as method calls.

NOTE: Unlike Fluent pattern, this notation does not require ANY extension methods or builders to enable the notation.

The proposal is for the above to operate the EXACT SAME as it would if all ".method()" calls were simply called from outside the Initialization block using a local variable reference.

It should also be noted, that this proposal enables functionality that would not otherwise be available via calling methods after initialization. In the above example, if you are initializing children INSIDE the Init-Block (which is already supported by C#), then how do you call Methods upon the Parent object BEFORE adding children? Answer: You'd have to resort to Fluent-pattern or some extra builder logic to enable that to happen.

C# should make simple convenient things simple. Making the Init-Block work like a "wither" block (while enforcing a rule that properties must be set first, or at minimum that "init-only" properties must be set first).

@jmarolf
Copy link

jmarolf commented Feb 7, 2022

As per the rules laid out for this repo here

For new feature proposals, however, please raise them for discussion, and only submit a proposal as a pull request if invited to do so by a member of the Language Design Team (a "champion").

I am converting this to a discussion

@dotnet dotnet locked and limited conversation to collaborators Feb 7, 2022
@jmarolf jmarolf converted this issue into discussion #5728 Feb 7, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants