Skip to content

Implicit null behavior #16

@Jack-Edwards

Description

@Jack-Edwards

Re: #15

An example for discussion:

public record Order
{
    public int Id { get; set; }
    public int InvoiceId { get; set; }
    public Invoice? Invoice { get; set; }
}

public async Task<Maybe<Invoice?>> GetInvoiceForOrderAsync(int orderId)
{
    var result = await _dbContext.Orders
        .Where(x => x.Id == orderId)
        .Select(x => new { x.Invoice })
        .FirstOrDefaultAsync();
        
    if (result is null)
    {
        return Maybe<Invoice?>.None;
    }

    return result.Invoice;
}

Suppose a client wants to view the invoice for a recently placed order. It takes a while for invoices to be created, so the invoice may not exist. This service method tries to abstract this by supporting the following scenarios:

  1. The order does not exist. Expect None.
  2. The order does exist, but the invoice does not exist. Expect Some with a null value.
  3. The order and invoice both exist. Expect Some with an instantiated value.

The problem is, EasyMonads does not currently work this way. The constructor for Maybe returns an instance in the None state if the provided value == null. Scenario 2 (above) is broken.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions