Skip to content

Add @compiles builtin #3144

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
marler8997 opened this issue Aug 31, 2019 · 1 comment
Closed

Add @compiles builtin #3144

marler8997 opened this issue Aug 31, 2019 · 1 comment

Comments

@marler8997
Copy link
Contributor

marler8997 commented Aug 31, 2019

@compiles(expr: var) bool

Takes an expression and returns a bool indicating whether the expression compiles.

This can be used to create test failure cases or implement sophisticated compile-time logic for fine-grained control over "generics" specialization.

Dlang equivalent __traits(compiles, <EXPR>) (https://dlang.org/spec/traits.html#compiles)

Example: used for test failure cases

pub fn foo(comptime T: type) type {
    switch (T) {
        builtin.TypeInfo.Pointer => { return T; }
        else => @compileError("foo requires Pointer types but got " ++ @typeName(T));
    }
}

test "foo" {
    // make sure this fails
    std.testing.expect(!@compiles(foo(u8)));
}
@marler8997
Copy link
Contributor Author

Duplicate of #513

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

1 participant