non empty slices

Alex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 2 06:37:46 PDT 2016


Ok, a strange question from my side again...
Let's begin, with what works:
Say, I define two types:

import std.typecons : Nullable;
alias M = uint;
alias Mr = Nullable!M;

then, I can write two types of methods. Those which can handle 
Mr's:
void fun1(Mr val)
{
     //do some funny stuff
}

and those, which assume an existing value is provided:
void fun2(M val)
{
     //do some other funny stuff
}

they could be overloaded by using the same name I think... but 
this is not the point.

The question is, how to define the same thing for ranges. What I 
started with is:

import std.range : iota;
alias MrSlice = typeof(iota(M.init));

so far, so good. The MrSlice-thing is the analogy for Mr, as it 
can be empty, as Mr can.
What I also need is the analogy for the M-thing. But how can I 
define a slice, which is not-empty by definition?

A workaround, which works is for example, using in a function 
which assumes a non-empty slice a contract:
void fun3(MrSlice val)
in
{
     assert(!val.empty);
}
body
{
     // do some funny stuff, part 3 :)
}

I tried also something using Algebraic types with an underlying 
int, but couldn't manage to append elements to something like:
alias List2 = Algebraic!(This[]);
or
alias List(Leaf) = Algebraic!(Leaf, This*);

The cool thing is, if the algebraic stuff works, it would 
potentially be able to replace all four aliases altogether...
But a knock-out criteria is: the solution has to be @nogc


More information about the Digitalmars-d-learn mailing list