Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/forbid null items #706

Merged
merged 2 commits into from Jul 22, 2023
Merged

Bugfix/forbid null items #706

merged 2 commits into from Jul 22, 2023

Conversation

PhilipCavanagh
Copy link
Contributor

In response to #692, this PR adds a where t : notnull constraint to Option<T>.
That change proliferates out to IChangeSet and into a lot of extension methods.

I think this constitutes a breaking change, but code that used nullable types may not have worked correctly anyway.

@glennawatson
Copy link
Member

Due to the size of the change and integration testing required it won't be merged straight away. Just a heads up.

@codecov
Copy link

codecov bot commented Jun 12, 2023

Codecov Report

Merging #706 (fb00538) into main (9af334c) will not change coverage.
The diff coverage is 96.75%.

@@           Coverage Diff           @@
##             main     #706   +/-   ##
=======================================
  Coverage   71.90%   71.90%           
=======================================
  Files         216      216           
  Lines       10893    10893           
=======================================
  Hits         7833     7833           
  Misses       3060     3060           
Impacted Files Coverage Δ
src/DynamicData/Aggregation/AggregateEnumerator.cs 70.58% <ø> (ø)
src/DynamicData/Aggregation/AggregationEx.cs 52.63% <ø> (ø)
src/DynamicData/Aggregation/AvgEx.cs 19.51% <ø> (ø)
src/DynamicData/Aggregation/CountEx.cs 25.00% <ø> (ø)
src/DynamicData/Aggregation/MaxEx.cs 82.75% <ø> (ø)
src/DynamicData/Aggregation/StdDevEx.cs 0.00% <ø> (ø)
src/DynamicData/Aggregation/SumEx.cs 6.06% <ø> (ø)
src/DynamicData/Alias/ObservableCacheAlias.cs 0.00% <ø> (ø)
src/DynamicData/Alias/ObservableListAlias.cs 15.00% <ø> (ø)
src/DynamicData/Binding/BindingListAdaptor.cs 88.09% <ø> (ø)
... and 151 more

@PhilipCavanagh
Copy link
Contributor Author

Due to the size of the change and integration testing required it won't be merged straight away. Just a heads up.

I imagined as such. I don't think it should change any behaviour, but it could break existing code if it uses nullable types.

@RolandPheasant
Copy link
Collaborator

I apologise for a long delay. I'd been pondering on whether we should do this, then I forgot !

The one thing which concerns me is that there is potentially a use case for null. A "fix" was introduced to enable it in the case of a transform to allow nulls. I believe the idea was to immediately filter the null out. Doing it this way enabled being able to suppress transforms in certain circumstances.

@oysteinkrog can you remember why you did this, and why I approved - See #216

Regardless, nulls are still a bad idea and I wonder whether we should push this and perhaps introduce a mechanism to Dynamic Data where a null returned by a transform results in a remove event rather than a null update.


/// <summary>
/// Wraps the specified value in an Optional container.
/// </summary>
/// <typeparam name="T">The type of the item.</typeparam>
/// <param name="value">The value.</param>
/// <returns>The optional value.</returns>
public static Optional<T> Some<T>(T value) => new(value);
public static Optional<T> Some<T>(T? value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value could be null as the constructor of optional directly casts a null to a None.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh. Ignore me as I just noticed the T?

@oysteinkrog
Copy link
Contributor

I apologise for a long delay. I'd been pondering on whether we should do this, then I forgot !

The one thing which concerns me is that there is potentially a use case for null. A "fix" was introduced to enable it in the case of a transform to allow nulls. I believe the idea was to immediately filter the null out. Doing it this way enabled being able to suppress transforms in certain circumstances.

@oysteinkrog can you remember why you did this, and why I approved - See #216

Regardless, nulls are still a bad idea and I wonder whether we should push this and perhaps introduce a mechanism to Dynamic Data where a null returned by a transform results in a remove event rather than a null update.

Hmm yes, we used to do this as we have a Transform that is essentially using a Try-pattern, meaning that it may fail.
The code we had that did this has been rewritten now and is now essentially using an Optional<> wrapper to represent the success/failure of the Transform, with an accompanying Filter afterwards, just like you describe.
I support this PR, it's a good change. We were using RX/DD for years before we truly realized that nulls are not supported in RX, and I think making DD consistent with the RX API/contract is a good idea.
This change would have prevented other null-related errors in our RX+DD code :)

@RolandPheasant
Copy link
Collaborator

Thanks for the input @oysteinkrog, that settles it. This PR is good to go.

@glennawatson I'll approve this, but before we release I'd like to do a little house keeping within the codebase. I'll be done with that within a few days.

@RolandPheasant
Copy link
Collaborator

Also @oysteinkrog I may additionally provide the option within the transform to return a null, in which case the item would be removed.

Copy link
Collaborator

@RolandPheasant RolandPheasant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a an excellent contribution to the library,

@RolandPheasant RolandPheasant merged commit a3eccd0 into reactivemarbles:main Jul 22, 2023
3 checks passed
@github-actions
Copy link

github-actions bot commented Aug 6, 2023

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants