Dynamic arrays, basic type names, auto

bearophile bearophileHUGS at yahoo.com
Thu Jul 10 06:36:48 PDT 2008


Yet another of my lists of silly suggestions, this time shorter :-)

A syntax like this (that is a bit hairy, I agree) may be useful for time-critical spots in the code:

auto arr = new int[1_000_000] = int.max;
auto arr = new int[1_000_000] = void;

--------------------------

I may appreciate D 2.0 to have its basic types named like this:

- int1 int2 int4 int8
- uint1 uint2 uint4 uint8
(Fixed size. The number is their length expressed in bytes (shorter than using bits). There aren't int16 / uint16 yet)

- int
(built-in multi-precision signed integer. The compiler has heuristics to replace it with a faster fixed size integer in lot of situations. Optimized (if necessary modifying the GC too) for very short numbers that can fit in just 4-8 bytes, so even where the compiler isn't able to replace it with a fixed sized integer the slowdown isn't too much big, this is to hopefully let C++/C programmers be less scared of them. People that need to compute operations on very large integers have to tolerate a bit of constant slowdown, or use GMP).

- word uword
(They are the default size of the CPU, 4, 8 or 16 or 32 bytes, so their size isn't fixed. 'word' replaces the current 'size_t')

I don't like this much, but I don't see better alternatives so far (you need less memory to remember them and dchar, wchar, char):
- char1, char2, char4
(They are unicode. char1 is often 1 byte, and char2 is often 2 bytes long, but they may grow to 4, so they aren't fixed size)

- str
(replaces the current 'string'. Strings are used all the time in certain kinds of code, so an identifier shorter than 'string' may be better)

- bool null true false void
(Java uses 'boolean', but 'bool' is enough for D).

- set
(built-in set type, often useful, see Python3 and Fortress)

- list (or sequence, or seq)
(Haskell uses "2-3 finger trees" to implement general purpose sequences, they may be fit for D too, they aren't meant to replace arrays
http://en.wikipedia.org/wiki/Finger_tree ).

Plus something else for float/double/real complex types.

-----------------------

D used 'auto' for automatic typing of variables:
auto s = "hello";

C# uses 'var', that's just 3 chars long and equally readable:
var s = "hello";

'let' is more logic, shorter than 'auto' and it was used in Basic too:
let s = "hello";

'var' can be written on the keyboard with just the left hand, so it may be written even faster than 'let'. But 'var' in modern D replaces 'inout'. So I think 'let' may be a good compromise for D.

Bye,
bearophile



More information about the Digitalmars-d mailing list