Library Instantiation Convenience

For modules defined for libraries, it is often desirable to choose one name for the library, module, and class. For example, a Base64 library may want to define a module named Base64 and a class named Base64 as follows:

1
2
3
4
5
6
7
module Base64
{
    class Base64
    {
        // ...
    }
}

However, due to the module and class having the same name, an inconvenience can arise when using the library:

1
2
3
4
import Base64;
 
// auto base64 = new Base64(); // 'Base64' is a module and cannot be instantiated
auto base64 = new Base64.Base64(); // Specifying the full path allows the compiler to resolve the name

JS++ provides the "library instantiation convenience" to remedy this:

1
2
3
import Base64;
 
auto base64 = new Base64(); // resolves to 'Base64.Base64' class

The rule for the compiler is defined as:

If the user instantiates an identifier for a class that has the same name as its containing module, instantiation will resolve the identifier to the class of the containing module rather than the containing module itself — if and only if the containing module has been imported.

Note that the convenience is not provided for function members of modules. Due to the naming conventions of JS++, module names and class names are recommended to be in UpperCamelCase while function names are recommended to be in lowerCamelCase. Consequently, function names and module names do not commonly collide.

See Also

Share

HTML | BBCode | Direct Link