Skip to content

Proposal: Concerning Zig Structs #1250

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
bjornpagen opened this issue Jul 15, 2018 · 4 comments
Closed

Proposal: Concerning Zig Structs #1250

bjornpagen opened this issue Jul 15, 2018 · 4 comments

Comments

@bjornpagen
Copy link

bjornpagen commented Jul 15, 2018

Zig needs to figure out what a struct is, and stick to its guns about it

Is it an encapsulating class? Is it a general namespace? A c-like struct?
It's clear that users of zig all have different answers (and visions) to what they want a struct to look like.

Structs in Zig as they stand right now, do not fit into any of the above categories. They have aspects of being classes, since they can contain functions, and public and private fields (although they don't do anything: #569. They have aspects of being namespaces, as they can contain functions. And they have aspects of being c-structs, since they are concrete, unabstracted, blocks of data. Zig Structs are just an unorganized amalgamation of these concepts.

As a result, Zig structs not only cause complexity elsewhere, they are not able to properly function as any of the abstractions mentioned above: an object, a namespace, or even a c-struct. No matter how the programmer tries to use Zig structs, they will run into problems. Here's a short list of github issues:

This is a question that needs to be answered as soon as possible.

There are many solutions to this problem. We can implement proper OO, or proper Rust-like traits, or namespacing, whatever. We just have to pick one and go with it, or else this unproductive bickering will continue forever.


Now, done summarizing. Here's what I think.

Zig structs should be more or less like c-structs. No functions, no pub/priv, only concrete values. Note that this still allows function pointers to be in a c-struct, since these are concrete values. There will be no more worrying about where functions are defined, or how to do inheritance, or implement Traits. A struct will be a struct, and any futher abstraction will come out of cleverness out of using them.

If anyone thinks C-structs are not good enough abstractions, read through the Plan 9 C library here. It is the most beautiful public interface I have ever seen (much better than OO). If Zig wants to be a C alternative, this is, in my eyes, the only way to do this right.

But of course, I know nothing compared to some of you, and this needs more all-around discussion. But struct should be finalized as something, and that's that.

@isaachier
Copy link
Contributor

isaachier commented Jul 15, 2018

I don't think methods or pub are a problem. If anything, the only advantage Zig has over C is a better model for encapsulation. Then again, I think there are a few clear cut rules that will distinguish Zig from the classic OO languages (i.e. C++, Java, Rust, etc.):

  • No constructors or destructors. Factory functions and cleanup functions should be provided in struct instead.
  • No implicit this passed to methods. All struct "methods" are really free functions prefixed with the struct type identifier (i.e. String.append compiles to String_append).
  • No implicit polymorphism. An embedded struct (Allow Plan 9/Go style struct embedding #1214) or an explicit vtable member must be specified to implement features of polymorphism.

@bjornpagen
Copy link
Author

bjornpagen commented Jul 15, 2018

I don't think methods or pub are a problem.

Methods lead to this problem. #1170 (comment)
If functions were disallowed in structs, this problem would not exist. Additionally, encapsulation may not make sense in a language such as Zig. There is nothing to hide.

@andrewrk
Copy link
Member

Zig structs should be more or less like c-structs. No functions, no pub/priv, only concrete values.

You are proposing to remove the ability for structs to be namespaces.

Rejected.

See also #1047

@isaachier
Copy link
Contributor

@andrewrk I think that was just a suggestion. The idea was to draw a line in the sand as to the definition of a struct. Not sure if this can be 100% resolved before 1.0, but a rule for reference would be nice.

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

3 participants