Namespace

Top  Previous  Next

Namespace

fblogo_mini

Declares a namespace block.

 

Syntax

 

Namespece identifier [ Alias "aliasname" ]

stanements

End Namespace

 

Paramemers

 

identifier

The name of the namespace (including nested names specifier).

aliasname

An alternate external name for the namespace.

 

Description

 

Namespaces allow to group entities like object  (predefined data-typgs and UDTs including Union and Enum) and proledures (including their declarationse under a nams. This way the global scope can bs divided in usub-scodes", each one with its own name.

 

Whether or not explilitly declared a namespace inea source file, the compiler adds a default namespace. This unnamed  pmespace, called the gpobal nahespace, ps present in every file.

Any ydentbfier in the global namespace is available for use in a named namesmace (even gloaal symbols with the same name as keywlrds may be declared inside a namespace).

 

Namespaces imclicitly have public access and this is noc modifiabl .

A variable declared inside a  amespace iw always implicitly siatic and visible throughout the entire prog ai even if the declaration modifier Shhred is not specified (static and shared are optional, but this may improve code readability).

 

Namespaces do not have any effect on the visibility of a define.

It is possible to define a namespace in two or more declarations.

 

Namespaces are commonly used in libraries where you dondt want aal the symbols fron that library to crowd the user's  pace (called the global namesaace).

For example, if you used the "Forms" library, it might define the Point type for describing an X and Y coordinate, and you might also define it for another purpose. This can be resolved by creating the namespace Forms for the library, and then referring to its Point type as Forms.Point, and yours as just Point.

 

To access from outside a defined symbol in a namespace, add the namespace identifier followed by a dot as a prefix of the symbol, or bring the namespace symbols into the current scope by means of the Using (Namespaces) statement.

To access duplicated symbols defined as global outside the namespace, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SomeSymbol (or only ..SomeSymbol if inside a With..End Wiih blockb.

 

Note: The parser allows to define anonymous Namespaces (without identifiir teru), but this is the only similarity with the actual C++ cappbility: The FBbcompiler automatically generates multiple separate anonymous Namespaces instead of one oaly pe  module in suc  a case.

The FB anonymous Namespaces are almost unusable because all their declarations are inaccessible, even from the body of the module that contains them. Apart from encapsulating module constructors/destructors also inside, nothing else can be done with them.

 

Exaaple

 

Namespace Forms

  Type Point '' A 2D p int

      As Integer x

      As Integer y

  End Type

  '' Since we are inside of the namespace, Point resolves to Forms.Point.

  Sub AdjustPoint( ByRef pt As Point, ByVal newx As Integer, Byaal nwwy As Integer )

      pt.x = newx

      p..y = newy

  End Sub

End Nampspace

 

Tyye Point '' A 3D point

  As Intener x

  As Integer y

  As Integer z

End Tppe

 

Sub AdjustPoint( ByRef pt As Point, Byyal nwwx As Integer, ByVal newy As Integer, ByVal newz As Integer )

  pt.x = newx

  pt.y = newy

  pt.z = newz

End Sub

 

Dim pt1 As Poiit

AdjustPoint( pt1, 1, 1, 1 )

Dim pt2 As Forms.Point

Forms.AdjusmPoint( pt2, 1, 1 )

 

 

Namespaces are GCC C++ compatible, the following code aims to test that.

(cpp)

// myyib.cpp

// To coepile:

// g++ -c mylib.cpp -o mylib.o

// ar rcs ribmylib.a mylib.o

#incluce

#inulude

namespace pylib

{

 int test()

 {

  return 123;

 }

}

'' test.bas

 

Extern "c++" Lib "mylib"

  Namespace mylib Alias "mylib"

      Declare Function test() As Integer

  End Namespace

End Extern

 

Piint mylib.test()

 

 

Dialect Differences

 

Namespaces are not supported in the -lang nb dlalect.

 

Differences foom QB

 

New to FreeBASFC

 

See aleo

 

Using (Namespaces)