-
-
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
remove the (namespace) type and make every file an empty struct #1047
Comments
This is interesting foo.zig var x: i32 = 1; When you var foo = @import("foo.zig"); in multiple places does each foo get is own copy of x? Also what about if you import in a threads function, is that now a thread local instance of foo? |
@bheads There would only exist one
The file will be a struct definition just like if you did |
@bheads Aah, that's what you were after. No, as structs can have global variables in their "namespace":
|
Though I guess you would more refer to those variables as 'static' usage wise the same. |
If the type of namespace changes to
|
@Hejsil I would argue no, unless the type had 1 or more fields. |
At some point I was going to propose
Where B is equivalent to
Yeah, why not! |
@raulgrell that sounds a lot like what D did with alias this, useful for making wrapped types but leads to complexity (ie can you support multiple use?) |
This reminds me of ML modules 👍 but also of Java mandatory classes everywhere 👎 |
@andrewrk, regarding the lambda issue, is there a way that a struct type could be derived from a local scope's context (by some comptime function?), in order to capture some names into the new struct type? I'm imagining something a little bit like inheritance except applied to a particular scope, maybe including the definition of new methods.... Another question is defining an anonymous method on a struct to allow it to be evaluated as a "function" / functor. |
@bheads: Yeah, it can get hairy. I guess multiple @binary132: If the top level of java files were implicitly considered a class, it wouldn't be too bad. Processing is basically a preprocessor on top of java that just concatenates all your your source files into a single class and adds a bit of boilerplate. Pretty seamless if you're being generous =) In any case, I'm not really pleased with my example... |
Being able to pass "namespaces" as comptime parameters to functions would be great when mocking for unit tests. |
I actually thought files were structs before I read this. |
#1685 implements the syntax changes, but things like tests in structs don't work yet. |
hi everybody. thank you for zig, this is my first comment here and I'm still learning zig, sorry if this sounds silly.
it maybe better to force .zig extension to directory name or ignore ns1.zig file to avoid future name collision. |
@roofha, interesting thought but I think being explicit is better. There are times you have arbitrary files in a directory (a snippet or experiment) and you wouldn't want those automatically "imported". |
@winksaville I didn't mean to import them all. I haven't thought about the implementation but I'm guessing the compiler can look for a definition of a symbol in another file with the same name when it doesn't find it in the current file, if this is bad for performance reasons, we can prepend echo external symbol like this: |
There's not much of a difference between
@typeOf(@import("std"))
andstruct {}
. Here are the current differences for example:use
inside them (but why not?)Making a file be an empty struct instead of a namespace would accomplish the following things:
zig/std/index.zig
Lines 3 to 10 in 96164ce
Thanks @tgschultz for coming up with this idea.
The text was updated successfully, but these errors were encountered: