From D Wiki
Jump to: navigation, search

In order to reduce the verbosity of D code (any D code, not just code making use of C++ libraries although they're the most affected) some experimental language features were added to D.

scope ref

A feature recurringly requested, its absence considered by many as producing terrible looking code, yet despite pull requests never made it into D:

Not being able to pass rvalues to ref parameters of non-template functions forces to manually declare temporaries:

void foo(ref SomeTy arg);

// foo(SomeTy(xyz)); // doesn't compile, so rewritten to:

auto temp = SomeTy(xyz);

A workaround is to write template functions with auto ref parameters, but this multiplies the number of symbols, and why shouldn't const ref or immutable ref be able to take rvalues in the first place, the C++ way?

Anyway mapped C++ functions by Calypso do not have the luxury of being rewritten as templates, and const Type& is omnipresent.

So because this wasn't bearable anymore, Calypso went ahead. Whether the reference escapes or not isn't checked, so the current implementation is a hack reaping the code beautification benefits while disregarding all safety and even correctness (temporaries should be used at least for literals):

Explicitly implicit constructor calls for functions arguments

In C++ constructors may be implicitly called by default unless marked explicit. This means that if a passed argument type ArgTy doesn't match any of the function overloads' parameter type ParamTy and if ParamTy is a class or struct, the C++ compiler looks for a constructor from ParamTy that takes an ArgTy argument and implicitly replace f(original_arg) by f(ParamTy(original_arg)).

D doesn't have implicit constructor calls. It may prevent some surprises, but a drawback is a lot more verbose code for instance whenever a custom string or reference class is needed:

class CustomStr
  CustomStr(string s) {...}

void Foo(CustomStr arg1, CustomStr arg2);

void Bar() {
  Foo(CustomStr("some string"), CustomStr("another string"));

This is a very common occurrence in C++ libraries. Using Qt means plastering the code with QSomeFunc(QString("lorem ipsum")), using OpenCV means InputArray or OutputArray everywhere, etc.

Implicit constructor calls have been little discussed as far as I know, but desired from times to times:

The @implicit attribute was added to DMD in order to enable implicit constructor calls, and in Calypso is applied to all C++ non-explicit constructors: