www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Common scope for function's in{}, out(result){} and body{}

reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
Hello!

There are local imports available in D.
But if contract programming is necessary there are no common 
function scope.

So I suppose D need common scope:

-----------------------------------
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
}
out(result) {
  // can use std.range, MaxLength, A, isOdd
}
body {
  // can use std.range, MaxLength, A, isOdd
}
-------------------------------


What do you think?

Best Regards,
Ilya
Sep 11 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Ilya Yaroshenko:

     immutable length = r1.length; //Error, can't use function 
 parameters.
Time ago it was argued that if the use of function parameters is allowed there (to define constants), you have poor's man pre-state ("old") in D. Bye, bearophile
Sep 12 2014
parent reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
bearophile, has common scope been already proposed?

I don't sure that I clearly understand you (my English is really 
poor),
I have assumed that code "immutable length = r1.length; //Error" 
shouldn't be compiled.

On Friday, 12 September 2014 at 07:32:11 UTC, bearophile wrote:
 Ilya Yaroshenko:

    immutable length = r1.length; //Error, can't use function 
 parameters.
Time ago it was argued that if the use of function parameters is allowed there (to define constants), you have poor's man pre-state ("old") in D. Bye, bearophile
Sep 12 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ilya Yaroshenko:

 has common scope been already proposed?
Something related to it was proposed as possible way to implement the pre-state in the contract programming of D, it was the idea of having a common scope for in{} and out{} (but nothing has came out of the idea yet). Bye, bearophile
Sep 12 2014
prev sibling next sibling parent Nick Treleaven <ntrel-public yahoo.co.uk> writes:
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.
Sep 12 2014
prev sibling parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Fri, Sep 12, 2014 at 06:41:29AM +0000, Ilya Yaroshenko via Digitalmars-d
wrote:
 Hello!
 
 There are local imports available in D.
 But if contract programming is necessary there are no common function
 scope.
 
 So I suppose D need common scope:
 
 -----------------------------------
 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
 }
 out(result) {
  // can use std.range, MaxLength, A, isOdd
 }
 body {
  // can use std.range, MaxLength, A, isOdd
 }
 -------------------------------
 
 
 What do you think?
[...] Actually, there *is* already a function-wide scope that includes contracts and signature constraints. Remember that when you write: auto functionName(A...)(A args) { ... } it's actually a shorthand for: template functionName(A...) { auto functionName(A args) { ... } } This is known as an "eponymous template", which is a nice syntactic shorthand where if the template member has the same name as the template, then you don't have to write: functionName!(...).functionName(...) but it can be collapsed into just: functionName!(...)(...) So, to achieve what you want, you'd simply write: template functionName(Range1, Range2) { import std.range; enum MaxLength = 1024; immutable A = [0.123, 0.456, 0.789]; bool isOdd(uint i) { return i%2; } static assert(isInputRange!Range1, ...); auto functionName(Range1 r1, Range2 r2) in { assert(!r1.empty); // can use std.range, MaxLength, A, isOdd } out(result) { // can use std.range, MaxLength, A, isOdd } body { // can use std.range, MaxLength, A, isOdd } } T -- That's not a bug; that's a feature!
Sep 12 2014
parent "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
I really forget about template =)
Thank You!
Sep 12 2014