Skip to content

SumType.init matches on first type #2

@atilaneves

Description

@atilaneves

It's usually the case that one would want sum types so that invalid states are unrepresentable. However:

import sumtype;
import std.stdio;
alias TheSum = SumType!(string, int);
auto s = TheSum();
s.match!((string s) => writeln("string"), (int i) => writeln("int"))

This will print "string". As I see it the options are:

  • Throw an exception if not initialised
  • assert(false) if not initialised
  • Decide between the current behaviour and throwing via a policy template parameter
  • Add a @disable this(); to SumType

What do you think?

std.variant.Algebraic throws a VariantException for the code above.

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