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

with statement #7322

Closed
krwq opened this issue Dec 8, 2015 · 5 comments
Closed

with statement #7322

krwq opened this issue Dec 8, 2015 · 5 comments
Labels
Area-Language Design Discussion Resolution-Duplicate The described behavior is tracked in another issue

Comments

@krwq
Copy link
Member

krwq commented Dec 8, 2015

Recently I pretty often start seeing patterns like:

class MyClass
{
  public Type1 Field1 { get; set; }
  public Type2 Field2 { get; set; }
  // ...
  public MyClass WithField1(Type1 field1)
  {
    Field1 = field1;
    return this;
  }
  // ...
}

This seems pretty weird to repeat yourself million times

Should we provide pascal like operator for this?

class MyClass
{
  public Type1 Field1 { get; set; }
  public Type2 Field2 { get; set; }
}

// somewhere in the code (we do have initializer for the constructor but what later?):
MyClass c = new MyClass();
with (c)
{
   .Field1 = something1;
   .Field2 = something2;
   // ...
}
@GeirGrusom
Copy link

var c = new MyClass { Field1 = something1, Field2 = something2 };

Already works, but this obviously doesn't work with immutable types, but I think there already is a issue for that.

@HaloFour
Copy link

HaloFour commented Dec 8, 2015

Dupe of #133, #951 and #6553 to varying degrees.

The With* property pattern is a different beast, though, particularly since it returns a reference to the class to facilitate chaining but it doesn't necessarily return the reference to the current instance, which facilitates incremental building of immutable objects.

@dsaf
Copy link

dsaf commented Dec 9, 2015

@GeirGrusom That's right #229.

Also if the pattern is meant to be a config DSL or some sort of monadic stuff, it might not be directly replaceable by the with construct: https://en.wikipedia.org/wiki/Fluent_interface

@gafter
Copy link
Member

gafter commented Dec 15, 2015

Also possibly a dup of #5172

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Discussion Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants