Questions Concepts
GitHub icon


Namespaces - language feature

< >
#include <iostream> using namespace std; // Variable created inside namespace namespace first { int val = 500; } // Global variable int val = 100; // Ways to do it: namespace ns_name { declarations } inline namespace ns_name { declarations } namespace { declarations } ns_name::name using namespace ns_name; using ns_name::name; namespace name = qualified-namespace ; namespace ns_name::inline(since C++20)(optional) name { declarations }
Example from C#:
namespace MyNamespace; class MyClass { public void MyMethod() { System.Console.WriteLine("Creating my namespace"); } }
// Typescript even supports splitting namespaces across multiple files: // Validation.ts namespace Validation { export interface StringValidator { isAcceptable(s: string): boolean; } } // LettersOnlyValidator.ts /// <reference path="Validation.ts" /> namespace Validation { const lettersRegexp = /^[A-Za-z]+$/; export class LettersOnlyValidator implements StringValidator { isAcceptable(s: string) { return lettersRegexp.test(s); } } }
Example from F#:
namespace Widgets type MyWidget1 = member this.WidgetName = "Widget1" module WidgetsModule = let widgetName = "Widget2"
Example from C3:
import std::io; ... io::printf("%d", i);
Example from Slope:
(ns-create 'hello-world) (ns-define 'hello-world 'print (lambda () (display "hello world!\n"))) (hello-world::print)

Languages with Namespaces include C++, C#, TypeScript, F#, C3, Slope, Dale, Real-Time Concurrent C, Static Typescript

Languages without Namespaces include C, Objective-C, Speedie, Tick C

This question asks: Does the language have a namespace construct?

HTML of this page generated by Features.ts

View source

- Build the next great programming language Search Add Language Features Creators Resources About Blog Acknowledgements Queries Stats Sponsor Traffic Traffic Today Day 305 Logout