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: "with" expressions for mutable objects #5676

Closed
alrz opened this issue Oct 4, 2015 · 11 comments
Closed

Proposal: "with" expressions for mutable objects #5676

alrz opened this issue Oct 4, 2015 · 11 comments
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue

Comments

@alrz
Copy link
Member

alrz commented Oct 4, 2015

This has been concluded in #5365 but I thought it deserves a separate ticket. Also it can be considered as an extension to #5172 for using with expressions on other types.

It basically provides a With...End With-like syntax for C# but as an expression.

var car = with carFactory.CreateCar() {
    Brand = "Ferrari",
    Model = "Enzo"
};

->

var car = carFactory.CreateCar();
car.Brand = "Ferrari";
car.Model = "Enzo";

However, you can ignore the returned value if you don't need it e.g. after with car { ... }; you can continue to use the existing variable.

@orthoxerox
Copy link
Contributor

How will you reconcile the difference in semantics between withing an immutable (create a copy and modify it) and a mutable (mutate the object passed to the expression) object?

@alrz
Copy link
Member Author

alrz commented Oct 4, 2015

@orthoxerox with mutableCar { ... } vs existing syntax immutableCar with { ... }. The former implies "with mutableCar in the context" and the latter implies "new immutableCar with following properties".

@orthoxerox
Copy link
Contributor

@alrz wouldn't this still be too confusing?

@alrz
Copy link
Member Author

alrz commented Oct 4, 2015

@orthoxerox Programmers should be familiar with this kind of "confusing"! For example const in C++ is pretty confusing:

const char* pstring = "";
char* const pstring = "";
const char* const pstring = "";

These are all different.

@MgSam
Copy link

MgSam commented Oct 4, 2015

So because a completely separate language has a confusing design for a completely separate feature means it's ok for C# to add confusing features?

@aluanhaddad
Copy link

Factory initializers are a good idea. Using the same keyword that is specifically intended for use in immutable context for a mutability oriented feature is not

@ashmind
Copy link
Contributor

ashmind commented Oct 4, 2015

Duplicate of #133?

@alrz
Copy link
Member Author

alrz commented Oct 4, 2015

@ashmind the use case is partially the same but you didn't mentioned a practical syntax, it's the same as with for record types which as @orthoxerox mentioned these should be different to some degree.
However, the intention for this proposal is not limited to factory initialization, it's more of a With...End With syntax that actually covers the factory initialization too (that's why I didn't see your proposal in the first place) and followed by #5365 this shouldn't be limited to just properties.

@alrz
Copy link
Member Author

alrz commented Oct 4, 2015

@aluanhaddad you're saying that this should use another keyword? my proposed syntax is based on the fact that these two basically do the same job with a different implementation. And since I'm against of totally immutable records (well, not algebraic types, just records; see #206 comment) this would make sense that you actually decide how you want to do this when you are doing it — like C++ classes; you decide where you want to put your variable (stack or heap) right when you are declaring it, not based on type.

@alrz
Copy link
Member Author

alrz commented Oct 5, 2015

As an alternative syntax this could be done with no keyword at all e.g. scoping braces with a leading expression var car = carFactory.CreateCar() { ... };.

@gafter
Copy link
Member

gafter commented Oct 5, 2015

@alrz This appears to be a dup of #133 with a different syntax.
#5365 does not appear to have much to do with this proposal, even though it is a separate proposal for a similar construct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

6 participants