-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Draft: Support for using len(), empty() and string() with an object #13238
Conversation
Codecov Report
@@ Coverage Diff @@
## master #13238 +/- ##
==========================================
+ Coverage 82.16% 82.83% +0.66%
==========================================
Files 160 150 -10
Lines 196356 183049 -13307
Branches 43997 41052 -2945
==========================================
- Hits 161338 151621 -9717
+ Misses 22131 18470 -3661
- Partials 12887 12958 +71
Flags with carried forward coverage won't be shown. Click here to find out more.
|
um....... -_-#
it's going to be a mixture of python/dart/Java/c++ ? :lol:
…--
shane.xb.qian
|
I personally kind of dislike the double-underscore in Python as well as I find them a little unnecessarily ugly. Other languages other than Python all found ways to do this without resorting to a hammer like this, but I can see the practical aspect of this. That said I don't write a lot of vim9script. |
If you're introducing dunder methods Why not just use the methods |
The difference is that the new() method is directly invoked by the user and the number On the other hand, these methods cannot be directly invoked by the user and are |
@yegappan since it's "draft", might be worth actually making it a draft PR so it isn't inadvertently committed before you're ready. |
Have you explored doing this like it would be done in a more strongly typed Note, this is an approximation, would really be like You could have something like
|
The spec for There's a test label'd
Which succeeds. Why change the spec? |
Makes sense. Updated the PR to return 0 if the __len() method is missing. |
This can be used for other operations in non-container classes. For example, currently |
I wasn't clear. It doesn't look like an error is given. There should be an exception. |
Of course, something like
The point is that it is useful to determine the capabilities of an object. Using an interface to tag an object is a common way to achieve that. |
If a class doesn't implement the "__len()" method, then the "E701: Invalid type for len()" |
Gotcha. I missed that when looking through the code. Sorry for the noise. |
Or maybe I only saw
and was looking at the help for I guess empty should default to TRUE, if __empty() is not specified. Not zero. |
I think the benefit of something like an interface, is that for things like |
I like that; currently define ToString() methods as needed, using a builtin is easier. There's current default behavior; would that still work, but could be overriden?
Yes, the programmer could define what lock/unlock of an object means. There's also lock/unlock of a member to consider. Currently there's no
I'm not sure what that's suggesting. Does it mean that if
which from outside the class effectively makes somelist look like a const.
But the class could still do If you want to support locking/unlocking class/object variables, and not just what they reference, I think that would just work (with some minor adjustment) and not require any dunder support, but I haven't done any testing on it. But if you wanted to allow the class/object to control it, there could be And there's other ways to look at it. Got involved in the whole lockvar issue after seeing
in the todo. What about it? I still don't know what that means. |
0ec5e66
to
0d05a8f
Compare
As mentioned by @ychin
I'll take it for a test drive if it finds its way in. I hope, at some point, that bundling |
646b062
to
2a5f181
Compare
I have simplified the support for using the builtin methods like empty(), len() and string() with an object. |
(I'll download and play with it, but some initial comments)
There's a problem using regular names, there's no way to know that it And there is also some [backwards?] compatibility issues. Consider, if I With this PR it's very easy to run into trouble:
I think for many, the Note that using |
A regular object method name cannot start with a lower case letter. So these builtin
You cannot define an object method with a lower case letter currently. So the object
If an object method name is all lower case and matches a Vim builtin function, then
A regular object method starting with a lower case letter cannot be defined.
These two invocations mean the same thing.
Won't it be simpler to support these optional builtin methods than adding one
As explained above, this change will not break backward compatibility. |
Of course, I was hallucinating, apologies for the noise.
I don't see how it is simpler for the vim9script programmer. Requiring contract specification avoids bugs and is easier to understand,
One factor is long term goals for interoperability. Without knowing what's being implemented, what kind of confusion might arise?
Right. I'm not sure that's a good thing, maybe it is. |
We could add builtin interfaces with names starting with a lower case letter (as a |
Here's some comments/possibilities (not an attempt at a spec). After writing Preceding a builtin About
But defining them is optional. Or you could have. (But
IOW, for many cases of an For max flexibility, could allow stuff like
Hmm, I wonder if For a list container, there's an implicit system definition like:
Run-time must check that the return value is the same as Or, could have, maybe better I think
For this This points out something interesting. The methods may have certain The above definition uses "T", like a generic. But this is a hidden/implicit Hmm, but I guess I can see how the following would be nice
then it's clear what the "constistent item type" is. |
In my previous comment I screwed up with the methods by including the implicit "self" in the function signature, like for
But the question, about how the compiler checks for consistent |
9a4d5eb
to
0c52aee
Compare
Add support for Vim9 object methods len(), empty() and string().