amantinband/error-or

Potential misuse

Closed this issue · 5 comments

The current API allows the following which is not the intended use.

public ErrorOr<string> SendMessage()
{
    return new ErrorOr<string>();
}

It must be like this. However the API does not enforces it because struct must have a default constructor.

public ErrorOr<string> SendMessage()
{
    return "Hello";
}

Thanks Hossain, that's a good point, but as you said, there isn't much we can do about this. I don't want to give up on ErrorOr being a struct since it doesn't allocate memory

you can throw an error if the default constructor is called like the following, this will at least inform the consumer about the misuse.

public ErrorOr()
{
    throw new InvalidOperationException("Invoking Parameterless constructor is not allowed.");
}

added as part of #105

will be available as part of ErrorOr 3.x

Note: This went out with 2.1.0 not 3.x. with #106