-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
all containers anonymous #160
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
One idea is that we can take advantage of the type Point = struct {
x: i32,
y: i32,
}; Now The idea breaks down a bit when you move to generic containers: inline fn List(inline T: type) -> type {
struct {
items: []T,
}
}
fn foo() {
var list1: List(i32) = zeroes;
list1.append(123);
} Here we declared |
Prerequisite to solving this is #169 |
I don't think enforcing anonymous-ness is such a great idea. What if you typedef with different name the same anonymous struct, which name is the compiler going to report? Requiring that all struct are anonymous is drastic. I'm suspecting that this feature will re-create the infamous C++ compiler unreadable error. I prefer readability & maintenance over cleverness of implementation. Is there a real gain here other than simplifying the language grammar? |
Not really. We'd only do this if we solved the compiler errors and debug symbols problem satisfactorily. Then it would help push the "one way to do things" design goal. Consider that this will already be possible. Even if we don't require all structs to be anonymous, it will still be possible to create structs with generic parameters in this way. So we kind of already have to solve this problem. And then if we solve it adequately, it's a simplification to make it the only way to do it. This is my logic. Let me ask this, if the naming problems were solved and compiler errors were ergonomic, would you be in favor of this change? Is there another reason to not do it? |
If ziglang ever want switch statement to do pattern matching (I hope it does!), it'll be hard to do with anonymous struct that has 2 semantically different structures with the same signature, e.g.
Only the name differentiate the semantic in the case of |
Can you give an example of how this pattern matching would work? It seems to me like the expression would be of a certain type, and testing if it was a different type wouldn't make sense. I think I just need a clarifying example. |
The language as is couldn't permit such construct, I'm thinking of scala's case class pattern matching. Could it be done with trait and implementing type?
maybe zig will never have this expressive power. |
Ah, thanks for the example. This is on the table. See #130 |
fn Foo(inline T: type) -> type {
struct {
item: T,
bar: &Bar(T),
fn member() {
bar.member();
}
}
}
fn Bar(inline T: type) -> type {
struct {
item: T,
foo: &Foo(T),
fn member() {
foo.member();
}
}
} This code would need to work correctly if we did this all containers anonymous thing. How it would work is, when the anonymous struct expression is encountered while executing the inline function, we would create the container type and the scope for it, however we would not eagerly scan the declarations - we would leave those lazy like other declarations. So functions Considering that something like this is possible: fn Foo(inline b: bool, inline T1: type, inline T2: type) -> type {
inline var T = T1;
const Result1 = struct {
item: T,
};
T = T2;
const Result2 = struct {
item: T,
};
return if (b) Result1 else Result2;
} If we do something like |
This is done and working and now merged into master branch. |
See previous discussion from #151.
Containers are structs, unions, and enums.
All containers anonymous makes the language even smaller and more elegant. However it presents a big problem to solve: naming.
Is the first variable you assign an anonymous struct to somehow more important in the name?
Should the name of the container be something the user explicitly has control over?
Should the function name and parameters be used to determine the name of the container? What if the function merely chooses between two existing types?
Is this maybe overkill and we shouldn't have all structs be anonymous?
Should containers accept a compile-time-string argument which is the name of the type?
The text was updated successfully, but these errors were encountered: