4 ms·
What does "becoming const" mean?
by k__ 6y ago
What does "becoming const" mean?
- Ygg2 6y agoMeans that functions are evaluated at compile time. However in this case, these are const generics, so you no longer need to use macro to generate [char;1],[char;2]... You just write [char; N] where N is const. Compiler determines which values of N are used and generates for them an appropriate version.
- steveklabnik 6y agocan be, you can call them at runtime too. And to be clear, const generics aren't stable yet, they're being used as an implementation detail of the standard library here, you cannot yet use them on stable in your code.
- Nullabillity 6y ago> However in this case, these are const generics, so you no longer need to use macro to generate [char;1],[char;2]... Const generics still aren't stable. I believe the GP was talking about the list of new `const fn`s in the release (near the bottom of the post).
- happythomist 6y agoI believe it's similar to "constexpr" in C++ -- it means those functions can now be evaluated at compile time.
- deleted 6y ago[deleted]
- dahfizz 6y agohttps://doc.rust-lang.org/reference/items/functions.html#const-functions https://doc.rust-lang.org/reference/items/functions.html#con... Basically, const functions can be used in places regular functions can't because they are directly "run" by the compiler at compile time.
- mden 6y agoConst values are inlined at compile time wherever they are used. Const functions let you run code to define const values. This code will be run at compile time before the const values are inlined. For example let's say you have a constant for PI but you need 2*PI. You could do the multiplication in const context, meaning the multiplication will happen during compilation and not execution. Slowly more and more of the language is becoming usable in const context so more advanced things can be done during compile time.
- colejohnson66 6y agoWell, if you need `2*PI`, there’s now `f32::TAU` and `f64::TAU` :) You’re still correct though
- hinkley 6y agoI like that const functions occupy a space between a normal function and hygienic macros. You can avoid writing out lookup tables long-form where the entries are very predictable. For instance you could write a base64 encoder and decoder without putting ["a", "b", "c"... ] in your code base, and then have to worry if you accidentally put "D" in there twice. The typical alternative in other languages is to evaluate this at startup time, which adds latency to your program. Compile-time function evaluation is also one of the features of the Jai programming language. If you can't get people to use your language, getting them to steal all your features is a good consolation prize.
- jsheard 6y ago> If you can't get people to use your language, getting them to steal all your features is a good consolation prize. It's difficult to use Jai as long as it only exists on Jonathan Blows computer, he still hasn't released it