Skip to content

Do not require explicit casting for small comptime literals that can fit in a typed integer #747

@tiehuis

Description

@tiehuis

Currently we emit an error if we attempt to pass a comptime literal to be formatted.

printf("{}", 5);
error: parameter of type '(integer literal)' requires comptime

This example requires that the 5 be explicitly cast to an integer of specified size before we can actually print.

printf("{}", u64(5));

Often, we just want any readable value we can so we could instead check whether the literal fits into the range of a specific large integer type (i.e. u64) and cast within the printf routine.

This proposal suggest doing automatic casting within printf in order to simplify and make code clearer. Also note we have a similar issue for floats, however casting/narrowing there is likely a bit more nuanced in respect to preserving precision.

The following would compile and work if accepted.

printf("{}", 5);

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions