Skip to content

Implication of polymorphic Amount to round-trip conversions #72

@sffc

Description

@sffc

The current Amount proposal allows code such as

let amt1 = new Amount(2.5, { unit: "meter" });
let amt2 = amt1.withNumber(amt1.toNumber() * 2);

Without the helper functions withNumber and toNumber, the code is still possible, though a bit more verbose, perhaps worth moving into a helper function:

function doubleAmount(amt) {
    let number = Number.parseFloat(amt.toString());
    return new Amount(number * 2, { unit: amt.unit });
}

A concern here is that if developers are encouraged to use such a pattern, the resulting code is less efficient than if they had not converted to an Amount, if the Amount is stored as a decimal-like type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions