www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - with (auto p = new ...)

reply "Andre" <andre s-e-a-p.de> writes:
Hi,

I just wonder why "with (auto p = new ...)" is not working.
It would be some syntax sugar in this scenario:

	with (auto p = new Panel())
	{
		parent = this;
		text = "bla";
		with (auto b = new Button())
		{
			parent = p; // Here p is needed
			text = "bla2";
		}
	}

source\app.d(8): Error: expression expected, not 'auto'
source\app.d(8): Error: found 'p' when expecting ')'
source\app.d(8): Error: found '=' instead of statement
...

Kind regards
André
Sep 23 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:
I presume with is limited to expressions and not statements as the messages says. However, you can use if (auto p = new Panel()) instead as long as the new doesn't fail, that is you still have memory left :)
Sep 23 2014
prev sibling next sibling parent reply "Graham Fawcett" <fawcett uwindsor.ca> writes:
How about:

     auto b - new Button(); with (b) {

On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 André
Sep 23 2014
parent reply "Graham Fawcett" <fawcett uwindsor.ca> writes:
Sorry, I sent that last message before I intended to.

How about:

      auto b = new Button(); with (b) {
        ...
      }

'b' is explicitly outside of the scope of the 'with' block, which 
may not be what you intended. But you could use more braces to 
add an extra level of scope if that's an issue:

     ...
     text = "blah";
     {
        auto b = new Button(); with (b) {
            ...
        }
     }
     // b is no longer in scope

Graham



 On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 André
Sep 23 2014
parent reply "andre" <andre s-e-a-p.de> writes:
Yes, that is also working. As far as I remember (only my tablet 
currently available) also this works:

Panel p;
with(p = new Panel ()) {}

Therefore it seems strange,the same does not work with auto.

Kind regards
André


On Tuesday, 23 September 2014 at 19:49:22 UTC, Graham Fawcett 
wrote:
 Sorry, I sent that last message before I intended to.

 How about:

      auto b = new Button(); with (b) {
        ...
      }

 'b' is explicitly outside of the scope of the 'with' block, 
 which may not be what you intended. But you could use more 
 braces to add an extra level of scope if that's an issue:

     ...
     text = "blah";
     {
        auto b = new Button(); with (b) {
            ...
        }
     }
     // b is no longer in scope

 Graham



 On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 André
Sep 23 2014
next sibling parent "Meta" <jared771 gmail.com> writes:
I think this is just a language oversight. It's allowed in if 
statements, and people have made a good case for allowing it for 
switch statements. It just hasn't been implemented. I made an 
attempt one evening to implement it for switch statements,  but 
I'm not at all familiar with DMD, so I put it on the back burner 
for the time being.
Sep 23 2014
prev sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Tuesday, 23 September 2014 at 20:16:29 UTC, andre wrote:
 Therefore it seems strange,the same does not work with auto.
Yes, it seem logical to me allow this also in with and switch.
Sep 23 2014
parent reply "Andre" <andre s-e-a-p.de> writes:
Enhancement 13526 filed:
https://issues.dlang.org/show_bug.cgi?id=13526
Sep 24 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 24 Sep 2014 14:39:25 +0000
Andre via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Enhancement 13526 filed:
 https://issues.dlang.org/show_bug.cgi?id=3D13526
i wrote a quickhack-patch for this ER. as it's my first patch that goes outside parser it needs to be reviewed by someone who knows compiler internals. ah, and we need grammar fixes for it too, but it's completely out of my scope now.
Sep 24 2014
prev sibling next sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
aOn Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 see more example
http://techgurulab.com/course/java-quiz-online/ see more example http://techgurulab.com/course/java-quiz-online/e example
Sep 23 2014
prev sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 André
have you tried this? --- import std.stdio; struct myStruct{ int c=299792458; } void main(){ with(new myStruct()){ writeln(c); } } ---
Sep 26 2014
parent "Freddy" <Hexagonalstar64 gmail.com> writes:
On Friday, 26 September 2014 at 19:59:56 UTC, Freddy wrote:
 On Tuesday, 23 September 2014 at 15:19:59 UTC, Andre wrote:
 Hi,

 I just wonder why "with (auto p = new ...)" is not working.
 It would be some syntax sugar in this scenario:

 	with (auto p = new Panel())
 	{
 		parent = this;
 		text = "bla";
 		with (auto b = new Button())
 		{
 			parent = p; // Here p is needed
 			text = "bla2";
 		}
 	}

 source\app.d(8): Error: expression expected, not 'auto'
 source\app.d(8): Error: found 'p' when expecting ')'
 source\app.d(8): Error: found '=' instead of statement
 ...

 Kind regards
 André
have you tried this? --- import std.stdio; struct myStruct{ int c=299792458; } void main(){ with(new myStruct()){ writeln(c); } } ---
opps, sorry I misread the quesion.
Sep 26 2014