patch 8.2.0695: Vim9: cannot define a function inside a function
Problem: Vim9: cannot define a function inside a function.
Solution: Initial support for :def inside :def.
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index c06cc23..7127331 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -2,19 +2,7 @@
source check.vim
source view_util.vim
-
-" Check that "lines" inside ":def" results in an "error" message.
-func CheckDefFailure(lines, error)
- call writefile(['def Func()'] + a:lines + ['enddef'], 'Xdef')
- call assert_fails('so Xdef', a:error, a:lines)
- call delete('Xdef')
-endfunc
-
-func CheckScriptFailure(lines, error)
- call writefile(a:lines, 'Xdef')
- call assert_fails('so Xdef', a:error, a:lines)
- call delete('Xdef')
-endfunc
+source vim9.vim
func Test_def_basic()
def SomeFunc(): string
@@ -95,8 +83,17 @@
assert_equal('one', MyDefaultArgs('one'))
assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
- call CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef'], 'E1001:')
- call CheckScriptFailure(['def Func(arg: number = "text")', 'enddef'], 'E1013: argument 1: type mismatch, expected number but got string')
+ CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef'], 'E1001:')
+ CheckScriptFailure(['def Func(arg: number = "text")', 'enddef'], 'E1013: argument 1: type mismatch, expected number but got string')
+enddef
+
+def Test_nested_function()
+ def Nested(arg: string): string
+ return 'nested ' .. arg
+ enddef
+ assert_equal('nested function', Nested('function'))
+
+ CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
enddef
func Test_call_default_args_from_func()
@@ -721,5 +718,13 @@
unlet g:UseVararg
enddef
+def Test_nested_closure()
+ let local = 'text'
+ def Closure(arg: string): string
+ return local .. arg
+ enddef
+ assert_equal('text!!!', Closure('!!!'))
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker