In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
,
scope is an enclosing context where
values
In ethics and social sciences, value denotes the degree of importance of some thing or action, with the aim of determining which actions are best to do or what way is best to live ( normative ethics), or to describe the significance of different a ...
and
expressions are associated. The scope resolution operator helps to identify and specify the context to which an
identifier
An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, person, physical countable object (or class thereof), or physical mass ...
refers, particularly by specifying a
namespace
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
Namespaces ...
or
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
. The specific uses vary across different
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s with the notions of scoping. In many languages, the scope resolution operator is written
::
.
In some languages, notably those influenced by
Modula-3
Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2 known as Modula-2+. It has been influential in research circles (influencing the designs of languages such as Java, C#, Python and Nim), but it ha ...
(including
Python and
Go), modules are
object
Object may refer to:
General meanings
* Object (philosophy), a thing, being, or concept
** Object (abstract), an object which does not exist at any particular time or place
** Physical object, an identifiable collection of matter
* Goal, an a ...
s, and scope resolution within modules is a special case of usual object member access, so the usual method operator
.
is used for scope resolution. Other languages, notably
C++ and
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, feature both scope resolution and method access, which interact in various ways; see examples below.
C++
class A ;
namespace B // namespace B
int A::i = 4; // scope operator refers to the integer i declared in the class A
int x = B::c; // scope operator refers to the integer c declared in the namespace B
PHP
In
PHP, the scope resolution operator is also called Paamayim Nekudotayim (, , the second word a colloquial corruption of נקודתיים, ), which means “double
colon” in Hebrew.
The name "Paamayim Nekudotayim" was introduced in the Israeli-developed
Zend Engine 0.5 used in
PHP 3. Initially the error message simply used the internal token name for the
::
,
T_PAAMAYIM_NEKUDOTAYIM
causing confusion for non-Hebrew speakers. This was clarified in PHP 5.4 as below:
$ php -r ::
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
As of PHP 8, the Hebrew name has been removed from the error message:
$ php -r ::
Parse error: syntax error, unexpected token "::", expecting end of file in Command line code on line 1
Ruby
In
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, scope resolution can be specified using the module keyword.
module Example
Version = 1.0
class << self # We are accessing the module's singleton class
def hello(who = "world")
"Hello #"
end
end
end #/Example
Example::hello # => "Hello world"
Example.hello "hacker" # => "Hello hacker"
Example::Version # => 1.0
Example.Version # NoMethodError
# This illustrates the difference between the message (.) operator and the scope operator in Ruby (::)
# We can use both ::hello and .hello, because hello is a part of Example's scope and because Example
# responds to the message hello.
#
# We can't do the same with ::Version and .Version, because Version is within the scope of Example, but
# Example can't respond to the message Version, since there is no method to respond with.
Scope is also affected by
sigils which preface variable names:
* "
$
" -
global variable
In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global ...
* "
@
" -
instance variable
In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e., a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similari ...
of
self
In philosophy, the self is an individual's own being, knowledge, and values, and the relationship between these attributes.
The first-person perspective distinguishes selfhood from personal identity. Whereas "identity" is (literally) same ...
* "
@@
" -
class variable
In class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist.
A class variable is not an instance variable. It is a special t ...
* No sigil, lowercase or underscore -
local variable
In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with ...
or method
* No sigil, uppercase -
constant
References
{{Reflist
External links
Bjarne Stroustrup's C++ Glossary
Operators (programming)
PHP software
Articles with example C++ code
Articles with example Ruby code
Articles with example PHP code