-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Generic consts #2424
Comments
How does this differ from const fn? |
And if you want to use it in a runtime context, altho the types have to be evaluated at compile-time, you can use const POW<const X: usize, 0>: usize = 1;
const POW<const X: usize, const Y: usize>: usize = X * POW<X, Y-1>;
fn do_something(x: usize, y: usize) -> usize {
POW<use x, use y>
} It's not different from const fn - it replaces const fn. The ability to fn do_something(x: usize) -> usize {
POW<use x, 3> // expands/unrolls to x*x*x. this is inherently more flexible than const fn.
}
fn do_something_else(x: usize) -> usize {
POW<use x, use 3> // recurses at runtime (may be unrolled by LLVM tho)
} (Altho the idea of |
I do think that the ability to coerce literals into multiple types without macros would be very useful. Not sure if this is the answer, though. |
This doesn't replace the "type level integers" portion of #1038, so there are currently no RFCs for type level integers, right? (beyond the existing ways to hack them in) |
Are you asking for something different than RFC 2000?
… On Oct 16, 2018, at 2:03 PM, Alex ***@***.***> wrote:
This doesn't replace the "type level integers" portion of #1038, so there are currently no RFCs for type level integers, right? (beyond the existing ways to hack them in)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I would like to have this in order to be able to return a static array with the same number of arguments as was provided in a static input array, like so: fn do_something<N>(elements: [isize; N]) -> [usize; N] {
// Implement, somehow ...
} Is that something you would be able to do with |
@emanuelpalm that's const generics (consts in generics i.e. |
@SoniEx2 Oh. True. I came here via some other issue, and I guess I didn't read the description properly. :P |
Here: const POW<const X: usize, 0>: usize = 1;
const POW<const X: usize, const Y: usize>: usize = X * POW<X, Y-1>;
fn do_something(x: usize, y: usize) -> usize {
POW<use x, use y>
} You seem to be coercing a value known only at runtime into a |
Not to be confused with const generics, but I'll use both to describe the idea.
Let's say you have a const initializer and you want it to be different for every type:
This is the basic generic const construct. Ofc, if you want length also specified:
This creates an array of size L containing only None.
Ofc, you also don't need to use types, consts are fine too:
But perhaps you want it to be recursive:
And you want to calculate Pi with it [1]:
And things of the sort.
Yeah... they'd be nice.
May we have them?
The text was updated successfully, but these errors were encountered: