Difference between revisions of "Language Reference/Namespaces"

From wiki.visual-prolog.com
m (→‎Namespace qualification: wiki markup bug)
(namespace \)
 
(5 intermediate revisions by 2 users not shown)
Line 9: Line 9:


<vipbnf><NamespaceIdentifier>: one of
<vipbnf><NamespaceIdentifier>: one of
  \
   <LowercaseIdentifier>
   <LowercaseIdentifier>
   <LowercaseIdentifier> \ <NamespaceIdentifier></vipbnf>
   <LowercaseIdentifier> \ <NamespaceIdentifier></vipbnf>
Line 18: Line 19:
Namespace entrances divide source files into '''''namespace regions'''''.
Namespace entrances divide source files into '''''namespace regions'''''.


A namespace entrance marks the beginning of a namespace region, which ends ends at the next namespace entrance or the end of the file.
A namespace entrance marks the beginning of a namespace region, which ends at the next namespace entrance or the end of the file.


Every file starts in the '''''root namespace'''''.
Every file starts in the '''''root namespace'''''.
Line 28: Line 29:
Any interface, class and implementation that is meet inside a namespace region belongs to that namespace.
Any interface, class and implementation that is meet inside a namespace region belongs to that namespace.


'''Example'''
{{Example|
 
<vip>class aaa
<vip>class aaa
end class aaa
end class aaa
Line 46: Line 46:


Subsequently, the class <vp>aaa</vp> belongs to the root namespace, the class <vp>bbb</vp> belongs to the namespace <vp>xxx</vp> and finally the class <vp>ccc</vp> belongs to the namespace <vp>xxx\yyy</vp>.
Subsequently, the class <vp>aaa</vp> belongs to the root namespace, the class <vp>bbb</vp> belongs to the namespace <vp>xxx</vp> and finally the class <vp>ccc</vp> belongs to the namespace <vp>xxx\yyy</vp>.
}}


=== Referencing names in namespaces ===
=== Referencing names in namespaces ===
Line 53: Line 54:
The leading backslash indicates that we start from the root namespace.
The leading backslash indicates that we start from the root namespace.


A class/interface can always be uniqly referenced using its full name.
A class/interface can always be uniquely referenced using its full name.


The full names are not always convenient and therefore it is possible to use shorter names in certain situations.
==== Open namespaces ====


=== Open namespaces ===
The full names are not always convenient and therefore it is possible to use shorter names by opening namespaces.


First of all it is possible to <vp>open</vp> namespaces inside a scope:
<vipbnf><ScopeQualification>: one of
    <OpenQualification>
    ...


<OpenQualification>: one of
    open <NamespaceIdentifier>\
    ...</vipbnf>
Opening a namespace is distinguished from opening a class/interface by a trailing backslash.
{{Example|
<vip>class aaa
<vip>class aaa
     open xxx\yyy\
     open xxx\yyy\
Line 66: Line 76:
end class aaa</vip>
end class aaa</vip>


Opening a namespace is distinguished from opening a class/interface by the trailing backslash.
The namespace <vp>xxx\yyy</vp> is opened inside <vp>aaa</vp>.
}}


The namespace that a certain scope (i.e. interface/class/implementation) belongs to is (implicitly) open inside that scope.
When a namespace is open that part of a full name can be left out.


=== Namespace qualification ===
{{Example| A domains with the full name <vp>\xxx\yyy\zzz\ccc::ddd</vp> can be as referenced <vp>zzz\ccc::ddd</vp> inside <vp>aaa</vp> because <vp>xxx\yyy</vp> is open.
}}


Since several namespaces be open simultaneously it is possible that names may clash.  Using the full name it is always possible to resolve any ambiguity.
Notice that the short name does not start with a backslash; A name starting with a backslash is always a full name.


But it is also possible to use '''''suffix qualification'''''.  With suffix qualification you write a suffix of the namespace. A suffix goes from right after a backslash to the end of the namespace identifier.
The namespace that a certain scope (i.e. interface/class/implementation) belongs to is (implicitly) open inside that scope.
 
'''Example'''  The suffixes of <vp>\xxx\yyy\zzz</vp> are:
 
<vip>xxx\yyy\zzz
yyy\zzz
zzz</vip>
 
So if <vp>aaa</vp> is a class in <vp>\xxx\yyy\zzz</vp>, these are the possible suffix qualifications:
 
<vip>xxx\yyy\zzz::aaa
yyy\zzz::aaa
zzz::aaa</vip>

Latest revision as of 17:03, 26 February 2019

Namespaces can be used to avoid name clashes, without having to use long strange names. The names in two different namespaces will never clash, but it may be necessary to qualify references with the namespace (or part of it) to resolve ambiguities.

A namespaces are declared and defined implicitly using NamespaceEntrance'es:

NamespaceEntrance:
   namespace NamespaceIdentifier
NamespaceIdentifier: one of
   \
   LowercaseIdentifier
   LowercaseIdentifier \ NamespaceIdentifier

In short a NamespaceIdentifier is a sequence of lowercase identifiers separated by backslashes.

Namespace Entrances and Regions

Namespace entrances divide source files into namespace regions.

A namespace entrance marks the beginning of a namespace region, which ends at the next namespace entrance or the end of the file.

Every file starts in the root namespace.

Namespace regions are not influenced by #include directives, meaning:

  • Namespace entrances in an #include-file does not change the namespace region in the including file
  • Any file starts in the root namespace (also if it is included inside a namespace region in another file).

Any interface, class and implementation that is meet inside a namespace region belongs to that namespace.

Example
class aaa
end class aaa
 
namespace xxx
 
class bbb
end class bbb
 
namespace xxx\yyy
 
class ccc
end class ccc

This file is divided in three regions (assuming that it is a complete file). The first region is in the root namespace (\), the second region belongs to the xxx namespace and the third region belongs to the xxx\yyy namespace.

Subsequently, the class aaa belongs to the root namespace, the class bbb belongs to the namespace xxx and finally the class ccc belongs to the namespace xxx\yyy.

Referencing names in namespaces

If ccc is a class in the namespace xxx\yyy, then the full name of ccc is \xxx\yyy\ccc.

The leading backslash indicates that we start from the root namespace.

A class/interface can always be uniquely referenced using its full name.

Open namespaces

The full names are not always convenient and therefore it is possible to use shorter names by opening namespaces.

ScopeQualification: one of
    OpenQualification
    ...
 
OpenQualification: one of
    open NamespaceIdentifier\
    ...

Opening a namespace is distinguished from opening a class/interface by a trailing backslash.

Example
class aaa
    open xxx\yyy\
...
end class aaa

The namespace xxx\yyy is opened inside aaa.

When a namespace is open that part of a full name can be left out.

Example A domains with the full name \xxx\yyy\zzz\ccc::ddd can be as referenced zzz\ccc::ddd inside aaa because xxx\yyy is open.

Notice that the short name does not start with a backslash; A name starting with a backslash is always a full name.

The namespace that a certain scope (i.e. interface/class/implementation) belongs to is (implicitly) open inside that scope.