Calypso/LanguageAdditions

From D Wiki
Revision as of 16:38, 11 July 2016 by Syniurge (talk | contribs)
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:

 http://forum.dlang.org/post/mailman.3720.1453131378.22025.digitalmars-d@puremagic.com

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);
foo(temp);

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? Some vocal opponents have an argument.

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):

https://github.com/Syniurge/Calypso/commit/652780400cac85aa75670eba263c93e48bf2432f https://github.com/Syniurge/Calypso/commit/af60ca29add17df0f45cd722c39d756668d453cf

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 look 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 for 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:

 http://forum.dlang.org/post/ltdchmnokbgyjctiqxru@forum.dlang.org

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:

https://github.com/Syniurge/Calypso/commit/999bd81cd094584a1db2357230888bf7d96e7d09 https://github.com/Syniurge/Calypso/commit/66df399a0d0bf5f3ccd8c96db354635eef86480b https://github.com/Syniurge/Calypso/commit/4e080f5cd1fa636a98d50d35a2632abcdcbe75cc https://github.com/Syniurge/Calypso/commit/471fad6a1dba98c752d1dee1f81e836d16a89320

Considered: enabling UFCS for overloaded operators