Skip to content

Create Nullable IValueConverters, and Add Nullable Static Analyzers#200

Merged
TheCodeTraveler merged 9 commits intomainfrom
Add-Nullable-Static-Analysis
Nov 23, 2021
Merged

Create Nullable IValueConverters, and Add Nullable Static Analyzers#200
TheCodeTraveler merged 9 commits intomainfrom
Add-Nullable-Static-Analysis

Conversation

@TheCodeTraveler
Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler commented Nov 23, 2021

Description

Create Nullable IValueConverters

ICommunityToolkitValueConverter

public interface ICommunityToolkitValueConverter : IValueConverter
{
	new object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture);

	new object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture);

	object? IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
		Convert(value, targetType, parameter, culture);

	object? IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
		ConvertBack(value, targetType, parameter, culture);
}

ICommunityToolkitIMultiValueConverter

public interface ICommunityToolkitMultiValueConverter : IMultiValueConverter
{
	new object? Convert(object[]? values, Type? targetType, object? parameter, CultureInfo? culture);

	new object[]? ConvertBack(object? value, Type[]? targetTypes, object? parameter, CultureInfo? culture);

	object? IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture) =>
		Convert(values, targetType, parameter, culture);

	object[]? IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) =>
		ConvertBack(value, targetTypes, parameter, culture);
}

This PR creates ICommunityToolkitValueConverter and ICommunityToolkitIMultiValueConverter.

Why?

The MAUI team has not yet enabled nullable (#nullable enable) for Microsoft.Maui.Controls.IValueConverter. This means that, until they enable nullable, we have to assume that a nullable value can be passed into the methods and that a nullable value is required to return.

The best way to enforce this going forward is to create our own nullable implementations of IValueConverter that will be used by each Converter in the toolkit.

We can also leverage [return: Null] to let our users know that we are not returning null when we are 100% sure that Convert always returns a non-null value. For example, BoolToObjectConverter.ConvertBack will always return a bool.

Once the MAUI team enables nullable on IValueConverter, and they've decided which parameters should/shouldn't be null for Convert and ConvertBack, we can revisit ICommunityToolkitValueConverter and ICommunityToolkitIMultiValueConverter.

Nullable Static Analyzers

This PR has a secondary goal of implementing Nullable Static Analyzers.

Why?

We have Nullable enabled by default, and, as responsible library maintainers, we owe a responsibility to our users to always handle null properly to make their lives easier.

Detailed Solution

TBD

@TheCodeTraveler TheCodeTraveler changed the title Add Nullable Static Analyzers Create ICommunityToolkitValueConverter, and Add Nullable Static Analyzers Nov 23, 2021
@TheCodeTraveler TheCodeTraveler changed the title Create ICommunityToolkitValueConverter, and Add Nullable Static Analyzers Create Nullable IValueConverters, and Add Nullable Static Analyzers Nov 23, 2021
@TheCodeTraveler TheCodeTraveler marked this pull request as ready for review November 23, 2021 10:53
@TheCodeTraveler TheCodeTraveler merged commit c669a11 into main Nov 23, 2021
@delete-merged-branch delete-merged-branch bot deleted the Add-Nullable-Static-Analysis branch November 23, 2021 13:29
@github-actions github-actions bot locked and limited conversation to collaborators Nov 23, 2024
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.

2 participants