C
C#15mo ago

❔ how to create a class equivalent to another

No description
26 Replies
Omnissiah
Omnissiah15mo ago
look at the doc for the records, you don't need all that stuff record HumanCouple(Human first, Human second);
fæ
OP15mo ago
thats a primary constructor? ok hold on
fæ
OP15mo ago
are these equivalent?
No description
Angius
Angius15mo ago
More or less
Mango
Mango15mo ago
You can even look into ValueTuples, you don’t need to declare a Record
Kiel
Kiel15mo ago
@Many Nights. if you would really like to you can get even fancier with the record
public record HumanCouple(Human First, Human Second)
{
public static implicit operator HumanCouple((Human First, Human Second) tuple) => new(tuple.First, tuple.Second);
}
public record HumanCouple(Human First, Human Second)
{
public static implicit operator HumanCouple((Human First, Human Second) tuple) => new(tuple.First, tuple.Second);
}
Kiel
Kiel15mo ago
No description
Kiel
Kiel15mo ago
if you wanted lol
Mango
Mango15mo ago
Why use a Record over a ValueTuple for simple data storage/move? Not trying to argue against it, seems like extra steps to do something as basic as return two objects at the same time from a method.
Angius
Angius15mo ago
It's more strongly typed, and it's just plain easier to use when it needs to be passed around It's easier to make a method that takes a Couple as a parameter, rather than a (Human first, Human second) It's also more descriptive After all, (Human first, Human second) could refer to mortal enemies, besties, parents, organ donor and the patient who got it, etc Couple or HumanCouple immediately tells you what it is
Mango
Mango15mo ago
So it adds readability
Angius
Angius15mo ago
Now, you can (or will be able to?) alias tuples with names But it's only on a per-file level, IIRC Yep, adds readability
Mango
Mango15mo ago
Nothing technical
Angius
Angius15mo ago
No, it's not like there's a performance benefit or something Not that I know of, at least
Mango
Mango15mo ago
What if you had a ValueTuple of (Person husband, Person wife) GetCouple()?
Angius
Angius15mo ago
Yeah, that is more understandable But then you have to take care to keep naming it like that Because names for tuple elements don't matter, IIRC
Mango
Mango15mo ago
That is right
Angius
Angius15mo ago
So this will be assignable to a variable of type (Person justADude, Person mortalEnemy)
Mango
Mango15mo ago
The benefit is that you don’t have to write up a Record, but at the expense that data flow can get lost in the weeds easier Benefit of Record is that there’s stricter data flow so it’s harder for intent to get lost But you do have to create a new Record everytime you want to use one
Murten
Murten15mo ago
That's not very 2023 of you.
Mango
Mango15mo ago
So is using JS for backend but people still do LULLLLLL always analyze technical benefits first, abstract benefits come second. But if the abstract benefits outweigh the technical Then that choice wins
Murten
Murten15mo ago
Making a record takes like 2 seconds though.
Mango
Mango15mo ago
Making a single record takes 2 second You think an app will only have 1 record in its lifetime? I dunno about you but I try to keep my codebase surface area as small as possible, less areas to debug.
Angius
Angius15mo ago
Then you could argue in favour of never using classes, just tuples and extension methods on them
Mango
Mango15mo ago
You could, but I’m not dense enough to know that not using classes would be better Use it if it makes sense to
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server