On 4/27/06, Bram Moolenaar <[EMAIL PROTECTED]> wrote:
>
> Eric Arnold wrote:
>
> > This seems like a klunky way of getting a value from getbufvar().
> >
> > let attach_to = {}
> > if len( getbufvar( bufnr, 'attach_to' ) ) > 0
> > let attach_to = getbufvar( bufnr, 'attach_to'
> > )
> > endif
> > if len( attach_to ) > 0
> >
> > If I simply use
> >
> > let attach_to = {}
> > let attach_to = getbufvar( bufnr, 'attach_to' )
> > if len( attach_to ) > 0
> > .......
> > it will fail on variable type when the target for getbufvar(), i.e.
> > b:attach_to doesn't exist. Fortunately, len() is smart enough to
> > take different types, so there is a workaround.
> >
> > I don't know if there's a good way around this without easing up on
> > the variable type-checking, and [re-]initialize variables to the
> > correct type upon assignment. This would make life soooooo much
> > easier with Vim 7 script.
>
> I assume you have used the "attach_to" variable somewhere before. Then
> this should work fine:
>
> unlet! attach_to
> let attach_to = getbufvar( bufnr, 'attach_to' )
> if len(attach_to) > 0
This is cleaner than what I have been doing. However, it still has
the problem that
later code expects attach_to to be a 'dict'. If the
getbufvar() returns a null, then the resulting attach_to will be
a regular var, which will cause me to have to put
unlet! attach_to
in a bunch of other places.
I have an idea that might get around some of the tedious problems
dealing with type checking: when testing or assigning from/to a var,
how about a function called
force( {expr} )
that would convert the var into the type that is expected by context
in the type checking sequence?
Then I could do
let attach_to = force( getbufvar( bufnr, 'attach_to' ) )
Forces the return value to an empty 'dict' {} since attach_to
was previous defined as a 'dict' type.
> Obviously getbufvar() cannot use a default type (empty list, empty
> dictionary, zero...) when a variable does not exist.
I don't understand. The variable did exist,
let attach_to = {}
And the type checking code knew that it was a 'dict' and was
mismatched from the return value from getbufvar() . Hopefully, a
force() function would deal with this.
P.S. Any comments on the idea of auto-instantiation of 'dict' and
'list' members when they are referenced, i.e. this works,
let tst={}
let tst['b'] = [1,2,3,somevar] )
but this doesn't, though it is equivalent:
let tst={}
call add( tst['b'], [1,2,3,somevar] )