4 ms·
As someone who uses .NET daily, but loves Clojure and other functional programming paradigms - Records seem like a very compelling feature. Immutable data struc
by elamje 6y ago
As someone who uses .NET daily, but loves Clojure and other functional programming paradigms - Records seem like a very compelling feature. Immutable data structures without any hassle to set up. Simply write “record” where you would normally write “class” and boom, you are working with immutable objects. This is one step closer to one the best features of Clojure IMO -everything is immutable.
Additionally, there seems to be another new way to accomplish this. Normally class properties are accessed via get; and set;
Now we have the ability to use get; and init; which effectively makes the object immutable/read only after initialization. The syntax is just a little cleaner and more obvious to the reader how the property should be used.
C# has really been adding some great features the past few years and is certainly getting more expressive and concise which I appreciate.
- kroltan 6y agoYou probably know, but F# exists if you want a functional language in the .NET ecosystem. Granted, you end up dealing with a bunch of OOP libraries still since .NET is very C#-centered, but the language is pretty good and the integration is painless.
- forest_dweller 6y agoIf I wrote a F# codebase at a lot of companies I would find myself swiftly booted out the door.
- kroltan 6y agoOh yes you'd definitely get booted for using F# in a C# shop. I love F# but would never use it at my current job because nobody else on my team knows anything about FP, even though the language itself can be learned in about a week since it's so simple.
- forest_dweller 6y agoMy comment was a glib comment and wasn't supposed to be taken too seriously.
- kroltan 6y agoFair enough, but it also outlines a bit of truth that you shouldn't necessarily go against the grain of the programming culture of a workplace just due to personal preference.
- elamje 6y agoYes, I’d love to use F#, but not a lot of opportunities out there. And .NET, like you mentioned, is very C# focused which would become frustrating at times.
- jackfoxy 6y agoMy experience with F# over 7 years of production coding is for most needs there is already an F# .NET library or wrapper over an OO library. It's mostly only very specialized needs that drive me to an OO .NET library.
- throwaway_pdp09 6y agoRecord looks like scala case classes. I miss them; they save a lot of mucking about (like, loads). Immutability is an extra, it's the brevity it brings.
- jimbob45 6y agoAren’t records pretty trivial to create as a custom class in C#?
- 4rt 6y agothey compile to classes in the background. the benefit is in the succinct syntax, value-type style equality checks and built in immutability (via the new init-only properties).
- smt88 6y agoAbsolutely not. Records generate a lot of code when you compile them, including methods to compare by value. The boilerplate required to do that in C# without records is extensive and hard to maintain. Even structs, which are supposed to be value objects, are not easy to compare by value. I'll put it this way: records are important enough that I stopped using C# because it lacked something like records, and now I'm going to pick it up again.
- Serow225 6y agoIf you want to see how much code you get 'for free' by defining a record that you used to have to do manually, check out the generated C# here (structural equality, GetHashCode, Deconstruct, cloning for 'with', pretty printing, etc) https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BYAUANwgQAIAzAe3OIF5iA7OAd2IAVEpy6AKAIgClycHkmIAmAAwBKANx48COAGNyCFK3adiXAAIBGccQByEMHBEBLOjGIBBAOZwZc3AHoX5u3RVxiMABbmUMSMKgDWROQArnRqFCRQfkQADgA2EMDEYIFQlnbEAMIAxACcxFCRSUkqMHh0JnBQSRCKPnoAdABK0TDmpm355GBJ5imIAMqI+OYtQQDeeMSLxNoAzMuixACSUACiAB7wCHUpm3Tm1rMAvniXQA== https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+BYAUANwgQAI...
- Jordanpomeroy 6y agoThat’s neat! It’s also generating code for the main method, which is also a new c# 9 feature
- Renaud 6y agoAren't records meant to be immutable? I don't understand why properties have setters in the generated definition of the record. I'm missing something.