Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/vimeval.txt`
6c3492b9ba392a25e2fe0b8da3cac133365c9449ff72644f0000000100000fa0
 *truthy*
An expression can be used as a condition, ignoring the type and only using
whether the value is "sort of true" or "sort of false".  Falsy is:
	the number zero
	empty string, blob, list or dictionary
Other values are truthy.  Examples:
	0	falsy
	1	truthy
	-1	truthy
	0.0	falsy
	0.1	truthy
	''	falsy
	'x'	truthy
	[]	falsy
	[0]	truthy
	{}	falsy
	#{x: 1} truthy
	0z	falsy
	0z00	truthy

							*non-zero-arg*
Function arguments often behave slightly different from |TRUE|: If the
argument is present and it evaluates to a non-zero Number, |v:true| or a
non-empty String, then the value is considered to be TRUE.
Note that " " and "0" are also non-empty strings, thus considered to be TRUE.
A List, Dictionary or Float is not a Number or String, thus evaluate to FALSE.

				*E745* *E728* *E703* *E729* *E730* *E731*
				*E974* *E975* *E976*
|List|, |Dictionary|, |Funcref|, and |Blob| types are not automatically
converted.

							*E805* *E806* *E808*
When mixing Number and Float the Number is converted to Float.  Otherwise
there is no automatic conversion of Float.  You can use str2float() for String
to Float, printf() for Float to String and float2nr() for Float to Number.

					*E362* *E891* *E892* *E893* *E894* *E907*
When expecting a Float a Number can also be used, but nothing else.

					*no-type-checking*
You will not get an error if you try to change the type of a variable.


1.2 Function references ~
					*Funcref* *E695* *E718* *E1192*
A Funcref variable is obtained with the |function()| function, the |funcref()|
function or created with the lambda expression |expr-lambda|.  It can be used
in an expression in the place of a function name, before the parenthesis
around the arguments, to invoke the function it refers to.  Example: >

	:let Fn = function("MyFunc")
	:echo Fn()
<							*E704* *E705* *E707*
A Funcref variable must start with a capital, "s:", "w:", "t:" or "b:".  You
can use "g:" but the following name must still start with a capital.  You
cannot have both a Funcref variable and a function with the same name.

A special case is defining a function and directly assigning its Funcref to a
Dictionary entry.  Example: >
	:function dict.init() dict
	:   let self.val = 0
	:endfunction

The key of the Dictionary can start with a lower case letter.  The actual
function name is not used here.  Also see |numbered-function|.

A Funcref can also be used with the |:call| command: >
	:call Fn()
	:call dict.init()

The name of the referenced function can be obtained with |string()|. >
	:let func = string(Fn)

You can use |call()| to invoke a Funcref and use a list variable for the
arguments: >
	:let r = call(Fn, mylist)
<
								*Partial*
A Funcref optionally binds a Dictionary and/or arguments.  This is also called
a Partial.  This is created by passing the Dictionary and/or arguments to
function() or funcref().  When calling the function the Dictionary and/or
arguments will be passed to the function.  Example: >

	let Cb = function('Callback', ['foo'], myDict)
	call Cb('bar')

This will invoke the function as if using: >
	call myDict.Callback('foo', 'bar')

Note that binding a function to a Dictionary also happens when the function is
a member of the Dictionary: >

	let myDict.myFunction = MyFunction
	call myDict.myFunction()

Here MyFunction() will get myDict passed as "self".  This happens when the
"myFunction" member is accessed.  When assigning "myFunction" to otherDict
and calling it, it will be bound to otherDict: >

	let otherDict.myFunction = myDict.myFunction
	call otherDict.myFunction()

Now "self" will be "otherDict".  But when the dictionary was bound explicitly
this won't happen: >

	let myDict.myFunction = function(MyFunction, myDict)
	let otherDict.myFunction = myDict.myFunction
	call otherDict.myFunction()

Here "self" will be "myDict", because it was bound explicitly.


1.3 Lists ~
						*list* *List* *Lists* *E686*
A List is an ordered sequence of items.  An item can be of any type.  Items
can be accessed by

Title: Truthy, Falsy, Function References, and Lists in Vimscript
Summary
This section covers truthy and falsy values in Vimscript, explaining how different data types are interpreted in conditional contexts. It also details Function References (Funcrefs), including how to create them, use them with dictionaries and arguments to create partial functions, and invoke them using the `:call` command. Finally, it introduces Lists as ordered sequences of items of any type.