Skip to content

Nullability issue with DisposeWith at Bind, OneWayBind #2486

@imckl

Description

@imckl

Describe the bug

The compiler warns about "[CS8631] Nullability of type argument doesn't match constraint type." when building application with c# 8.0 Nullable reference types enabled in .netcore3.1

Steps To Reproduce

Code behind xaml:

this.Bind(ViewModel,
    vm => vm.PropertyA,
    v => v.SomeButton)
    .DisposeWith(disposable);

When build the application, the complier warns about [CS8631]

Expected behavior

Should be without any warning.

It seems that System.Reactive.Disposables.DisposeWith does not except a Nullable value as parameter(which writes as IDisposable Extensions in class DisposableMixins)

I've write a new DisposeWith which accepts nullable value

public static T? DisposeWith<T>(this T? item, CompositeDisposable? compositeDisposable) where T : class?, IDisposable
{
    if (compositeDisposable == null)
    {
        throw new ArgumentNullException(nameof(compositeDisposable));
    }

    if (item == null)
    {
        return null;
    }
            
    compositeDisposable.Add((IDisposable)item);
            
    return item;
}

But it conflicts with original DisposeWith Extension because they share same method signature.
Hence renaming to DisposeWithNullable or some other might be a workaround, tills lots of code should be modified to fix this wanring.

So what's the proper way to resolve this Nullability issue?

Screenshots

image

Environment

  • OS: Windows 10 x64, .net core 3.1
  • Version 11.5.17
  • Device: WPF

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions