Polyglot: Concepts
    Overview
    Concepts
    Compile Time and Runtime
    Build Tools
    Numerics
    Memory Management
    Zero Cost Abstractions
    Design Patterns
    Compatibility
    Functional Programming
    Bit Manipulation
    Generics
    String Interning
Polyglot: Language Notes

Programming Languages - Concepts

Updated: 2022-04-25

Standard vs. Implementation

  • Standard is the language specification, listing all the definitions and features of the language, however nothing "physical".
  • Implementation is the actual engine that can execute the code that comply with the spec.

Examples:

  • Java:
  • JavaScript/ECMAScript:
    • one specification: ECMA-262
    • JavaScript, JScript and ActionScript are all implementations of ECMA-262 standard. The implementations are compatible with ECMAScript though may also provide additional features.
  • SQL
    • Standards like SQL-92, SQL-2011, etc.
    • Databases usually implement only a portion of the standards, plus some custom features. So one SQL query from a certain database may or may not work in another database.

Imperative vs Functional

  • In a imperative program: a function depends on two things
    • the parameters it is passed and also
    • the current state of the machine store.
  • In a functional language:
    • only the parameters it is passed matters.

Dynamic Type vs Static Type

Dynamically typed language means that variables do not have types, only values do. There are no type definitions in the language. All values carry their own type.

Rust goes a middle way: It’s a static type system, but it only requires the programmer to specify top-level types like function arguments and constants. Inside function bodies, Python-style type inference is allowed.

Dynamically Linked vs Statically Linked

  • static: link at compile time
  • dynamic: link at runtime

Some languages use both: static linking to deploy, dynamic linking to iterate

Statically Declared Array vs Dynamically Declared Array

  • static: allocates memory at compile-time, size is fixed, in stack, no need to manage memory
  • dynamic: can alter the size at run time, in heap, need to allocate and free memory
int* a = new int[10];
delete[] a;

Alternatively std::vector can be used, which resizes itself automatically

std::vector<int> v = {1, 2, 3, 4}

System Programming Languages

wiki

A system programming language usually refers to a programming language used for system programming; such languages are designed for writing system software, which usually requires different development approaches when compared with application software. System software is computer software designed to operate and control the computer hardware, and to provide a platform for running application software.

Mutability

  • Immutable: cannot be changed once created
  • Mutable: can be changed

Java: String and all of the number classes, such as Integer, Double, Character, and BigInteger.

Stack vs Heap

  • Stack: primitive values, references to objects, and methods; short-lived. LIFO. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to the other objects.
  • Heap: object created by new; array is also object, so also in heap.
    • Young gen
    • Old gen
    • PermGen(replaced by Metaspace, which uses native memory, in Java 8)
  • use -Xmx, -Xms to specify the size of Heap
  • use -Xss to specify the size of stack

Recursive methods usually use much more stack space, and therefore more memory, than an iterative counterpart.

COBRA vs RMI

  • RMI is a Java-specific technology. CORBA has implementations for many languages. You can use CORBA to share objects between programs written in different languages (e.g. C++ and Java).
  • CORBA uses IDL (Interface Definition Language) to separate interface from implementation. RMI just uses Java interfaces.

PPL: Probabilistic Programming Language

http://dippl.org/

4th Generation Programming Language

https://en.wikipedia.org/wiki/Fourth-generation_programming_language

Char in Programming Language

  • Java: 2 bytes, 16 bits, unsigned([0, 65535]), unicode(UTF-16)
  • C/C++: 1 byte, 8 bits, ASCII

Domain-Specific Language (DSL)

Different from a General-Purpose Language (GPL), a DSL is for a specific problem. For example, CSS is just for styling web elements, SQL is for database queries, make for building programs.

Ruby, Scala, Groovy are especially good for building DSLs. E.g. Gremlin is a graph traversal language written in the Groovy.

Weak Reference

Weak reference is a reference that does not protect the referenced object from collection by a garbage collector, the object will be garbage collected once they are not reachable elsewhere, even though this weak reference is still pointing to it.

Some garbage-collected languages support weak references, like Java (WeakReference class), C#, Python (weakref module), etc.

Collections

  • Java LinkedHashMap = python collections.OrderedDict: remembers the order that keys were first inserted
  • Java TreeMap: red-black tree based, sorted by the keys, log(n) for all actions; no python equivalent

HPC

High-performance computing (HPC) frameworks, such as OpenMP and OpenACC: provide application program interfaces (APIs) that programmers can use to express parallelism in code.

Clousure

Closures are special functions that can capture the environment, i.e. variables within a lexical scope. Or "an inner function that has access to the outer function’s variables", i.e. a clousure can access variables, not in its parameter list.

A callback using context variables is a closure.

In C++, "closures" = lambda expressions. lambda:closure = class:instance. Closures are to lambdas as objects are to classes: closures and instances are runtime only; lambdas occupy no data memory at runtime but may occupy code memory, closures occupy data memory but not code memory (In other languages like Python, closure is unrelated to Lambdas)

In JavaScript, Clousures can be used to emulate private methods as in other languages: they 'remember' the data and you can later operate on the data through the returned functions.

Type Introspection and Reflection

  • Type Introspection: the ability of a program to examine the type or properties of an object. C++ is not capable of introspection at runtime, only compile-time.
  • Reflection: the ability of a process to examine, introspect, and modify its own structure and behavior.

Type Casting vs Type Conversion

Trivially destructible

Informally this means that the destructor does not do anything, even taking member and base destructors into account. More formally it means that the type has no user-defined or virtual destructor and that all bases and non-static members are trivially destructible.

Pointers

A pointer is an object that stores a memory address.

Smart pointers are data structures that have additional metadata and functionalities.

Foreign function interface (FFI)

A program written in one programming language can call routines or make use of services written in another.

In Java, it is called JNI (Java Native Interface) or JNA (Java Native Access).

Java foreign function: