0

As the title says, I'd like to be able to use something like Python's @property decorator (its semantics ofc, not the syntax) when writing a 'class' in VimL.

Currently I have something along the following the lines:

let s:File = {}

let s:File.curr_filename = function('s:get_curr_filename')
let s:File.curr_file_dir = function('s:get_curr_file_dir')

(the two above get functions can be assumed to work correctly)

Now if I want to get the value of curr_filename and curr_file_dir of the s:File object, I'd have to invoke them as functions, which is awkward. So if possible, I'd like these members to appear as properties, but behave as methods which evaluate to the correct value dynamically whenever accessed; this is basically what Python's property decorator provides.

Can this be (easily) achieved in VimL?

1
  • 1
    I'm pretty sure this is impossible. Since I'm not 100% sure and can't "cite" anything, I'll refrain from answering. But I would suggest you don't keep your hopes up. :) (If I'm wrong, then I will be glad to see it!) Nov 12 '20 at 7:52
2

Unfortunately, I don't see how as Vim provides no way to override the behaviour of assignment or of coercion (C++ way) or the "equivalent" we have in Python behind property.

At best we can define a :PropSet command that forwards the assignment through the setter. But I don't see any simple way to handle situations where we need to evaluate expressions as this would require to parse manually a complex expression where sometimes the dot would mean access to the property or the method or concatenate.

(Dare I say it's better to use object programing to model behaviours and not data aggregation: https://www.infoworld.com/article/2073723/why-getter-and-setter-methods-are-evil.html By thinking about what my objects can do for me and not what they can remember I hardly need setters/getters/properties even in VimL.)

1
  • Alright, thanks for the insight.
    – Jethro Cao
    Nov 12 '20 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.