Skip to content
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: Property Setters as Methods #9103

Closed
HaloFour opened this issue Feb 23, 2016 · 9 comments
Closed

Proposal: Property Setters as Methods #9103

HaloFour opened this issue Feb 23, 2016 · 9 comments

Comments

@HaloFour
Copy link

This is a total spaghetti proposal. 🍝

I've seen a number of proposals around fluent APIs and how the language should be modified to extend initialization or reuse the target object, often with either messy grammar results or features that have been intentionally declined from the language for years.

What if the C# compiler would permit using property setters on arbitrary types as if they were methods that accepted the proposed value as a parameter and returned the parent instance?

public class Foo {
    public string Bar { get; set; }
    public int Baz { get; set; }

    public static Foo MakeAFoo() {
        return new Foo();
    }
}

var result = Foo.MakeAFoo()
    .Bar("Hello")
    .Baz(1234)
    .ToString();

// equivalent to
Foo $tmp = Foo.MakeAFoo();
$tmp.Bar = "Hello";
$tmp.Baz = 1234;
string result = $tmp.ToString();

It might get a little ugly with indexers or events, though.

@alrz
Copy link
Member

alrz commented Feb 23, 2016

Imaginary compiler error - Baz takes an int

Anyway, this can be an alternative to #133, however, it dictates fluent API style. Not to mention that this is something towards easing mutation which isn't considered as a theme for now, I believe.

@HaloFour
Copy link
Author

@alrz Oops, I'll fix that. Yes, #133 is one of the various proposals that all fit under this theme.

@DerpMcDerp
Copy link

Another problem with this proposal is that properties can return delegates. If your example had been public Func<string, Foo> Bar { get; set; }, the call would have been ambiguous.

Also, Foo already has setter methods (i.e. set_Bar and set_Baz), C# just doesn't expose them for some reason. An alternative to your proposal would to just have C# remove this restriction and combine it with something like #8947 to allow for non-fluent interfaces:

var result = Foo.MakeAFoo()
    ..set_Bar("Hello")
    ..set_Baz(1234)
    ..ToString();

@HaloFour
Copy link
Author

@DerpMcDerp

Another problem with this proposal is that properties can return delegates.

This proposal deals strictly with the setter accessors, and they return void, so the type of the property would not be a concern.

@svick
Copy link
Contributor

svick commented Feb 24, 2016

@HaloFour I think that @DerpMcDerp's point was that when you write foo.Bar("Hello"), then it's ambiguous whether you're getting Bar and then invoking the returned delegate or whether you're calling the setter.

@HaloFour
Copy link
Author

@svick Ah yes, that is a good point. Pretty much shuts down this proposal. Oh well, it was just spaghetti against the wall.

@manixrock
Copy link

@DerpMcDerp the way method cascading is usually implemented, it's not limited to setters, and uses the usual operators. So the example would be more like this:

var result = Foo.MakeAFoo()
    ..Bar = "Hello"
    ..Baz = 1234
    .ToString(); // the last one doesn't use ".." as we want it returned into result

@HaloFour
Copy link
Author

@manixrock

C# isn't a whitespace sensitive language which makes that syntax quite ambiguous.

var result = Foo.MakeAFoo()..Bar="Hello"..Baz=1234.ToString();

@manixrock
Copy link

Edit: comment was wrong/incomplete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants