Language issues

From D Wiki
Revision as of 18:21, 18 May 2016 by Vladimir Panteleev (talk | contribs) (Attribute creep: style)
Jump to: navigation, search

Like any programming language, D is not perfect. This page lists some potential problems that have been brought up and debated throughout the language's history. Fixing these is not straight-forward or may not justify breaking backwards-compatibility. As of the moment of writing this, there are no plans for a major revision of D which would allow such breaking changes, but this list may also be useful for other programming languages which wish to learn from D's experience.

See also: Language design discussions

Properties

Design of properties has been an oft-discussed topic. This language feature has evolved throughout D's history (e.g. the addition and removal of the -property switch). There are numerous DIPs with proposals on improving the design:

See also:

Unicode and ranges

D's ranges currently treat strings in a special way: they present them as a range of code points, thus implicitly performing UTF decoding. Algorithms that do not require working with decoded code points, and which would gain a performance advantage by treating the input as a range of code units (or an array of code units), must incorporate D strings as a special case. This approach complicates the implementation by some degree, and only solves a certain subset of problems for a subset of written languages.

Walter Bright has mentioned that auto-decoding is his #1 thing he currently dislikes about D [1] [2].

See also:

Attribute creep

In a 2014 forum thread titled "What are the worst parts of D?", a prevalent complaint was the number of attributes that functions can be decorated with.

D currently allows the following attributes:

Additionally, function return types can be or be annotated with the following keywords:

Signed/unsigned comparisons

Currently, the following code fails:

uint i = 1;
assert(i > -2);

i > -2 evaluates to false due to C compatibility (the same expression also evaluates to false in C, and one of D's goals is that if any C code compiles in D, then it should act like in C). One proposal is to make signed/unsigned comparisons generate a warning.

See issue 259 for discussion.

Null References

Null references have often been blamed as a common class of bugs, which could be prevented statically by the compiler (e.g. as a NotNull modifier, or explicitly marking variables possibly containing null values). This can be done by statically checking if all code paths (e.g. all class constructors) initialize a reference variable to a non-null value.

See also:

Arrays in boolean context

Dynamic array variables in boolean context (e.g. when encountered in an if condition by themselves) are considered equivalent to their .ptr property. It has been argued they should instead use their .length property, so that empty (but not null) arrays evaluate as false. See Issue 4733 - Possible bugs caused by dynamic arrays in boolean evaluation context and related pull requests.

Virtual by default

In D, methods are virtual by default. Although this approach does have benefits from the point of view of extensibility, it has been criticized that this default incurs a considerable performance impact and harms forward compatibility in user code, unless explicitly disabled everywhere as appropriate using the final keyword.

Although at one point Walter Bright intended to change the default (see Issue 11616 and DIP51), ultimately this proposal has been rejected.

Default constructors

D does not support custom default constructors for value types: attempting to declare a struct constructor without any arguments is an error. The rationale for this limitation is to allow any type to have a known constant default value (.init property). This is unlike C++, where structs and classes can have default constructors, which allow executing custom code at the point of a variable's declaration.

D's restriction prevents implementing certain C++ patterns, and requires users to rely on conventions for factory functions.

Advantages of .init

According to Walter Bright, some advantages of .init over default constructors are:

  1. the default object is trivially constructable and cannot fail (for nested struct .init contains null context pointer, use default constructor instead)
  2. an easy way for other constructors to not have overlooked field initializers, so they get garbage initialized like in C++
  3. generic code can rely on the existence of trivial construction that cannot fail
  4. dummy objects can be created, useful for "does this compile" semantics
  5. an object can be "destroyed" by overwriting it with .init (overwriting with 0 is not the same thing)
  6. when you see: T t; in code, you know it is trivially constructed and will succeed
  7. objects can be created using TypeInfo

Explicit syntax and @disable this

In theory, explicit struct construction syntax S s = S(); could allow calling a zero-argument constructor (vs. just S s; which is default construction). Forgetting to use the explicit syntax can be solved by disabling the default constructor (@disable this;).

See also:

Memory management

D is often criticized for its reliance on a garbage collector, and the quality of the current GC implementation.

The current GC implementation is:

  • conservative (meaning, it can't guarantee whether any object actually has live references to it)
  • non-moving (the GC doesn't attempt to compact the heap to reduce fragmentation and working set size)
  • stop-the-world (all threads are stopped during a GC scan)

Precise (as far as the heap is concerned) and concurrent D GC implementations have been presented:

Although the GC can be disabled, using D completely without the GC may be challenging, as some parts of D expect a GC to be available:

  • Many modules in Phobos, D's standard library, expect a GC to be present to clean up temporary allocations;
  • D language features such as arrays require careful use to avoid memory leaks;
  • D language features such as closures and exceptions cannot be used, as they do not provide a way to manually deallocate allocated memory.

Some alternative runtimes / standard libraries which do not rely on the GC are listed here.