1

Lets say I have class:

public class Result<TValue,TFailureReason>: ActionResult<TFailureReason>
{
//...
    public static implicit operator Result<TValue, TFailureReason>(TValue value)
        => Succeeded(value);

    public static implicit operator Result<TValue, TFailureReason>(TFailureReason failureReason)
        => Failed(failureReason);
}

And I'm trying to use it here:

public async Task<Result<IAppServicePlan, AzureSdkFailure>> GetAppServicePlanAsync(string name, string resourceGroupName)
    {
        try
        {
            IAppServicePlan appServicePlan = await azure.AppServices.AppServicePlans.GetByResourceGroupAsync(resourceGroupName, name, cancellationToken)
            .ConfigureAwait(false);

            return appServicePlan;
        } catch(Exception ex)
        {
            return new AzureSdkFailure(ex);
        }            
    }

But I get compile error:

'IAppServicePlan' to 'Result'. An explicit conversion exists (are you missing a cast?)

However when I change to this:

public async Task<Result<IAppServicePlan, AzureSdkFailure>> GetAppServicePlanAsync(string name, string resourceGroupName)
    {
        try
        {
            IAppServicePlan appServicePlan = await azure.AppServices.AppServicePlans.GetByResourceGroupAsync(resourceGroupName, name, cancellationToken)
            .ConfigureAwait(false);

            return Result<IAppServicePlan, AzureSdkFailure>.Succeeded(appServicePlan);
        } catch(Exception ex)
        {
            return new AzureSdkFailure(ex);
        }            
    }

It works fine.

Why return new AzureSdkFailure(ex); compiles and return appServicePlan; does not?

Dzior
  • 1,485
  • 1
  • 14
  • 30

0 Answers0