Sigil (computer programming)
In
$foo
, where $
is the sigil.
Sigil, from the Latin sigillum, meaning a "little sign", means a sign or image supposedly having magical power.[1] Sigils can be used to separate and demarcate namespaces that possess different properties or behaviors.
Historical context
The use of sigils was popularized by the
&
"). Raku also uses secondary sigils, or twigils,[2] to indicate the scope of variables. Prominent examples of twigils in Raku include "^
" (caret.
", used with object attribute accessors (i.e., instance variables).
Sigil use in some languages
In CLIPS, scalar variables are prefixed with a "?
" sigil, while multifield (e.g., a 1-level list) variables are prefixed with "$?
".
In
+
.
In CycL, variables are prefixed with a "?
" sigil.[3] Similarly, constant names are prefixed with "#$
" (pronounced "hash-dollar").[4]
In Elixir, sigils are provided via the "~
" symbol, followed by a letter to denote the type of sigil, and then delimiters. For example, ~r(foo)
is a regular expression of "foo". Other sigils include ~s
for strings and ~D
for dates. Programmers can also create their own sigils.[5]
In the esoteric INTERCAL, variables are a 16-bit integer identifier prefixed with either ".
" (called "spot") for 16-bit values, ":
" (called "twospot") for 32-bit values, ",
" ("tail") for arrays of 16-bit values and ";
" ("hybrid") for arrays of 32-bit values.[6] The later CLC-Intercal added "@
" ("whirlpool") for a variable that can contain no value (used for classes) and "_
" used to store a modified compiler.[7]
In MAPPER (aka BIS), named variables are prefixed with "<" and suffixed with ">" because strings or character values do not require quotes.
In
$
" sigil, while all variables have a "%
" prefixed (regardless of local or global variables or data type). Binary variables are prefixed by an "&
".
In the
$
" precedes intrinsic function names and "special variable names" (built-in variables for accessing the execution state). "$Z
" precedes non-standard intrinsic function names. "$$
" precedes extrinsic function names. Routines (used for procedures, subroutines, functions) and global variables (database storage) are prefixed by a caret (^). The last global variable subtree may be referenced indirectly by a caret and the last subscript; this is referred to as a "naked reference". System-wide routines and global variables (stored in certain shared database(s)) are prefixed with ^%; these are referred to as "percent routines" and "percent globals".
In Objective-C, string literals preceded with "@
" are instances of the object type NSString
or, since clang v3.1 / LLVM v4.0, NSNumber
, NSArray
or NSDictionary
. The prefix @
is also used on the keywords interface
, implementation
, and end
to express the structure of class definitions. Within class declarations and definitions as well, a prefix of -
is used to indicate member methods and variables, while prefix +
indicates class elements.
In the
PILOT uses "$
" for buffers (string variables), "#
" for integer variables, and "*
" for program labels.
Python uses a "__
" prefix, called dunder, for "private" attributes.
In
$
" is prefixed to global variables, "@
" is prefixed to instance variables, and "@@
" is prefixed to class variables. Ruby also allows (strictly conventional) suffix sigils: "?
" indicates a predicate method returning a boolean or a truthy or falsy value, and "!
" indicates that the method may have a potentially unexpected effect and needs to be handled with care.[8]In Scheme, by convention, the names of procedures that always return a boolean value usually end in "?
". Likewise, the names of procedures that store values into parts of previously allocated Scheme objects (such as pairs, vectors, or strings) usually end in "!
".
Standard ML uses the prefix sigil "'
" on a variable that refers to a type. If the sigil is doubled, it refers to a type for which equality is defined. The "'
" character may also appear within or at the end of a variable, in which case it has no special meaning.
In Transact-SQL, "@
" precedes a local variable or parameter name. System functions (previously known as global variables) are distinguished by a "@@
" prefix. The scope of temporary tables is indicated by the prefix "#
" designating local and "##
" designating global.
In
$
" sigil.
In XSLT, variables and parameters have a leading "$
" sigil on use, although when defined in <xsl:param>
or <xsl:variable>
with the "name
" attribute, the sigil is not included. Related to XSLT, XQuery uses the "$
" sigil form both in definition and in use.
In MEL, variable names are prefixed by "$
" to distinguish them from functions, commands, and other identifiers.
Similar phenomena
Shell scripting variables
In Unix
Identifier conventions
In
Various programming languages including
beginning with a capital letter differently from identifiers beginning with a small letter, a practice related to the use of sigils.Stropping
Actually a form of
[end]
.[10]The "@
" prefix can also be applied to string literals; see literal affixes below.
Hungarian notation
Related to sigils is Hungarian notation, a naming convention for variables that specifies variable type by attaching certain alphabetic prefixes to the variable name. Unlike sigils, however, Hungarian notation provides no information to the compiler; as such, explicit types must be redundantly specified for the variables (unless using a language with type inference). As most standard compilers do not enforce use of the prefixes, this permits omission and also makes code prone to confusion due to accidental erroneous use.[11]
Literal affixes
While sigils are applied to names (identifiers), similar prefixes and suffixes can be applied to
r"C:\Windows"
in Python, which represents the string with value C:\Windows
; as an escaped string this would be written as "C:\\Windows"
.
As this affects the semantics (value) of a literal, rather than the syntax or semantics of an identifier (name), this is neither stropping (identifier syntax) nor a sigil (identifier semantics), but it is syntactically similar.
Java annotations
Compare Java annotations such as @Override and @Deprecated.
Confusion
In some cases the same syntax can be used for distinct purposes, which can cause confusion. For example, in
See also
References
- ^ "Definition of sigil". Collins English Dictionary. Retrieved 22 May 2012.
- ^ "Perl 6 variables:Twigils", Perl 6 Documentation.
- ^ "Variables – Cycorp". www.Cyc.com. Retrieved 31 December 2017.
- ^ "Constants – Cycorp". www.Cyc.com. Retrieved 31 December 2017.
- ^ "Getting Started: Sigils". Elixir. Retrieved 18 January 2022.
- ^ "C-INTERCAL 0.29 Revamped Instruction Manual". catb.org. Retrieved 2022-06-30.
- ^ "CLC-INTERCAL Reference". oshaboy.github.io. Retrieved 2022-06-30.
- ^ Black, David A. (15 August 2007). "Bang methods; or, Danger, Will Rubyist!". Archived from the original on 22 May 2016.
- ^ "C# Keywords". MSDN. Retrieved 2011-03-23.
- ^ "string (C# Reference)". MSDN. Retrieved 2011-03-23.
- ^ Linux kernel coding style, by Linus Torvalds