C
C#8mo ago

❔ how to create a class equivalent to another

No description
26 Replies
WEIRD FLEX
WEIRD FLEX8mo ago
look at the doc for the records, you don't need all that stuff record HumanCouple(Human first, Human second);
fæ
8mo ago
thats a primary constructor? ok hold on
fæ
8mo ago
are these equivalent?
No description
Angius
Angius8mo ago
More or less
Mango
Mango8mo ago
You can even look into ValueTuples, you don’t need to declare a Record
Kiel
Kiel8mo 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
Kiel8mo ago
No description
Kiel
Kiel8mo ago
if you wanted lol
Mango
Mango8mo 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
Angius8mo 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
Mango8mo ago
So it adds readability
Angius
Angius8mo 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
Mango8mo ago
Nothing technical
Angius
Angius8mo ago
No, it's not like there's a performance benefit or something Not that I know of, at least
Mango
Mango8mo ago
What if you had a ValueTuple of (Person husband, Person wife) GetCouple()?
Angius
Angius8mo 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
Mango8mo ago
That is right
Angius
Angius8mo ago
So this will be assignable to a variable of type (Person justADude, Person mortalEnemy)
Mango
Mango8mo 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
Murten8mo ago
That's not very 2023 of you.
Mango
Mango8mo 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
Murten8mo ago
Making a record takes like 2 seconds though.
Mango
Mango8mo 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
Angius8mo ago
Then you could argue in favour of never using classes, just tuples and extension methods on them
Mango
Mango8mo 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
Accord8mo 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
More Posts