-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
design flaw: 'type' type cannot be used with address-of operator #588
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
Comments
C++ has a function I admit it's a little... unzenful. But it'd be low-impact for something that's probably rare. I don't know. What do you think? (btw, besides the syntax, Zig is okay with pointers and slices of types?) |
Yes, at compile time. |
What is |
Consider this code:
This works fine. The value printed from the
@compileLog
statement is&i32
. This makes sense becauseb
is a pointer toa
.Now let's do it with a
type
:It doesn't work, because the
&
operator works differently fortype
than other types. Here,b
is a pointer toi32
instead of&type
which is how we wanted to use it.This prevents other things from working too; for example if you had a
[]type{i32, u8, f64}
and you tried to use afor
loop, it crashes the compiler because internally a for loop uses the&
operator on the array element.This is a design flaw in zig; we can't have it both ways.
The only reasonable way I can think of to fix this so far is to introduce a new operator, so we don't have this double-purposed
&
. For example:^x
would be a pointer to typex
.&x
would be taking the address ofx
.I'm inclined to leave
&
as the address-of operator because that is the same as C. Using&
as the pointer-to- operator is already different than C, so it's less cost to people learning the language to change it.The text was updated successfully, but these errors were encountered: