Skip to content

[Question] More elegant solution than nested Match #195

@Hiranus

Description

@Hiranus

I am dealing with parsing and validating xml and I wanted to avoid throw, so I decided to try OneOf, but how do I correctly handle multiple nested OneOf.Match() to not make them look like a ladder?

For example this - each call can result in XmlException being returned instead of whatever I want.

ValidationResult ValidateXML (string xml)
{
    XDocument doc = ParseIntoXDoc(xml);
    Version ver = ExtractVersion(doc);
    return ValidateXML(ver, doc);
}

Normally I would just have if after everything to check if I got null due to whatever reason, but OneOf simplifies it to this:

ValidationResult ValidateXML (string xml)
{
    return ParseIntoXDoc(xml).Match(
        ex => ValidationResult(ex),
        doc => ExtractVersion(doc).Match(
            ex => ValidationResult(ex),
            ver => ValidateXML(ver, doc);
        ));
}

After a few more checks like above it will look like if "ladder" and I really dislike that. Is there a more elegant solution to this?

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