✅ Nested Record Inheritance
I'm trying a bit of inheritance shenanigans, the API I'm working with wraps everything in a data object, which usually has two fields that always appear:
This way I can keep a base record for when I need to access cooldown/character in a generic method, but I can still the define the fields that are different:
However, I get an error:
0>FightResult.cs(6,43): Error CS8866 : Record member 'Artifacts.Model.Characters.CharacterActionResult.Data' must be a readable instance property or field of type 'Artifacts.Model.Characters.FightResultData' to match positional parameter 'Data'.
Am I looking over something?13 Replies
Compile C# code in #bot-spam, use
!eval
Example:
Lisa
REPL Result: Failure
Exception: CompilationErrorException
Quoted by
<@100644656710180864> from #bot-spam (click here)
Compile: 345.580ms | Execution: 0.000ms | React with ❌ to remove this embed.
What do you actually want to have happen here?
I mostly just want a contract so I can access Cooldown/Character across the different result types
And if that reduces some duplication, that's a bonus
No, as in literally what do you want to happen? What properties? What are their types?
So I can
records can't do that, either you use a class to being able to declare
new
/override
or you don't use inheritance, i'm afraid:salute2: Throwing it into a class then, Couldn't find that limitation in the documentation so I figured I was missing something
Solved by not hard locking on trying to make records work for no reason:
I know you said solved, but I don't think it's actually solved here. Let me ask a different way: write out, in full form, what you wanted Data to be in both of these locations
The abstraction should include:
While the inheriting object should be able to add extras, such that the complete object contains:
So let's re-examine what you originally wrote. In your FightResult, you have a FightResultData, as opposed to a CharacterActionResultData in the base type
Which of those is the type you actually want? What do you want to get when you have an instance of a FightResult?
When I have a FightResult I actually want FightResultData.
When I have a CharacterActionResult I actually want CharacterActionResultData
Ok, so the problem is that properties don't work this way
Let me ask another question: why were you using records in the first place?