Afaik, opfunc requires literal function name not the variable name holding function reference.
If what you want is to simplify opfunc creation there is another way:
func! ToggleComment(type = '') if a:type == '' " HERE you set opfunc to ToggleComment let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') return 'g@' endif " Do your ToggleCommenting stuff here " execute "lua require('commented').toggle_comment()" endfunc nnoremap <silent> <expr> <space>c ToggleComment()
Check let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') line. Here expand('<sfile>') would be function ToggleComment and with matchstr you extract function name.
Here for example my operator for vimscript evaluation:
" Essential for my vimscripting " run selected vimscript xnoremap <silent> <space>v y:@"<cr> " run vimscript line nmap <space>vv V<space>v " run operator func! s:vimrun(type = '') if a:type == '' let &opfunc = matchstr(expand('<sfile>'), '[^. ]*$') return 'g@' endif let commands = #{line: "'[V']y", char: "`[v`]y", block: "`[\<c-v>`]y"} silent exe "noautocmd keepjumps normal! " .. get(commands, a:type, '') @" endfunc nnoremap <silent> <expr> <space>v <SID>vimrun()
With <space>vip I can evaluate paragraph as vimscript...