Replies: 7 comments 1 reply
-
This code is a working solution with vim 9.1.255 and later. Note that this solution keeps a reference to the object key. This
The file in the next comment is imported. If However, notice that with a
would be handy in various situations. This is another case.
|
Beta Was this translation helpful? Give feedback.
-
This code automatically creates a unique_object_id by examining the stack to determine the unique class name that can be assigned to an object. Lets hope it goes away soon. Here's an example of a class that can be added to the dictionary defined above.
And here's
|
Beta Was this translation helpful? Give feedback.
-
@zzzyxwvut , @yegappan you might find this interesting |
Beta Was this translation helpful? Give feedback.
-
For any readers, there's an interesting example at #14330 (comment) |
Beta Was this translation helpful? Give feedback.
-
This is an example of creating an object dictionary that does strict type checking. If there were generics, this would look like
The example:
|
Beta Was this translation helpful? Give feedback.
-
@yegappan I've re-written this. I'd appreciate any comments; in particular about |
Beta Was this translation helpful? Give feedback.
-
There's an obvious way to avoid the risk of a name conflict for a variable that provides a unique object id/key. How could I forget, dunder to the rescue. Something like |
Beta Was this translation helpful? Give feedback.
-
Note that both
class
andenum
have the problem.For
enum
s,eval('Planet.earth')
works in some cases to convert a string to anenum
value.name
was added with an enum instance variable. Then, in conjunction with eval, the idea was to use something likeeval('Planet.' .. obj.name)
to get the object from a string. There are problems if theenum
is defined in another file and/or there are multipleenum
of the same name in different files; so this is not a general solution.Problems
There are various things that
vim
can do, such as including builtin capability to assist:dict
dict
to accept an object as a key (E1324: Using an Object as a String)In the meantime, the following comments show a way that a developer can create such a dictionary. The trick is to construct a unique ID, in these examples unique_object_id is an instance variable.
A
vim
provided unique_object_id would get rid of the weirdGenerateKey
file/code below, #14374 (comment).In the following
vim9script
an id looks likeIf this were provided by
vim
, the17
would be something the system can use to get at the actual object (assuming such a thing actually exists). For an enum value could have something likeNote:
earth
is extracted byid->matchstr('\(@\)\@<=.*')
orid[ id->stridx('@') + 1 : ]
; I likeid->split('@')[1]
. So with a unique id, thename
instance variable is not needed. The same variable name could be used with both an enum and class instance.An alternative to unique_object_id member variable would be a builtin function; this would avoid any backward comaptibility issues but with a performance cost.
A
vim
builtin could take this unique id and return the object. A null/empty-thing should be returned if the object is not found; garbage collected or non-existent, an exception if malformed.This is developed from #14330 (comment) and its following comments.
Related: @zzzyxwvut did some work on object serialization. See #14330 (comment) and following comments in that thread.
Beta Was this translation helpful? Give feedback.
All reactions