You can call a function transparently within a mapping, thus circumventing
mode changes (e.g. by pressing :
) or losing counts, by using <expr>
mappings, similar to what you suggested. Just use them to call a function like so:
fun! SetOpFunc()
set opfunc=CountSpaces
return 'g@'
endfun
nno <expr> <F4> SetOpFunc()