-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
implement @as
builtin and fix result location semantics with regards to type coercion
#3628
Conversation
@@ -61,7 +61,7 @@ pub const State = struct { | |||
} | |||
|
|||
pub fn squeeze(self: *Self, out: []u8) void { | |||
var i = usize(0); | |||
var i = @as(usize, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better idiom would be var i: usize = 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. They are semantically identical, therefore the shorter one is preferred.
Must the type parameter to @as() be comp-time known? For variably sized stack based allocations, it seems that it must be, but if pointers are all the same size, there is an alluring reason to allow runtime-known pointer type casts from @as(), or a close relative. For example, maybe you have a single routine that returns a pointer pointing to a vector whose length is that of the active processor. |
@JesseRMeyer I don't understand what you're suggesting - sounds like a proposal for a separate issue, that could use some code examples to help clarify. |
This branch implements #1757. It breaks a lot of code.
It also hooks up variable declarations which have type annotations into result location:
And it hooks up type coercion into result location semantics:
This makes it easier to read code, since a function call is always a function call now, and type coercion is a lot easier to spot.
It also makes the language simpler, and therefore the compiler easier to work on. Less room for bugs dealing with the ambiguity of function call syntax possibly being a function call, or type coercion.
Merge Checklist
Bear with me on this change. It'll be worth it. As a consolation prize for dealing with your code breaking, this is a direct prerequisite of anonymous struct literals (#685) which I plan to do next. So after you fix all your type coercions, you'll at least get to have a hot new feature to play with soon after.