Sum Type とは?
sum type は、tagged union やバリアントとも呼ばれ、固定された選択肢の集合のうち 1 つを値として持つ型で、どれであるかを識別する tag を伴います。各選択肢は独自の種類のデータを持てます。取りうる値の数はその variant の可能性の和であり、これが名前の由来です。
なぜ重要か
sum type は「これらのケースのいずれか」を正確にモデル化し、コードにすべてのケースの考慮を強制します。多くの場合、網羅性解析によってチェックされます。緩やかに関連づいた flag で選択肢を表現することに起因するバグの一群を丸ごと排除します。
関連ガイド
What Is a Product Type?A product type groups several values together at once, like a struct or tuple, so a value carries all of its…
What Is an Algebraic Data Type?An algebraic data type composes types as combinations of fields and alternatives, modeling data as products a…
What Is Pattern Matching?Pattern matching inspects a value against a set of shapes, selecting a branch and binding parts of the value…