5 ms·
I think it's been elegant for a number of years now. Pretty much ever since Linq became usable.
by ShinTakuya 5y ago
I think it's been elegant for a number of years now. Pretty much ever since Linq became usable.
- pipeline_peak 5y agoYeah, lambdas and reflection really.
- vips7L 5y agoPersonally I think reflection is the devil and the reason why we can’t abandon VMs for native compilers.
- pipeline_peak 5y agoReflection does away the need to manually implement serializers. In a growing codebase where data models transform and references to those models begin to rot, that is incredibly useful.
- vips7L 5y agoThat can be done with code generation at compile time. We don’t need to reflect at runtime for that.
- Const-me 5y agoUsability is worse, IMO. Serialization libraries need to be decoupled from the records being serialized. These two things are compiled into different assemblies, and often written by different people. Still possible to replace with compile time codegen, but the implementation gonna be complicated and fragile.
- kaidon 5y agoType inference with `var` as well; which was necessary for LINQ anyway IIRC.
- pipeline_peak 5y agoWhy is it useful for LINQ? I feel like it’s mostly dependent on lambdas and generics.
- oaiey 5y agoThere are anonymous types for intermediate results in the linq stream. And since they are anonymous types you need the c var keyword when working with the outcome.
- pipeline_peak 5y ago….All these years and I’ve never known about Anonymous types. I know about records and dynamic objects. I’m guessing they aren’t used often because I have yet to find them.
- caseymarquis 5y agoLINQ GroupBy() is the most common use I can think of. I would definitely look into that as it's a nice tool to have. Otherwise, keep them in mind if you're ever pulling data from an external source (SQL, JSON, GQL, Redis, etc) and you're annoyed about having to make a class for some trivial operation. If a library doesn't have an API for using them with generics, you can paper over that with: T fn<T>(T throwAway) => Library.Deserialize<T>();
- to11mtm 5y agoAnother example of Anonymous types would be a simple .Select(x=> new {x.ID, x.Name}); Sometimes people lump these in with dynamic objects but those are a very different class of thing indeed. Now, what would be -really- nice is if we had a way to specify that an anonymous type implements an interface. That could potentially simplify a -lot- of DTO modeling.