Skip to content

Automatically compute comptime function arguments at compile-time? #7384

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

Closed
jshholland opened this issue Dec 10, 2020 · 2 comments
Closed

Automatically compute comptime function arguments at compile-time? #7384

jshholland opened this issue Dec 10, 2020 · 2 comments
Milestone

Comments

@jshholland
Copy link
Contributor

The following fails to compile:

const std = @import("std");

pub fn main() void {
    var arr = [_]u16{ 38, 283, 293, 2838, 1823, 183, 123, 23, 193, 949 };
    std.sort.sort(u16, &arr, {}, std.sort.asc(u16));
    for (arr) |n| std.debug.print("{}\n", .{n});
}

with the error:

./foo.zig:5:46: error: runtime value cannot be passed to comptime arg
    std.sort.sort(u16, &arr, {}, std.sort.asc(u16));
                                             ^

This is easily fixed by changing it to comptime std.sort.asc(u16). But shouldn't the compiler know from the definition of std.sort.sort:

pub fn sort(
    comptime T: type,
    items: []T,
    context: anytype,
    comptime lessThan: fn (context: @TypeOf(context), lhs: T, rhs: T) bool,
) void {

that the lessThan argument is needed at comptime, and therefore evaluate it then without being explicit? Or is this an instance of "Communicate intent precisely" (or some other aspect of The Zen of Zig)? Note that I don't have to say comptime u16.

@jshholland
Copy link
Contributor Author

I'm not sure if this is related to #5672

@Vexu
Copy link
Member

Vexu commented Dec 10, 2020

Duplicate of #1708

@Vexu Vexu marked this as a duplicate of #1708 Dec 10, 2020
@Vexu Vexu closed this as completed Dec 10, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants