Skip to content
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

comptime type deduction #984

Closed
bheads opened this issue May 4, 2018 · 4 comments
Closed

comptime type deduction #984

bheads opened this issue May 4, 2018 · 4 comments

Comments

@bheads
Copy link

bheads commented May 4, 2018

It seems like this might become a common pattern for generic coding in zig

pub fn foo(comptime T: type, T: a) void {
}

var a  = 5;
foo(int, a); 

In these cases the compiler knows the type of a, it would be useful if you did not have to supply the type.

foo(a);  //<< much simpler and cleaner

Maybe some reorganization of the function signature.

pub fn foo(T: a, comptime T: type = @typeOf(a)) void  {..}
@bheads bheads changed the title compttime type deduction comptime type deduction May 4, 2018
@andrewrk
Copy link
Member

andrewrk commented May 4, 2018

have you seen this yet?

fn foo(a: var) void {
}

test "aoeu" {
    var a: u32  = 5;
    foo(a); 
}

@bheads
Copy link
Author

bheads commented May 4, 2018

I have not seen that yet, should cover a lot of cases. But what about enforcing same types

pub fn foo(comptime T: type, a : T, b : T)

Would have to include a typeof check in the body

@bheads bheads closed this as completed May 4, 2018
@andrewrk
Copy link
Member

andrewrk commented May 4, 2018

That can work like this:

fn foo(a: var, b: @typeOf(a)) void {}

However perhaps you want to resolve the types with peer type resolution. There's an open issue for that: #439

@bheads
Copy link
Author

bheads commented May 5, 2018

Good to know typeOf works likes this. Thanks.

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

2 participants