Parameter types as constraints

Option 1

"Parameters" section with optional is clause.

function logarithm is real:
    parameters:
        number is real. -- The number to find the log of.
        base   is real. -- The logarithm base.

    preconditions:
        number > 0.
        base   > 1.

    return {some calculation}.

Pros:

Cons:

Option 2

Inline parameters with optional parameters section.

function logarithm(number, base) is real:
    parameters:
        number is real. -- The number to find the log of.
        base   is real. -- The logarithm base.

    preconditions:
        number > 0.
        base   > 1.

    return {some calculation}.

Pros:

Cons:

Option 3

Inline parameters with parameter types specified as requirements.

function logarithm(number, base) is real:
    requires:
        number is real.
        base   is real.

    preconditions:
        number > 0.
        base   > 1.

    return {some calculation}.

Pros:

Cons:

Alternative

More generalized polymorphism. Right now polymorphism in OO languages selects the most "specific" function based on which function has the most specific type specifications. Generalized polymorphism would select the function with the most specific constraints (requirements). Determining if a set of constraints is more specific than another may be difficult or impossible in general, though...

Edit this page | 6 years, 11 months old