[PRL] IOP and disjoint unions

William D Clinger will at ccs.neu.edu
Thu Apr 28 16:13:08 EDT 2005


> When you're writing OO code in an interface-oriented way, how do you 
> encode disjoint unions? The typical way to do it in class-based 
> languages is to encode the variants as subclasses of a base class.

I would either do it the way Carl suggested (by allowing the methods
of the interface to throw dynamic exceptions, which sorta defeats
the purpose of static type-checking), or by writing a separate
interface for each variant, plus yet another separate interface
for the union type that consists of nothing but predicates and
conversions, where the conversions are allowed to throw dynamic
exceptions if they do not apply (which defeats the purpose of
static type-checking only for the conversions, which is a structured
use of dynamic exceptions that you could encapsulate within a macro
if macros were only available in your language), e.g.

interface LambdaTerm {

    // Predicates that report whether this term is of the queried form.
    boolean isAbs();
    boolean isVar();
    boolean isApp();

    // Conversions from LambdaTerm to its summands.

    Abstraction toAbs () throws IllegalConversion;
    Variable toVar () throws IllegalConversion;
    Application toApp () throws IllegalConversion;
}

Will



More information about the PRL mailing list