qwertie / ecsharp

Home of LoycCore, the LES language of Loyc trees, the Enhanced C# parser, the LeMP macro preprocessor, and the LLLPG parser generator.
http://ecsharp.net
Other
177 stars 25 forks source link

Immutability 'with' #22

Closed ktodyruik closed 8 years ago

ktodyruik commented 8 years ago

Could loyc macros implement concise immutability syntax (fsharp style) in c#? Including 'with' to make changes easily to immutable types?

qwertie commented 8 years ago

The grammar of C# isn't friendly to allowing people to define new operators, particularly words. For example, if we wanted to add a with operator, we'd run into ambiguity since a<b> with (x: y) is already a valid expression in C#. (EDIT: EC# does have a backquote operator though, so you could write a<b>with(x: y) ) Also, since LeMP is a lexical macro processor, macros can't make decisions based on the types involved, which may be a problem.

That said, I already solved this problem from another angle. If you use the new ADT macro in LeMP, you get immutable types with free "With" functions to make changes easily.