Home Explore Blog CI



neovim

35th chunk of `runtime/doc/vimfn.txt`
2ef14029b6199e9bc4ff8d6a2eb6cd6588aad260068cb6c60000000100000faf
 Broken = function(dict.Func, [arg], dict)
<
		When using the Funcref the function will be found by {name},
		also when it was redefined later. Use |funcref()| to keep the
		same function.

		When {arglist} or {dict} is present this creates a partial.
		That means the argument list and/or the dictionary is stored in
		the Funcref and will be used when the Funcref is called.

		The arguments are passed to the function in front of other
		arguments, but after any argument from |method|.  Example: >vim
			func Callback(arg1, arg2, name)
			"...
			endfunc
			let Partial = function('Callback', ['one', 'two'])
			"...
			call Partial('name')
<		Invokes the function as with: >vim
			call Callback('one', 'two', 'name')

<		With a |method|: >vim
			func Callback(one, two, three)
			"...
			endfunc
			let Partial = function('Callback', ['two'])
			"...
			eval 'one'->Partial('three')
<		Invokes the function as with: >vim
			call Callback('one', 'two', 'three')

<		The function() call can be nested to add more arguments to the
		Funcref.  The extra arguments are appended to the list of
		arguments.  Example: >vim
			func Callback(arg1, arg2, name)
			"...
			endfunc
			let Func = function('Callback', ['one'])
			let Func2 = function(Func, ['two'])
			"...
			call Func2('name')
<		Invokes the function as with: >vim
			call Callback('one', 'two', 'name')

<		The Dictionary is only useful when calling a "dict" function.
		In that case the {dict} is passed in as "self". Example: >vim
			function Callback() dict
			   echo "called for " .. self.name
			endfunction
			"...
			let context = {"name": "example"}
			let Func = function('Callback', context)
			"...
			call Func()	" will echo: called for example
<		The use of function() is not needed when there are no extra
		arguments, these two are equivalent, if Callback() is defined
		as context.Callback(): >vim
			let Func = function('Callback', context)
			let Func = context.Callback

<		The argument list and the Dictionary can be combined: >vim
			function Callback(arg1, count) dict
			"...
			endfunction
			let context = {"name": "example"}
			let Func = function('Callback', ['one'], context)
			"...
			call Func(500)
<		Invokes the function as with: >vim
			call context.Callback('one', 500)
<
		Returns 0 on error.

                Parameters: ~
                  • {name} (`string`)
                  • {arglist} (`any?`)
                  • {dict} (`any?`)

                Return: ~
                  (`any`)

garbagecollect([{atexit}])                                    *garbagecollect()*
		Cleanup unused |Lists| and |Dictionaries| that have circular
		references.

		There is hardly ever a need to invoke this function, as it is
		automatically done when Vim runs out of memory or is waiting
		for the user to press a key after 'updatetime'.  Items without
		circular references are always freed when they become unused.
		This is useful if you have deleted a very big |List| and/or
		|Dictionary| with circular references in a script that runs
		for a long time.

		When the optional {atexit} argument is one, garbage
		collection will also be done when exiting Vim, if it wasn't
		done before.  This is useful when checking for memory leaks.

		The garbage collection is not done immediately but only when
		it's safe to perform.  This is when waiting for the user to
		type a character.

                Parameters: ~
                  • {atexit} (`boolean?`)

                Return: ~
                  (`any`)

get({list}, {idx} [, {default}])                              *get()* *get()-list*
		Get item {idx} from |List| {list}.  When this item is not
		available return {default}.  Return zero when {default} is
		omitted.

                Parameters: ~
                  • {list} (`any[]`)
                  • {idx} (`integer`)
                  • {default} (`any?`)

                Return: ~
                  (`any`)

get({blob}, {idx} [, {default}])                                    *get()-blob*
		Get

Title: Function Call Mechanics and Garbage Collection in Vimscript
Summary
This section details how function calls work in Vimscript with `function()`, including passing arguments, using dictionaries, and nesting function calls to add arguments to Funcrefs. It also covers the `garbagecollect()` function, which cleans up unused Lists and Dictionaries with circular references, and the `get()` function used to retrieve an item at a specific index from a List or Blob.