www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - surrounded type modifier

reply "Namespace" <rswhite4 googlemail.com> writes:
Code:
----
const { /// [1]
	int a = 3;
}

void main()
{
	const { /// [2]
		int b = 4;
	}
}
----

Why is [1] allowed, but not [2]?
Sep 18 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Namespace:

 Code:
 ----
 const { /// [1]
 	int a = 3;
 }

 void main()
 {
 	const { /// [2]
 		int b = 4;
 	}
 }
 ----

 Why is [1] allowed, but not [2]?
Think about what this does: void main() { { int b = 4; } } It creates a new scope inside the function. How do you tell apart the syntax to create a new scope from having a "group tagging" as in the global case? Bye, bearophile
Sep 18 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 18 September 2013 at 13:42:37 UTC, bearophile wrote:
 Namespace:

 Code:
 ----
 const { /// [1]
 	int a = 3;
 }

 void main()
 {
 	const { /// [2]
 		int b = 4;
 	}
 }
 ----

 Why is [1] allowed, but not [2]?
Think about what this does: void main() { { int b = 4; } } It creates a new scope inside the function. How do you tell apart the syntax to create a new scope from having a "group tagging" as in the global case? Bye, bearophile
If a type modifier is in front of '{' it's a group tagging, otherwise a scope.
Sep 18 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
Same thing with debug:

{
     // scope code
}

debug {
     // debug code
}
Sep 18 2013
prev sibling parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Wednesday, 18 September 2013 at 13:23:10 UTC, Namespace wrote:
 Code:
 ----
 const { /// [1]
 	int a = 3;
 }

 void main()
 {
 	const { /// [2]
 		int b = 4;
 	}
 }
 ----

 Why is [1] allowed, but not [2]?
Citing grammar: FunctionBody: BlockStatement BodyStatement InStatement BodyStatement OutStatement BodyStatement InStatement OutStatement BodyStatement OutStatement InStatement BodyStatement BlockStatement: { } { StatementList } As you can see there is no room for attributes. Why dmd does not support attributes here is separate question - probably because such construct would be confused with lambda, but this is not a serious reason.
Sep 18 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 18 September 2013 at 14:17:04 UTC, Maxim Fomin 
wrote:
 On Wednesday, 18 September 2013 at 13:23:10 UTC, Namespace 
 wrote:
 Code:
 ----
 const { /// [1]
 	int a = 3;
 }

 void main()
 {
 	const { /// [2]
 		int b = 4;
 	}
 }
 ----

 Why is [1] allowed, but not [2]?
Citing grammar: FunctionBody: BlockStatement BodyStatement InStatement BodyStatement OutStatement BodyStatement InStatement OutStatement BodyStatement OutStatement InStatement BodyStatement BlockStatement: { } { StatementList } As you can see there is no room for attributes. Why dmd does not support attributes here is separate question - probably because such construct would be confused with lambda, but this is not a serious reason.
Should I open an enhancement report?
Sep 18 2013
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Wednesday, 18 September 2013 at 14:23:25 UTC, Namespace wrote:
 Should I open an enhancement report?
Of course you are always free to open enhancement reports.
Sep 18 2013