2014-11-28
Review of the D 2 Programming Language
Familiarity with C is assumed.
First Program
import std.stdio; int main() { writefln("Hello world"); return 0; }
High-Level Feature List
- Garbage Collection exists and is used.
- Manual freeing of allocated memory is possible.
- Lambdas exist and are used.
- Automatic type inference exists and is used. Limitation: the First Program above would not work with auto main
- typeof exists and can be used in variable and parameter declarations, also typeof(return)(...) works.
- Lazy evaluation exists and can be optionally specified by a type modifier of a parameter.
- Closures exist and work properly.
- Exceptions exist and are used.
- Classes exist and are used. Topmost class is Object.
- All variables are automatically initialized.
- Has Module System.
- Templates that don't suck exist. One-similarly-named-member will replace template instantiation if needed.
- Uniform Function Call Syntax is used (as in Go, Dylan). So v.foo(arg) is the same as foo(v, arg) .
- Proper arrays with length and fallback to the element pointer (using ptr) exist.
- Ranges as safe-ish arrays and interfaces to array-like things and iterators exist.
- Implicit casts are done a lot less than in C.
- Strings are built-in and immutable.
- Associative arrays (dictionaries) are a built-in type.
- Types can have modifiers like immutable, const, shared.
- Functions can be pure or nogc.
- Variable arguments are handled a lot nicer than in C.
- Operator overloading exists.
- Array and associative array literals (with []) and struct literals (with {}) are possible.
- foreach exists (foreach(char[] arg; args) ...).
- version blocks which allow conditional compilation of blocks exist.
- Array Slices as views into "larger" arrays exist.
- Supports function contracts (preconditions, postconditions, invariants) like void foo() in { assert ... } out(result) { assert ...} body {...} invariant { ... } .
- Built-in functionality for unit tests exists, also doctests exist.
- scope exists. It is used for deferred cleanup (scope(exit) { ... }). Also for marking that parameters are not used beyond the scope of the function.
- scoped exists. It is used to force stack allocation. Is unsafe.
- @disable to disable constructors exists.
- Float literals: 1.23 or 0x9a.bc or 0xabc.defP4 or 1_000.5. The f modifier is for using float.
- String literals: escaped-aware "xxx", raw `xxx` or multiline q"EOF...EOF".
- goto case in switch must be used to go to the next case.
- Volatile accesses (__iomem) are done using special functions (volatileLoad, volatileStore).
- std.algorithm: map and filter exist.
- opDispatch member function: called when a message arrives that the object does not understand.
- C interop: strdup exists and returns a char[].
- std.array.appender exists: used for repeated appending to something (i.e. strings etc), also formattedWrite. Read result using data
High-Level "Cultural" Problems
- C-like model for numbers: no overflow detection, division of integers truncates.
- Java-like model for exceptions: you cannot resume where an exception was caused (to retry). Exceptions are class instances.
- For all reference types: null is a member. (though see Nullable)
- Even though Algebraic exists, unions still exist and the compiler still can't decide between the fields on its own.
- Algebraic cannot be recursive.
- Lexical Order at global scope doesn't matter. Why not?
- Mutable is the default.
- Impure is the default.
- Operator overloading doesn't work with a global function as implementation.
- Type declarations are read right-to-left, as in C. double[int][string] is an associated array from a string to (an associated array from an int to a double)
- Primitive Types like int etc have a fixed size regardless of architecture - rather than what's the biggest fits-into-register integer or float.
- Even though closures work, the closures are called "delegates" and the C-like functions without any context are called "functions" even though in the field there are only functions. There's a delegate keyword and a function keyword and they differ.
- enum is used for int and string constants (among other things), and algebraic data types are not called enum. Also, C enum "values" are automatically "converted" to integers.
- Uses opCmp as shortcut for defining all the ordering relation, even though it's possible for a number to be neither smaller NOR bigger than another number. Not sure if opCmp can be replaced by opEquals then.
- Calls a type real that is not the real numbers.
Implementation Problems
- Bindings for many Linux headers are missing.
- No clear road for streams and main interface is deprecated.
- gdc needs strange command line arguments (-frelease -O3 -fdata-sections -Xlinker --gc-sections and then strip) in order to make it do smart linking and get a reasonable executable file size.
Tips
Types
Character
- char: UTF-8 code unit.
- wchar: UTF-16 code unit.
- dchar: UTF-32 code unit and Unicode code point, max 0x0000FFFF anyway (WTF).
wchar currencySymbol = '\€'; wchar Ğ_w = '\u011e'; dchar Ğ_d = '\U0000011e';
Void
- void
Integers
- int
- uint
- byte
- ubyte
- long
- ulong
Floating-Point
- float
- double
- real
String
- string
Others
- function "pointer"s
- arrays
- associative arrays
- enums
- Algebraic
- structs
- unions
- classes
- variant
System Library Bindings
- Gtk+, gstreamer, gda, gl, sv, vte bindings exists. (GtkD)
2014-11-29
D Algebraic Data Types
import std.variant : Algebraic; import std.stdio : writeln, stdout; int main() { auto v = Algebraic!(int,string)(2); stdout.writeln(v); int a = *v.peek!(int); stdout.writeln(a); //stdout.writeln(v.peek!(string)); // is null string s = *v.peek!(string); // works but bad idea //stdout.writeln(s); // crash // compilation error: long b = *v.peek!(long); v = "hello"; return 0; }
What Algebraic does is implement a very specific sum type, the easiest one, as a class.
I don't know what that kind of overcomplication is supposed to bring but fine, it works.
Currently, Algebraic does not allow recursive data types.
Allegedly, future additions to Algebraic will allow compile-time checking that all possible types are handled by the program.
2014-11-30
D Array Slices
import std.stdio; void main() { int[] slice = [ 1, 3, 5, 7, 9, 11, 13, 15 ]; int[] half = slice[0 .. $ / 2]; int[] quarter = slice[0 .. $ / 4]; slice ~= 42; // adding to the longest slice ... slice[1] = 0; // ... and then modifying an element writeln(quarter); writeln(half); writeln(slice); }
Result
[1, 0] [1, 0, 5, 7] [1, 0, 5, 7, 9, 11, 13, 15, 42]
2015-08-14
Because of current events in the world, I made my computers a little bit more trustworthy.
Olimex LIME2
The Olimex LIME2 is a small Allwinner A20-based development board.
Everything is Free Software except for the 3D graphics.
By "everything" I mean:
- Board Bootloader
- U-Boot
- Gigabit Ethernet
- USB 2.0
- SATA II
- NAND flash
- Kernel
Disadvantage: CPU is a bit slow (800 MHz).
Disadvantage: graphics can do at most 1920x1080 at 60 Hz and that pretty slowly.
Disadvantage: RAM has some serious stability problems. That is because dcdc3 should be 1300 mA but is less. Have to patch u-boot/board/sunxi/board.c manually.
TODO increase DRAM clock to 532 MHz which should allegedly work.
I connected a WLAN USB adapter to it, using a L adapter in order to decrease interference.
The WLAN USB adapter sometimes exceeds the power limit so I limited txpower. That helps a lot.
Lenovo X200
Lenovo doesn't really have a good track record about security ... or even sanity lately.
However, this laptop is quite old (CPU is still much faster than the A20 above). Back then Lenovo wasn't that much of an asshole.
I replaced the wireless chip (PCI Express), removed the WWAN chip and used a SOIC 16 test clip to flash libreboot instead of the Lenovo BIOS (that BIOS was especially untrustworthy). While at it, I disabled write access to any of the flash regions by the main CPU.
I then installed GuixSD on it.
Everything seems to work fine.
Disadvantage: has a fan.
Disadvantage: The Fn and Ctrl keys are in the wrong order (Ctrl is not the leftmost key) and swapping them doesn't work.
Disadvantage: Embedded Controller is still closed source I think.
Disadvantage: Display Panel is TN. Better (and compatible) is HV121WX4-120. Still only 1280 px x 800 px
I still have to find out what the empty expansion port is.
Future Research
There are several Free CPU designs, for example OpenSPARC or MIPS. Find out whether anyone sells chips of them and validate them.
Maybe get the HP Allwinner A33 tablet.
Compiler Trustworthiness
Compilers are also an obvious problem.
Best to Worst:
- Compiler that is GPL licensed.
- Compiler that is BSD licensed.
- Compiler that is "open source" and can be fixed locally.
- Compiler that is "open source".
- Compiler that is based on "open source".
- Proprietary Compiler.
Privacy
Use Privacy Badger and Tor Browser.
Maybe use superglue.it hardware (D-Link DIR-505 or TP-LINK WR-710N) or FreedomBox to host your website.
Maybe FreedomBox.
Website
Maybe neocities.org hosting?