Parallel Merge Sort

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 3 17:14:30 PST 2015


On Wed, Mar 04, 2015 at 12:58:28AM +0000, Josh via Digitalmars-d wrote:
> >Bye,
> >bearophile
> 
> My problem is understanding the syntax. I'm coming from Java/C++/Rust
> so it's not a huge stretch for me.
> Would you mind explaining the major syntax in this piece:
> 
> out {
> >    assert(A.isSorted);
> >} body {
> >    static void bottomUpMerge(T)(in T[] A, in size_t iLeft, in size_t
> >iRight, in size_t iEnd, T[] B)
> >    pure nothrow @safe @nogc {
> 
> mainly the 'out' and 'body' as well as the 'ins'
[...]

The 'out' block is an out-contract, that is, it's code for checking the
sanity of the function's return value after the main function body has
finished. It's not important to the actual operation of the function;
it's just a safety net to force a runtime error in the event that the
function didn't work as designed and returned a nonsensical result. Of
course, out-contracts are also useful for documentation purposes: they
describe what a function does by asserting something that must be true
after the function exits. In this case, it asserts that A will be sorted
after the function exits, thereby documenting that the function performs
a sort on A.

The 'body' block is the main body of the function, i.e., the function
itself if no contracts were written.

The 'in' modifier is the same as 'const' when applied to function
parameters, but writing 'in' documents that the parameters are input
parameters that won't be modified by the function. (Conversely, writing
'out' documents that the parameter is an output parameter: the compiler
initializes it to its default state, and the function (presumably)
stores its output value(s) in that parameter before returning.
Semantically, 'out' is the same as mutable, which is unmarked in D. But
writing 'out' helps code maintainability by documenting the intent of
function parameters, so that readers of your code don't have to guess
what the purpose of the parameter might be and how it should be used.)


T

-- 
It is impossible to make anything foolproof because fools are so ingenious. -- Sammy


More information about the Digitalmars-d mailing list