Replies: 8 comments 10 replies
-
In todo.txt
I've gotten into the habit of putting
in all classes. |
Beta Was this translation helpful? Give feedback.
-
I don't understand what this achieves other than requiring a second named constructor with parameters.
You mean as a discipline to remind you to implement it? I fear I'm missing something here. |
Beta Was this translation helpful? Give feedback.
-
Thanks. No magic then. :) |
Beta Was this translation helpful? Give feedback.
-
Using a "primary constructor" approach might be an improvement. It's explicit. class A(this._x: number, this._y: number)
this._z: number
def newWithZ(this._x, this._y, this._z)
enddef
endclass
A.new(1, 2)
A.newWithZ(1, 2, 3) |
Beta Was this translation helpful? Give feedback.
-
The fact that it needs to be documented with a warning seems reason enough to remove the feature. Is this "trouble" balanced by any purported convenience? I'm not convinced the generated constructor is even applicable in the most general case. It seems most suitable for classes acting as records. I think the easiest solution is to drop the feature and default to a nullary constructor. Other arguably better solutions include a primary constructor or named parameters. I note that Zimbu uses a simple nullary constructor.. Overall, I find this feature a bit gimmicky and counter to the aim of being "closer to commonly used programming languages". @yegappan if you're not interested in changing this can you please just close it? |
Beta Was this translation helpful? Give feedback.
-
If we replace the default autogenerated new() constructor which accepts all the member variables as arguments with an empty (or null) constructor, then to initialize the object variables to an initial value, the user needs to create a constructor method which will essentially be the same as the default constructor. For example, consider the following example (using the default constructor):
This creates an object and initializes "n" to 100 and "s" to "abc" using the default constructor. If we remove the default constructor, then the user needs to use the following to do the same:
As you can see the user needs to now manually add the previously auto-generated default constructor. Bram added the autogenerated default constructor to simplify this. But this suffers from the side-effect Does using a generated default constructor method to initialize the object variables bring more value than |
Beta Was this translation helpful? Give feedback.
-
I wrote a response to this, and ended up turning it into enhancement request #13460. The bottom line is:
, unless I forget, to avoid the default constructor. Instead of pushing the language in one direction or the other, I'd say that |
Beta Was this translation helpful? Give feedback.
-
I understand all the issues and trade-offs. I just came to the conclusion that it doesn't bring enough value. It seems like the sort of feature that will cost some small number of users, particularly inexperienced programmers, ten minutes of head-scratching and a smaller number of users long-lived bugs. It made me think of Wat!. As mentioned, I think some sort of primary constructor model solves all of these issues. This syntax is reasonably common, particularly in more recently developed languages. Anyway, I don't want Ernie to be disappointed; it reminds me of my father. |
Beta Was this translation helpful? Give feedback.
-
The default constructor's parameter list order matches that of the field declarations. This seems like a probable cause of surprising bugs for casual users. I don't really think the feature pulls its weight.
Generally languages with this relationship between constructor parameters and fields use the constructor's parameter list to automagically declare fields. In Vim9 script the relationship is inverted.
E.g., switching the declaration order of the fields below causes
Subtract()
to produces a different result without error. There's an obviously natural ordering of fields here but this not always the case.Field declaration order essentially becomes part of the class 'interface'.
The best solution is to support named parameters and given the languages purported interest in call-site annotations this would be generally useful.
The obvious workaround is to declare explicit constructors.
I noted this issue when there was an initial request for comments but still managed to stumble over it.
Beta Was this translation helpful? Give feedback.
All reactions