Reply to message «<SID> or s: - General questions about functions», 
sent 10:41:25 12 April 2011, Tuesday
by rameo:

> After a half year of use of VIM, I still don't understand what is, and
> if there is a difference between <SID> and "s:". (even after reading
> the help file)
It is simple: if a function name (only *function* name!) starts with `s:' and 
contains only letters, digits and underscores then `s:' will be translated to 
`<SID>' (which is really `<SNR>{N}_'). There is a special case: anonymous 
dictionary functions:
    function s:Foo()
will define function <SNR>{N}_Foo, but
    function s:.Foo()
(note the dot) will define function {M} and store reference to created function 
in the value of the key Foo of dictionary s: (see :h internal-variables).

Both {N} and {M} are decimal string representations of arbitrary unsigned 
integer numbers. First is the number of the script where function was defined, 
second is the number of the function (incremented for each subsequent function 
definitions).

`s:' -> `<SNR>{N}_' translation is valid for function names only. Script 
variables cannot be accessed this way, in variables `s:' only designates the 
scope of variable.

When you will see any function definition, don't forget that there are *no* 
local functions, all function definitions are global. `s:' translation is just 
a 
hack: you may define function `<SNR>1000_Foo' even if you don't have one 
thousand script files. You may even define function `<SNR>Foo', or `b:Foo', or 
`w:Foo', or even `_:foo': neither of these are really local functions. If you 
write a script and want to make your functions unaccessible without a debugger, 
use anonymous functions:
- you cannot call them without having a reference,
- you can obtain reference to them only by copying an existing reference,
- you cannot access script-local variable without a debugger (unless you posted 
reference to s: dictionary somewhere).

> Fun!, fun!, Function! and function!
> is the same isn't?
> Endfunction, endfunction, Endfun, endfun also?
Case matters, `command' and `Command' are never the same. Truncations are 
different: `fu', `fun' and `function' are the same as well as `endf', `endfun' 
and `endfunction'. For user-defined commands truncations must be unambigious 
(you can't have commands `FooBar' and `FoBar' and truncate one of them to 
`Fo'), 
but this is not true for built-ins (`f' is a short form of `file', for example).

> Is this the same?:
> fun! s:Myfunction() --> :call s:Myfunction() --> call
> <SID>Myfunction()
> fun! s:myfunction() --> :call s:myfunction() --> call
> <SID>myfunction()
> fun! s:MYFUNCTION() --> :call s:MYFUNCTION() --> call
> <SID>MYFUNCTION()
Case matters, but `s:' and `<SID>' are the same.

Original message:
> After a half year of use of VIM, I still don't understand what is, and
> if there is a difference between <SID> and "s:". (even after reading
> the help file)
> 
> Can they be exchanged?
> 
> Fun!, fun!, Function! and function!
> is the same isn't?
> Endfunction, endfunction, Endfun, endfun also?
> 
> I often Titlecase functionnames.
> Is this the same?:
> fun! s:Myfunction() --> :call s:Myfunction() --> call
> <SID>Myfunction()
> fun! s:myfunction() --> :call s:myfunction() --> call
> <SID>myfunction()
> fun! s:MYFUNCTION() --> :call s:MYFUNCTION() --> call
> <SID>MYFUNCTION()

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to