Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
September 18, 2013 surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Code: ---- const { /// [1] int a = 3; } void main() { const { /// [2] int b = 4; } } ---- Why is [1] allowed, but not [2]? |
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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
|
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Same thing with debug: { // scope code } debug { // debug code } |
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
|
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | 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?
|
September 18, 2013 Re: surrounded type modifier | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
|
Copyright © 1999-2021 by the D Language Foundation