Skip to content

Commit a72cf50

Browse files
author
Ben Striegel
committed
Further rationale and alternatives
1 parent 2f3e4c9 commit a72cf50

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

text/0000-associated-constants-on-ints.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ use std::u32;
9292
assert_eq!(u32::MAX, u32::max_value());
9393
```
9494

95+
The fact that this sort of shadowing of primitive types works in the first place is surprising
96+
even to experience Rust programmers; the fact that such a pattern is seemingly encouraged by
97+
the standard library is even more of a surprise. By making this change we would be able to
98+
remove all modules in the standard library whose names shadow primitive types.
99+
95100
# Guide-level explanation
96101
[guide-level-explanation]: #guide-level-explanation
97102

@@ -151,12 +156,12 @@ no longer accessible to users of the new edition.
151156
However, given how many `MAX` and `MIN` constants there are in the stdlib,
152157
it is easy to argue that such unprefixed constants in the wild would be confusing,
153158
and ought to be avoided in the first place. In any case, users desperate for such behavior
154-
will be trivially capable of doing `const MAX = i32::MAX; foo(MAX, MAX);`
159+
will be trivially capable of doing `const MAX: i32 = i32::MAX; foo(MAX, MAX);`
155160

156161
# Alternatives
157162
[alternatives]: #alternatives
158163

159-
Unlike the twelve integral modules, the two floating-point modules would not themselves be
164+
- Unlike the twelve integral modules, the two floating-point modules would not themselves be
160165
entirely deprecated by the changes proposed here. This is because the `std::f32` and `std::f64`
161166
modules each contain a `consts` submodule, in which reside constants of a more mathematical bent
162167
(the sort of things other languages might put in a `std::math` module).
@@ -166,6 +171,21 @@ separation is not consistent with the existing set of associated functions imple
166171
and `f64`, which consist of a mix of both functions concerned with mathematical operations
167172
(e.g. `f32::atanh`) and functions concerned with machine representation (e.g.
168173
`f32::is_sign_negative`). However, although earlier versions of this RFC proposed deprecating
169-
`std::{f32, f64}::consts`, the current version does not do so, as this was met with mild resistance
170-
(and, in any case, the greatest gains from this RFC will be its impact on the integral modules).
174+
`std::{f32, f64}::consts` (and thereby `std::{f32, f64}` as well), the current version does not do
175+
so, as this was met with mild resistance (and, in any case, the greatest gains from this RFC will
176+
be its impact on the integral modules).
171177
Ultimately, there is no reason that such a change could not be left to a future RFC if desired.
178+
However, one alternative design would be to turn all the constants in `{f32, f64}` into associated
179+
consts as well, which would leave no more modules in the standard library that shadow primitive
180+
types. A different alternative would be to restrict this RFC only to the integral modules, leaving
181+
f32 and f64 for a future RFC, since the integral modules are the most important aspect of this
182+
RFC and it would be a shame for them to get bogged down by the unrelated concerns of the
183+
floating-point modules.
184+
185+
- Rather than immediately deprecating the existing items in the standard library, we could add
186+
the new associated consts without any corresponding deprecations. The downside of this idea is
187+
that we now have *three* ways of doing the exact same thing, and without deprecation warnings
188+
(and their associated notes) there is little enough to guide users as to which is solution
189+
is the idiomatic one. It is the author's opinion that there is no downside to deprecation
190+
warnings in this case, especially since mitigation of the warning is trivial (as discussed in
191+
the Drawbacks section above).

0 commit comments

Comments
 (0)