by ad133 · 4y ago · 0 comments
[Input Data, maybe null] -> Validate field is not null -> Call this method with the assertion.
This is a small bug-bear for me with nullable types and I wish there was a better way to do it, but many languages allow you to smart-cast away nulls, but only within the local scope. If you want to pass a struct-type around which has nullable fields, but you have already checked for non-null (like this one) you need to convert to a different struct-type, which doesn't have the nullability on its fields. I can't think of a good way round this - as you say with the unit test remark, there is nothing to stop another piece of code calling this method with nulls.
Which is exactly what IMO the author should have done. It's actually a reasonable use-case for inheritance:
#nullable enable
record SlackEvent
( int EventId
, string Content
, string? TeamId
);
record TeamSlackEvent
( int EventId
, string Content
, string TeamId
)
: SlackEvent
( EventId : EventId
, Content : Content
, TeamId : TeamId
);