On 12-Aug-2010 10:19, winterTTr wrote: > I mean if there is such mechanism in the vim script itself. > OK, maybe the title of this mail is not very clear for you to > understand, i just give an example. > > let foo = {} > let foo.var = 100 > > we can access the value 100 by : > echo foo.var > echo foo['var'] > > i just want to know if there is a mechanism to convert > ==> echo foo.var_not_exist > to > ==> echo foo.__index__( "var_not_exist" ) "pseudo code, the > __index__ does not exist as i know > , which __index__ is a method when the key can not be found in a > dictionary object. > > I found this kind of mechanism in many script languages, such as python, lua. > With this mechanism, we can hack the dictionary to many "interesting" feature. > > Is there this accessing mechanism in vim script? > # As i know it seems not. > > Or is there an alternate method to accessing dictionary when the key > is not found > , instead of showing error directly?
You can use echo get(foo, 'var', MyFunction('var')) MyFunction() can then return whatever you want if the key 'var' is not found in foo. The only downside is that MyFunction() is always evaluated, even if the key _is_ found. -- regards, ingo -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php