www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - With statement extension

reply deed <none none.none> writes:
Would it be possible to extend current with statement's 
expressiveness by two lowerings:

1) Alias expression/symbol and replace in macro fashion:
    with (a : exprA) { /* use a. will be replaced by exprA by 
compiler. */ }
2) Accept a list of arguments: with (a, b : expr , ...) {} gets 
lowered to
    with (a) with (b : expr) ... {}

As a result, the following would be allowed:

with (aSymb, anExpr, aTempInst, a: symbA, b: exprB, c: tempInstC)
{
     // - current rules for aSymb, anExpr, aTempInst.
     // - a, b and c are replaced by symbA, exprB and tempInstC, 
respectively.
}

For example:

foreach (ref el; elements) with (
     M   : el.aMatrix,
     Ex  : el.someProperties.someModulusX,
     Ey  : el.someProperties.someModulusY,
     G   : el.someProperties.anotherModulus,
     vxy : el.someProperties.aRatio,
     vyx : el.someProperties.anotherRatio)
{
     M = [[1/Ex,    -vyx/Ey,   0],
          [-vxy/Ex,    1/Ey,   0],
          [0,             0, 1/G]];
}

struct SomeProperties (T) {
     T someModulusX;
     T someModulusY;
     T anotherModulus;
     T aRatio;
     T anotherRatio;
     ...
}

struct Element (T) {
     T aMatrix;
     SomeProperties!T someProperties;
     ...
}

Element!float[] elements; elements.length = N;
Apr 26 2016
next sibling parent Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Tuesday, 26 April 2016 at 12:53:49 UTC, deed wrote:
 Would it be possible to extend current with statement's 
 expressiveness by two lowerings:

 1) Alias expression/symbol and replace in macro fashion:
    with (a : exprA) { /* use a. will be replaced by exprA by 
 compiler. */ }
 2) Accept a list of arguments: with (a, b : expr , ...) {} gets 
 lowered to
    with (a) with (b : expr) ... {}
This looks useful, and should be easy to implement. +1 from me.
Apr 27 2016
prev sibling parent reply deed <none none.none> writes:
On Tuesday, 26 April 2016 at 12:53:49 UTC, deed wrote:
 For example:
A stronger example would be a scope with two similar types, i.e.: void foo (Matrix matrix, SameType e1, SameType e2) { with ( M : matrix.rawArr; Ex1 : e1.someProperties.someModulusX, Ey1 : e1.someProperties.someModulusY, Ex2 : e2.someProperties.someModulusX, Ey2 : e2.someProperties.someModulusY, v : e1.someProperties.aRatio) foreach (i; ...) foreach (j; ...) { M[...] = Ex1 > Ex2 ? 1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 - Ey2)^^2) : 1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 + Ey2)^^2) ; } } Current with-statement gets you next to nowhere here: void foo (Matrix matrix, SameType e1, SameType e2) { with (matrix) with (e1.someProperties) foreach (i; ...) foreach (j; ...) { rawArr[...] = e1.someProperties.someModulusX > e2.someProperties.someModulusX ? 1/aRatio^^2 * sqrt( aRatio * (e1.someProperties.someModulusX + e2.someProperties.someModulusX)^^2 + aRatio^^2 * (e1.someProperties.someModulusY - e2.someProperties.someModulusY)^^2) : 1/aRatio^^2 * sqrt( aRatio * (e1.someProperties.someModulusX + e2.someProperties.someModulusX)^^2 + aRatio^^2 * (e1.someProperties.someModulusY) + e2.someProperties.someModulusY)^^2) ; } } And let's say it would be much more descriptive to name e1 and e2 as currentSquare and adjacentTriangle and there you go.. Apparently, the two proposed lowerings could boost the expressiveness of the language without breakage.
Apr 27 2016
parent reply Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 04/27/16 15:55, deed via Digitalmars-d wrote:
 
 A stronger example would be a scope with two similar types, i.e.:
 
 void foo (Matrix matrix, SameType e1, SameType e2)
 {
     with (
     M    : matrix.rawArr;
     Ex1  : e1.someProperties.someModulusX,
     Ey1  : e1.someProperties.someModulusY,
     Ex2  : e2.someProperties.someModulusX,
     Ey2  : e2.someProperties.someModulusY,
     v    : e1.someProperties.aRatio)
 
     foreach (i; ...) foreach (j; ...) {
         M[...] = Ex1 > Ex2 ?
         1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 - Ey2)^^2) :
         1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 + Ey2)^^2) ;
     }
 }
 
 Current with-statement gets you next to nowhere here:
[...] void foo (Matrix matrix, SameType e1, SameType e2) { ref M() { return matrix.rawArr; } ref Ex1() { return e1.someProperties.someModulusX; } ref Ey1() { return e1.someProperties.someModulusY; } ref Ex2() { return e2.someProperties.someModulusX; } ref Ey2() { return e2.someProperties.someModulusY; } ref v() { return e1.someProperties.aRatio; } foreach (i; ...) foreach (j; ...) { M[...] = Ex1 > Ex2 ? 1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 - Ey2)^^2) : 1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 + Ey2)^^2) ; } } artur
Apr 27 2016
next sibling parent reply Adrian Matoga <dlang.spam matoga.info> writes:
On Wednesday, 27 April 2016 at 18:34:18 UTC, Artur Skawina wrote:
 [...]

    void foo (Matrix matrix, SameType e1, SameType e2)
    {
        ref M()   { return matrix.rawArr; }
        ref Ex1() { return e1.someProperties.someModulusX; }
        ref Ey1() { return e1.someProperties.someModulusY; }
        ref Ex2() { return e2.someProperties.someModulusX; }
        ref Ey2() { return e2.someProperties.someModulusY; }
        ref v()   { return e1.someProperties.aRatio; }

        foreach (i; ...) foreach (j; ...) {
            M[...] = Ex1 > Ex2 ?
            1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 - 
 Ey2)^^2) :
            1/v^^2 * sqrt(v * (Ex1 + Ex2)^^2 + v^^2 * (Ey1 + 
 Ey2)^^2) ;
        }
    }

 artur
Unless any of the properties is an enum or, well, a property, and I'd expect both in such case.
Apr 28 2016
parent reply Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 04/28/16 10:28, Adrian Matoga via Digitalmars-d wrote:
 On Wednesday, 27 April 2016 at 18:34:18 UTC, Artur Skawina wrote:
 [...]

        ref Ex1() { return e1.someProperties.someModulusX; }
Unless any of the properties is an enum or, well, a property, and I'd expect both in such case.
s/ref/ property auto ref/, but it either won't matter in practice ( property) or be easily fixable (non-lvals). The main point was that what OP is asking for is yet another function definition syntax. D has quite a few already, and more are regularly requested (like overloading `alias`). Adding a special case just for with-expressions wouldn't be a good idea. artur
Apr 28 2016
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 28 April 2016 at 11:10:21 UTC, Artur Skawina wrote:
 On 04/28/16 10:28, Adrian Matoga via Digitalmars-d wrote:
 On Wednesday, 27 April 2016 at 18:34:18 UTC, Artur Skawina 
 wrote:
 [...]

        ref Ex1() { return e1.someProperties.someModulusX; }
Unless any of the properties is an enum or, well, a property, and I'd expect both in such case.
s/ref/ property auto ref/, but it either won't matter in practice ( property) or be easily fixable (non-lvals). The main point was that what OP is asking for is yet another function definition syntax. D has quite a few already, and more are regularly requested (like overloading `alias`). Adding a special case just for with-expressions wouldn't be a good idea. artur
If it's just about syntax sugar, I definitely would prefer to be able to use alias over ref, e.g. alias v = el.someProperties.aRatio;
Apr 28 2016
parent Meta <jared771 gmail.com> writes:
On Thursday, 28 April 2016 at 11:39:22 UTC, Seb wrote:
 If it's just about syntax sugar, I definitely would prefer to 
 be able to use alias over ref, e.g.

 alias v = el.someProperties.aRatio;
The main issue with this currently is that you can only alias symbols, but el.someProperties.aRatio is an expression, not a symbol.
Apr 28 2016
prev sibling parent deed <none none.none> writes:
...
On Wednesday, 27 April 2016 at 18:34:18 UTC, Artur Skawina wrote:
 ...
        ref M()   { return matrix.rawArr; }
        ref Ex1() { return e1.someProperties.someModulusX; }
 ...
If the generated code is equal, without relying on too much optimization, I agree this is close enough, although uglier. If so, it raises the question of why having the with-statement in the language in the first place? However, I doubt you will have the same control in an equally convenient way.
Apr 29 2016