News for hackers. Stuff that matters.
Re:Deep Dive Into C# 9 by dullgiulio on Friday December 06, 2019 @04:19AM (#21720262)
What is the "lightweight" part of records? Is it because there is no need for a vtable as there is no inheritance?
Re:Deep Dive Into C# 9 by merb on Friday December 06, 2019 @04:24AM (#21720280)
lightweight means they have structural equality and are immutable. this is already a huge bonus. microsoft thought people will add methods to them, thus used the class type.
Re:Deep Dive Into C# 9 by dullgiulio on Friday December 06, 2019 @04:50AM (#21720393)
Thanks. I don't really understand what is light about that.

Being able to compare memory to compare two object doesn't make an object any lighter, compared to magically calling a compare method on the object itself (can be decided at compilation time, thus no performance penalty: it depends on what the compare method does.)

Immutability also doesn't make the object lighter, it's again a compile-time property.

I am still missing something...

Re:Deep Dive Into C# 9 by merb on Friday December 06, 2019 @04:57AM (#21720430)
yeah as said, normally people would see records/data classes as classes that don't use the same amount of memory than a normal class. i.e. some kind of immutable struct. that's why I'm a little bit salty about the feature.

currently records are more comparable to the scala case classes, where you have automatic destructors, better equality, immutability, etc... and not necessary a compacter memory data.

reading the proposal makes more sense than this deep dive, since it explains the reasoning why they don't added special cased data classes.

Re:Deep Dive Into C# 9 by UK-Al05 on Friday December 06, 2019 @05:55AM (#21720684)
Previously you had to write a lot of code to implement equality/compare etc.

Records automatically implement them. So its lightweight in that you need less code.

Re:Deep Dive Into C# 9 by alkonaut on Friday December 06, 2019 @05:36AM (#21720605)
99% of the time when I make a record type with a "with" it's a color, a vector etc. Not sure I really understand why the records can't be either classes or structs.