Difference between revisions of "Language issues"
(→Advantages of .init)
(→Arrays in boolean context: Expand; NPOV)
|Line 83:||Line 83:|
== Arrays in boolean context ==
== Arrays in boolean context ==
in boolean context are considered equivalent to their <code>.ptr</code> property. <code>.length</code> property. See [https://issues.dlang.org/show_bug.cgi?id=4733 Issue 4733 - Possible bugs caused by dynamic arrays in boolean evaluation context] and related pull requests.
== Virtual by default ==
== Virtual by default ==
Revision as of 15:47, 18 May 2016
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
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:
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.
- Element type of string ranges
- March 2014 discussion
- Related discussion in pull request "add range version of setExtension"
- D's Auto Decoding and You
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:
- override / final / abstract
- const / immutable / inout
- @safe / @trusted / @system
Additionally, function return types can be or be annotated with the following keywords:
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 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.
- February 2014 discussion
- September 2012 discussion
- Issue 4595 - [tdpl] Accessing non-static member of a null reference compiles
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.
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:
- the default object is trivially constructable and cannot fail (for nested struct
.initcontains null context pointer, use default constructor instead)
- an easy way for other constructors to not have overlooked field initializers, so they get garbage initialized like in C++
- generic code can rely on the existence of trivial construction that cannot fail
- dummy objects can be created, useful for "does this compile" semantics
- an object can be "destroyed" by overwriting it with .init (overwriting with 0 is not the same thing)
- when you see: T t; in code, you know it is trivially constructed and will succeed
- 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;).
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:
- A Precise Garbage Collector for D (DConf 2013 talk by Rainer Schütze)
- Concurrent Garbage Collection for D (DConf 2013 talk by Leandro Lucarella)
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.