-
-
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
support clearer "method" syntax on structs? #746
Comments
There is an advantage to functions and "methods" having the same syntax, which is, that they can be copypasted between global scope and struct scope, and nothing needs to changes (refactorability). Many of the comments seem to be related to just needing better errors for these cases. I totally agree that the compiler shouldn't think I'm trying call "methods" from the type, and static functions from the instance. As for |
This is generally how I assumed things worked and how I declare my classes. I would generally be in favour of the most obvious approach being the one used. One small detail that needs to be considered is how testing behaves in conjunction with internal struct functions. These private details would be untestable with more limited scoping rules which may be an issue as tests currently are not allowed in struct definitions. An alternative would be to allow |
@Hejsil I agree with that thought on refactorability and also that mostly I'm concerned about the error messaging. I find it strange that I can call "static" functions with the dot syntax as well as access members of the struct (and thereby call functions that are a member without passing self) I have some more thoughts on this but here's a snippet I was thinking about if we had anon functions and default struct initialization params: const warn = @import("std").debug.warn;
fn func(x:u8) u8 {
return x;
}
const Thing = struct {
// could be default to func which could live in the container Thing scope
// here or potentially be anonymous
f: (fn(u8) u8) // = func,
};
test "run" {
const s = Thing { .f = func };
warn("\n{}\n", s.f(8)); // note that this doesn't pass self
warn("\n{}\n", Thing.f(9)); // error: container 'Thing' has no member called 'f'
} |
Maybe we need another syntax for calling methods. |
One idea is that we already have the concept of a "test build". And in this build mode, everything is public. |
I'm fairly satisfied with status quo, though I agree error messages could be improved. Is this a clearer explanation of what's going on? Disregard visibility for now.
|
I think I mistakenly labeled this a proposal. What's going on here is that early on in the language, I added And then along the way I've become more in favor of public things, especially struct fields. I certainly think that test builds at least should have full access to all private things. What's needed is a comprehensive proposal that really considers everything and prescribes a full description of how public/private stuff should work in Zig, and then we can talk about that proposal. |
My basic feeling here is that calling a function using dot syntax on an instance of a struct is a method call. We're already implicitly passing self but forcing explicit arity on self at the definition site. Would it be unreasonable to introduce a
meth
keyword to delineate implicit self passing from function namespacing? It would only be available inside a struct. I realize this makes structs act like classes in a way, but it is clear that this is an emergent pattern and making that use explicit would be clearer in many cases. Furthermore, disallowing method calls on struct names and function calls on instances at the call site would codify what is already an implicit ruleset.This is syntax related to OOP but not to vtables or polymorphism, which are different issues. Clearer object-ish calling conventions might make that easier to deal with as well, conceptually.
I'm not necessarily advocating for more object orientation, I'm just talking about some things that feel counterintuitive to me in the status quo.
The text was updated successfully, but these errors were encountered: