Skip to content

Proposal: Rename comptime_int and comptime_float to anyint and anyfloat #7492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ghost opened this issue Dec 18, 2020 · 6 comments
Open

Proposal: Rename comptime_int and comptime_float to anyint and anyfloat #7492

ghost opened this issue Dec 18, 2020 · 6 comments
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ghost
Copy link

ghost commented Dec 18, 2020

EDIT: See @RaphiJS6-13 below for a better idea.

Minor part of #5895 that was not moved out following the post-#7396 purge. Remedying that now.

When a function takes an argument or a struct has a field of type comptime_int or comptime_float, the declaration is already annotated with comptime. A second comptime in the type name just seems repetitive. Removing it would have psychological benefits as well: the types seem less like arcane compiler details and more like a normal, everyday part of code, which they are, and makes it more obvious why every literal requires a type (status quo: "What's the type of this? It's an int. All looks normal. compile error Excuse me? flips through docs Oh, yeah, it's a comptime_int. Right."; this proposal: "What's the type of this? It's an int. Oh, yeah, that's a comptime-only type, isn't it? Better coerce it.").

There is precedent for this: type, anytype and such are comptime-only (or have restrictions on their use), and do not have any special markings indicating this. Continuing this pattern with int and float I believe will be a valuable step toward normalising comptime in the minds of users. I think of it like this: comptime_int and comptime_float sound like specialsed, arcane types, but they're actually in a way the most basic numeric types in the language, the types which coerce to all others. They should have suitably "basic" names.

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Dec 18, 2020
@Vexu Vexu added the breaking Implementing this issue could cause existing code to no longer compile or have different behavior. label Dec 18, 2020
@Vexu Vexu added this to the 0.8.0 milestone Dec 18, 2020
@tecanec
Copy link
Contributor

tecanec commented Dec 19, 2020

What about sizeless_int and sizeless_float? It's easy to tell that type is comptime-only, because there's an obvious reason why: zig uses static types. int and float don't have that benefit, because runtime ints and floats exist, and in most languages, int and float are runtime types. sizeless_int and sizeless_float gives us a clear reason why they are comptime-only, and it makes it clear that number literals would have those types.

@ghost
Copy link

ghost commented Dec 20, 2020

type does not need an explicit comptime_ prefix, because types are always comptime in Zig. Numeric types, on the other hand, have both comptime and runtime variants, so it makes sense to distinguish them.

I think people will spend more time trying to figure out what's wrong with their fn foo(x: int) int { ... } definition, than they will spend being confused by comptime_int in error messages.

@RaphiJS6-13
Copy link

RaphiJS6-13 commented Dec 27, 2020

What about anyint and anyfloat? It's more intuitive, and is consistent with anytype, anyframe, and anyerror.

Edit: anytype might be renamed, see #5893.

@ghost
Copy link
Author

ghost commented Dec 27, 2020

That's good, actually. I like that better.

@ghost ghost changed the title Proposal: Rename comptime_int and comptime_float to int and float Proposal: Rename comptime_int and comptime_float to anyint and anyfloat Dec 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@J-MR-T
Copy link

J-MR-T commented Jun 29, 2024

What about anyint and anyfloat? It's more intuitive, and is consistent with anytype, anyframe, and anyerror.

(any time I talk about ints in this comment, read ints or floats)

Naming them after these makes the whole any prefix less consistent in my opinion, not more. You can't declare a local variable anytype, but you can declare it comptime_int, because that's actually a type (although just comptime), as opposed to anytype.

Of course, this is why #5893 exists, but the suggestion above still implies anyint would fit better with anytype. To be honest, I don't see a need to rename comptime_int, especially not if it makes error messages about integer literals that are not coerced less clear. "anyint is a problem? Shouldn't I be able to use anyint if I don't care which int I need?". It's the same problem as with anytype, just from the other direction, which is why I would suggest to avoid introducing any more of this any- confusion.

@nektro
Copy link
Contributor

nektro commented Jun 29, 2024

You can't declare a local variable anytype

this the only any* that this applies to. anyframe, anyerror, *anyopaque can all be instantiated.

@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

7 participants