Problems using 'with' statement

Adam S adamschwalm at gmail.com
Sat Jan 4 13:11:31 PST 2014


On Saturday, 4 January 2014 at 20:56:41 UTC, Adam S wrote:
>
> I've encountered an issue using the 'with' statement. Can 
> anyone explain why the following requires that I explicitly 
> specify the variable name, even though it is within a 'with'?
>
> class Foo {
>     auto test(alias val)() if (is(typeof(val) == int)){}
>     auto test(alias val)() if (!is(typeof(val) == int)){}
> }
>
> void main() {
>
>     auto bar = new Foo;
>
>     with (bar) {
>         //test!2();        //fails to compile
>         //test!"cats"();   //fails to compile
>         bar.test!2();      //works
>         bar.test!"cats"(); //works
>     }
> }
>
> The commented out lines fail with:
>     Error: need 'this' for 'test' of type 'pure nothrow @safe 
> void()'
>
> Thanks

Most template member funs seems to work fine. The issue only 
seems to appear when the member function is overloaded. For 
example

class Foo {
     auto test(alias val)() if (is(typeof(val) == int)){}
     //auto test(alias val)() if (!is(typeof(val) == int)){}

}

void main() {
     auto bar = new Foo;

     with (bar) {
         test!2();      //works
     }
}

works fine until the overload of test is uncommented. Then the 
same error occurs.


More information about the Digitalmars-d-learn mailing list