-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
RFC: static methods in classes #2376
Comments
(n.b. -- The problem with the syntax I suggested in the second bullet point is that this means you can't have both a type (specifically a class type) and a module named |
@pcwalton suggested an alternative to the entire plan, which would be to implement C++-style friend classes (or friend modules, in our case), which would allow you to write code that could access a different class's private fields, obviating the need for static functions. |
I haven't followed this discussion, but if it's primarily a way to make ctors then I'm tentatively not in favor. It sounds very similar to our old way of creating obj constructors (just create a function and call it the constructor) which I found not terribly convenient to use. |
@brson It's not primarily a way to make ctors -- that was just an example (my fault for giving that too much weight). It arose because we were talking about #2290 and if we had static fns, we wouldn't need the special |
I prefer static methods to friends for the reason @brson gave, as well as the fact that having to make every class with private fields have a friend would be burdensome. |
@catamorphism I'm going to show my ignorance regarding classes. Does So regardless of the details I would like class constructors to avoid the following pattern:
It sounds like this is what we're looking at, where you do all the imperative, stateful work in one phase, then build a giant record containing the results of that work. It's a common pattern in Rust, but it's not a common pattern in class constructors in most languages, where you are free to build up the fields over the course of the constructor. It's a lot of boilerplate. |
@brson an |
@brson Also, if we add static fns, we can still keep ctors as a separate thing; the real point of static fns is to allow writing fns that take their |
So I was thinking again and there are a couple of arguments in favor of static functions that mean (IMHO) that we shouldn't shoot them down so quickly:
We could sidestep the name resolution problems by having the record literal syntax for class construction be module-private by default, and not having static functions per se at all. This is a little hokey, but the more I think about it the more I think it isn't terrible; you can make other things be module-private, so why not constructors? |
@pcwalton I don't think it's that hokey. It wouldn't really be that the record literal syntax is module-private -- it would be that the literal constructor ( |
I think module-private constructors would be useful. |
Closing for now because max/min classes address this. |
When discussing #2290 with @nikomatsakis and @pcwalton , we agreed that static methods might potentially be useful in classes. One of the motivating cases is to simplify how constructors get treated. We could even eliminate class constructors altogether, and write something like:
In this case, there's no constructor, but rather, a static method called
cat
that returns a new instance of the classcat
. I'm also assuming that a version of record initialization syntax could be used for classes, which doesn't work right now, but could be made to work. Since classes are nominal, we would need to prefix the record literal with the class name; this would decouple whatever other computation a constructor needs to do from the act of actually allocating a new record and filling in the class fields.So I'm proposing to:
static
modifier on function items, only valid within aclass
declaration, whose semantics are that a function declared asstatic fn f...
requires noself
argument and cannot refer toself
in its body -- however,f
can refer to class-private fields in its enclosing class.C
with theC::
prefix: as in,cat::cat()
above (This is up for discussion -- I don't care how the exact syntax looks)Notice that this removes the need for
@class
(as in the original spec, #1726) because you can now write a static method that takes an explicit self parameter.The text was updated successfully, but these errors were encountered: