Mercurial > vim
comparison runtime/doc/eval.txt @ 30065:6cf788ab844c v9.0.0370
patch 9.0.0370: cleaning up afterwards can make a function messy
Commit: https://github.com/vim/vim/commit/1d84f7608f1e41dad03b8cc7925895437775f7c0
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Sep 3 21:35:53 2022 +0100
patch 9.0.0370: cleaning up afterwards can make a function messy
Problem: Cleaning up afterwards can make a function messy.
Solution: Add the :defer command.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 03 Sep 2022 22:45:03 +0200 |
parents | e37754a13778 |
children | ebed259f919f |
comparison
equal
deleted
inserted
replaced
30064:a8f1fbaa43c8 | 30065:6cf788ab844c |
---|---|
2975 | 2975 |
2976 A function can also be called as part of evaluating an expression or when it | 2976 A function can also be called as part of evaluating an expression or when it |
2977 is used as a method: > | 2977 is used as a method: > |
2978 let x = GetList() | 2978 let x = GetList() |
2979 let y = GetList()->Filter() | 2979 let y = GetList()->Filter() |
2980 | |
2981 | |
2982 CLEANING UP IN A FUNCTION ~ | |
2983 *:defer* | |
2984 :defer {func}({args}) Call {func} when the current function is done. | |
2985 {args} are evaluated here. | |
2986 | |
2987 Quite often a command in a function has a global effect, which must be undone | |
2988 when the function finishes. Handling this in all kinds of situations can be a | |
2989 hassle. Especially when an unexpected error is encountered. This can be done | |
2990 with `try` / `finally` blocks, but this gets complicated when there is more | |
2991 than one. | |
2992 | |
2993 A much simpler solution is using `defer`. It schedules a function call when | |
2994 the function is returning, no matter if there is an error. Example: > | |
2995 func Filter(text) | |
2996 call writefile(a:text, 'Tempfile') | |
2997 call system('filter < Tempfile > Outfile') | |
2998 call Handle('Outfile') | |
2999 call delete('Tempfile') | |
3000 call delete('Outfile') | |
3001 endfunc | |
3002 | |
3003 Here 'Tempfile' and 'Outfile' will not be deleted if something causes the | |
3004 function to abort. `:defer` can be used to avoid that: > | |
3005 func Filter(text) | |
3006 call writefile(a:text, 'Tempfile') | |
3007 defer delete('Tempfile') | |
3008 defer delete('Outfile') | |
3009 call system('filter < Tempfile > Outfile') | |
3010 call Handle('Outfile') | |
3011 endfunc | |
3012 | |
3013 Note that deleting "Outfile" is scheduled before calling system(), since it | |
3014 can be created even when `system()` fails. | |
3015 | |
3016 The defered functions are called in reverse order, the last one added is | |
3017 executed first. A useless example: > | |
3018 func Useless() | |
3019 for s in range(3) | |
3020 defer execute('echomsg "number ' .. s .. '"') | |
3021 endfor | |
3022 endfunc | |
3023 | |
3024 Now `:messages` shows: | |
3025 number 2 | |
3026 number 1 | |
3027 number 0 | |
3028 | |
3029 Any return value of the deferred function is discarded. The function cannot | |
3030 be followed by anything, such as "->func" or ".member". Currently `:defer | |
3031 GetArg()->TheFunc()` does not work, it may work in a later version. | |
3032 | |
3033 Errors are reported but do not cause aborting execution of deferred functions. | |
3034 | |
3035 No range is accepted. | |
2980 | 3036 |
2981 | 3037 |
2982 AUTOMATICALLY LOADING FUNCTIONS ~ | 3038 AUTOMATICALLY LOADING FUNCTIONS ~ |
2983 *autoload-functions* | 3039 *autoload-functions* |
2984 When using many or large functions, it's possible to automatically define them | 3040 When using many or large functions, it's possible to automatically define them |