cpp_namespace_keyword

CPP namespace keyword

Return to namespace, C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics


Here's a comprehensive comparison of the C++ `namespace` keyword and its equivalent constructs in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL, including code examples and links to the respective language documentation. The concept of namespaces is fundamental for avoiding name collisions and organizing code logically across many programming environments.

  1. C++

In C++, `namespace` is a keyword used to define a named scope that allows the organization of code into distinct logical groups, preventing name collisions.

```cpp namespace MyNamespace {

   int myFunction() {
       return 0;
   }
} ```

C++ documentation: s://en.cppreference.com/w/cpp/keyword/namespace(https://en.cppreference.com/w/cpp/keyword/namespace)

  1. Python

Python uses modules as its way of implementing namespaces. A module can be created by saving code in a `.py` file, which then can be imported using the `import` statement. Python doesn't have a direct `namespace` keyword, but modules and packages serve a similar purpose.

```python

  1. mymodule.py

def my_function():

   return 0
```

```python import mymodule mymodule.my_function() ```

Python documentation: s://docs.python.org/3/tutorial/modules.html(https://docs.python.org/3/tutorial/modules.html)

  1. Java

Java utilizes packages as namespaces. The `package` keyword defines a namespace, allowing for organized, reusable code across different parts of a program or different programs.

```java package mypackage;

public class MyClass {

   public static int myFunction() {
       return 0;
   }
} ```

Java documentation: s://docs.oracle.com/javase/tutorial/java/package/packages.html(https://docs.oracle.com/javase/tutorial/java/package/packages.html)

  1. C#

C# uses namespaces to organize code in a similar manner to C++. The `namespace` keyword is used to declare a scope that contains a set of related objects.

```csharp namespace MyNamespace {

   class MyClass {
       public static int MyFunction() {
           return 0;
       }
   }
} ```

C# documentation: s://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces(https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/namespaces)

  1. Kotlin

Kotlin groups functions, variables, and other definitions into packages, which serve as Kotlin's version of namespaces. There's no `namespace` keyword; packages are used instead.

```kotlin package mypackage

fun myFunction(): Int {

   return 0
} ```

Kotlin documentation: s://kotlinlang.org/docs/packages.html(https://kotlinlang.org/docs/packages.html)

  1. JavaScript

JavaScript ES6 introduced modules, which serve as the language's approach to namespaces. While there's no `namespace` keyword, modules help in organizing code and preventing name collisions.

```javascript // myModule.js export function myFunction() {

   return 0;
} ```

JavaScript documentation: s://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)

  1. TypeScript

TypeScript enhances JavaScript's module system and adds namespaces. The `namespace` keyword is used for grouping functionalities and avoiding naming conflicts.

```typescript namespace MyNamespace {

   export function myFunction() {
       return 0;
   }
} ```

TypeScript documentation: s://www.typescriptlang.org/docs/handbook/namespaces.html(https://www.typescriptlang.org/docs/handbook/namespaces.html)

  1. PHP

PHP's namespaces were introduced in PHP 5.3.0, allowing for better code organization and the avoidance of name collisions between classes, functions, and constants.

```php namespace MyNamespace;

function myFunction() {

   return 0;
} ```

PHP documentation: s://www.php.net/manual/en/language.namespaces.php(https://www.php.net/manual/en/language.namespaces.php)

  1. Go

Go uses packages to organize code. Each file belongs to a package, which is Go's way of implementing namespaces. There's no `namespace` keyword.

```go package mypackage

func MyFunction() int {

   return 0
} ```

Go documentation: s://golang.org/doc/effective_go#packages(https://golang.org/doc/effective_go#packages)

  1. Rust

Rust uses modules, defined with the `mod` keyword, to organize code into namespaces. This allows for encapsulation and reusability of code.

```rust mod my_module {

   pub fn my_function() -> i32 {
       0
   }
} ```

Rust documentation: s://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html(https://doc.rust

-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html)

  1. Swift

Swift uses modules to organize code, but within a module, `namespaces` are implicitly defined by the types (classes, enums, structs). There's no explicit `namespace` keyword.

```swift // In Swift, namespaces are not explicitly declared. class MyClass {

   static func myFunction() -> Int {
       return 0
   }
} ```

Swift documentation: s://docs.swift.org/swift-book/LanguageGuide/ModulesAndSourceFiles.html(https://docs.swift.org/swift-book/LanguageGuide/ModulesAndSourceFiles.html)

  1. Transact-SQL

Transact-SQL does not have a direct equivalent to namespaces as seen in languages like C++ or C#. However, schemas in SQL Server can serve a similar purpose, grouping database objects together.

```sql CREATE SCHEMA MySchema; GO

CREATE TABLE MySchema.MyTable (

   ID int,
   MyColumn varchar(255)
); GO ```

Transact-SQL documentation: s://docs.microsoft.com/en-us/sql/t-sql/statements/create-schema-transact-sql(https://docs.microsoft.com/en-us/sql/t-sql/statements/create-schema-transact-sql)

  1. PL/SQL

PL/SQL uses packages to group related procedures, functions, and variables. While not directly called `namespaces`, packages achieve a similar organizational structure.

```plsql CREATE OR REPLACE PACKAGE MyPackage AS

   FUNCTION MyFunction RETURN NUMBER;
END MyPackage; / ```

PL/SQL documentation: s://docs.oracle.com/database/121/LNPLS/packages.htm(https://docs.oracle.com/database/121/LNPLS/packages.htm)

Each programming language has its own way of implementing the concept of namespaces, whether through explicit keywords like `namespace` in C++ and C#, or through packages and modules in languages like Java, Python, and Go. These mechanisms are crucial for code organization, readability, and the prevention of naming conflicts.


Fair Use Sources

C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)

Reserved Words: Programming Language Keywords, aka Reserved Identifiers. (navbar_reserved_words - see also navbar_programming)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


cpp_namespace_keyword.txt · Last modified: 2024/05/01 03:52 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki