Common scope for function's in{}, out(result){} and body{}
Nick Treleaven via Digitalmars-d
digitalmars-d at puremagic.com
Fri Sep 12 05:06:42 PDT 2014
On 12/09/2014 07:41, Ilya Yaroshenko wrote:
> auto functionName(Range1, Range2)(Range1 r1, Ranger2)
> scope {
> import std.range; //imports are allowed
> enum MaxLength = 1024; //enums are allowed
> immutable A = [0.123, 0.456, 0.789]; // immutable and const are
> allowed
> bool isOdd (uint i) {return i%2;}
>
> static assert(isInputRange!Range1,
> Range1.stringof!~" is not InputRange");
>
> int i; //Error, mutable data disallowed.
> immutable length = r1.length; //Error, can't use function parameters.
> }
> in {
> assert(!r1.empty); // we already know that r1 is InputRange and
> function have local imports!
>
> // can use std.range, MaxLength, A, isOdd
> }
I like this idea, I've wanted something like it to share an expression
used in a template constraint with the function body - I suggest that
constraints can use symbols defined in the 'scope' block too. As well as
making template and contract code more DRY, it may also allow us to
further reduce global imports where they are only used in template
constraints.
More information about the Digitalmars-d
mailing list