Jump to page: 1 2 3
Thread overview
Static foreach bug?
Sep 02, 2018
bauss
Sep 02, 2018
bauss
Sep 02, 2018
bauss
Sep 04, 2018
Timon Gehr
Sep 05, 2018
Dechcaudron
Sep 05, 2018
Jonathan M Davis
Sep 05, 2018
Dechcaudron
Sep 05, 2018
Jonathan M Davis
Sep 05, 2018
rikki cattermole
Sep 05, 2018
Andre Pany
Sep 05, 2018
JN
Sep 05, 2018
rikki cattermole
Sep 05, 2018
Timon Gehr
Sep 06, 2018
Dechcaudron
Sep 06, 2018
Jonathan M Davis
Sep 07, 2018
Dechcaudron
Sep 05, 2018
Timon Gehr
Sep 02, 2018
Basile B.
Sep 02, 2018
Jonathan M Davis
Sep 02, 2018
bauss
Sep 02, 2018
Neia Neutuladh
Sep 03, 2018
bauss
Sep 03, 2018
Neia Neutuladh
Sep 03, 2018
bauss
Sep 03, 2018
Jonathan M Davis
Sep 03, 2018
Soma
Sep 03, 2018
Meta
September 02, 2018
Is there a reason why you cannot create a separate scope within a static foreach?

The below will not compile:

```
enum a = ["a" : "a", "b" : "b", "c" : "c"];

static foreach (k,v; a)
{
    {
        enum b = k;
        enum c = v;
    }
}
```

It works if it's in a function of course.

This creates a big limitation when you're trying to ex. loop through members of inherited classes, interfaces etc. and then want to store the information in variables, because you cannot do it. Which means to work around it you have to do it all in an ugly one-liner.

Is it intended behavior? If so, why? If not is there a workaround until it can be fixed?

September 02, 2018
On Sunday, 2 September 2018 at 13:21:05 UTC, bauss wrote:
> Is there a reason why you cannot create a separate scope within a static foreach?
>

You can try it out here: https://run.dlang.io/is/7DgwCk


September 02, 2018
On Sunday, 2 September 2018 at 13:21:05 UTC, bauss wrote:
> Is there a reason why you cannot create a separate scope within a static foreach?
>
> The below will not compile:
>
> ```
> enum a = ["a" : "a", "b" : "b", "c" : "c"];
>
> static foreach (k,v; a)
> {
>     {
>         enum b = k;
>         enum c = v;
>     }
> }
> ```
>
> It works if it's in a function of course.
>
> This creates a big limitation when you're trying to ex. loop through members of inherited classes, interfaces etc. and then want to store the information in variables, because you cannot do it. Which means to work around it you have to do it all in an ugly one-liner.
>
> Is it intended behavior? If so, why? If not is there a workaround until it can be fixed?

It's intended, but with the possibility to add special syntax for local declarations in the future left open, as per:
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#local-declarations
September 02, 2018
On Sunday, 2 September 2018 at 13:26:55 UTC, Petar Kirov [ZombineDev] wrote:
> It's intended, but with the possibility to add special syntax for local declarations in the future left open, as per:
> https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1010.md#local-declarations

Is there any plans to implement it soon or is this going to be another half done feature?
September 02, 2018
On Sunday, 2 September 2018 at 13:21:05 UTC, bauss wrote:
> Is there a reason why you cannot create a separate scope within a static foreach?
>
> The below will not compile:
>
> ```
> enum a = ["a" : "a", "b" : "b", "c" : "c"];
>
> static foreach (k,v; a)
> {
>     {
>         enum b = k;
>         enum c = v;
>     }
> }
> ```
>
> It works if it's in a function of course.
> [...]

At the global scope it cant work since there even block statements are not possible, just this

```
{
    enum b = 0;
    enum c = 0;
}
```

doesn't compile; and this is what you tried to add in the `static foreach` body. So if i understand correctly this is even not a `static foreach` issue here.
September 02, 2018
On Sunday, September 2, 2018 7:21:05 AM MDT bauss via Digitalmars-d wrote:
> Is there a reason why you cannot create a separate scope within a static foreach?
>
> The below will not compile:
>
> ```
> enum a = ["a" : "a", "b" : "b", "c" : "c"];
>
> static foreach (k,v; a)
> {
>      {
>          enum b = k;
>          enum c = v;
>      }
> }
> ```
>
> It works if it's in a function of course.
>
> This creates a big limitation when you're trying to ex. loop through members of inherited classes, interfaces etc. and then want to store the information in variables, because you cannot do it. Which means to work around it you have to do it all in an ugly one-liner.
>
> Is it intended behavior? If so, why? If not is there a workaround until it can be fixed?

If you're not inside a function, why would it even be useful to be able to do that? That would be the equivalent of

{
    enum b = "a";
    enum c = "a";
}
{
    enum b = "b";
    enum c = "b";
}
{
    enum b = "c";
    enum c = "c";
}

Creating scopes like that isn't legal outside of a function (hence the compiler error), but even if it were, what good would it do you? There's no way to refer to those scopes. The only way that such declarations would make sense is if they're inside scopes with their own names so that they can be referred to via those names (e.g. inside a struct or template) or if they aren't in separate scopes and have unique names (which would mean giving them names other than b and c rather than trying to scope them). I fail to see why what you're complaining about here would even be useful.

Now, the fact that it's a bit of a pain to give each of those enums a unique name can certainly be annoying, and that's a problem with static foreach in general, but I don't understand why creating an extra scope like you're trying to do here would be at all useful outside of a function. What are you really trying to do here?

- Jonathan M Davis



September 02, 2018
On Sunday, 2 September 2018 at 18:07:10 UTC, Jonathan M Davis wrote:
> On Sunday, September 2, 2018 7:21:05 AM MDT bauss via Digitalmars-d wrote:
>> Is there a reason why you cannot create a separate scope within a static foreach?
>>
>> The below will not compile:
>>
>> ```
>> enum a = ["a" : "a", "b" : "b", "c" : "c"];
>>
>> static foreach (k,v; a)
>> {
>>      {
>>          enum b = k;
>>          enum c = v;
>>      }
>> }
>> ```
>>
>> It works if it's in a function of course.
>>
>> This creates a big limitation when you're trying to ex. loop through members of inherited classes, interfaces etc. and then want to store the information in variables, because you cannot do it. Which means to work around it you have to do it all in an ugly one-liner.
>>
>> Is it intended behavior? If so, why? If not is there a workaround until it can be fixed?
>
> If you're not inside a function, why would it even be useful to be able to do that? That would be the equivalent of
>
> {
>     enum b = "a";
>     enum c = "a";
> }
> {
>     enum b = "b";
>     enum c = "b";
> }
> {
>     enum b = "c";
>     enum c = "c";
> }
>
> Creating scopes like that isn't legal outside of a function (hence the compiler error), but even if it were, what good would it do you? There's no way to refer to those scopes. The only way that such declarations would make sense is if they're inside scopes with their own names so that they can be referred to via those names (e.g. inside a struct or template) or if they aren't in separate scopes and have unique names (which would mean giving them names other than b and c rather than trying to scope them). I fail to see why what you're complaining about here would even be useful.
>
> Now, the fact that it's a bit of a pain to give each of those enums a unique name can certainly be annoying, and that's a problem with static foreach in general, but I don't understand why creating an extra scope like you're trying to do here would be at all useful outside of a function. What are you really trying to do here?
>
> - Jonathan M Davis

Let me demonstrate why it's useful with just this unmaintainable piece of code:

```
final class ClassName : SoapBinding, Interface
{
  public:
  final:
  this()
  {
    super();
  }
   import __stdtraits = std.traits;
   static foreach (member; __traits(derivedMembers, Interface))
  {
    mixin
    (
      mixin("(__stdtraits.ReturnType!" ~ member ~ ").stringof") ~
      " " ~
      member ~
      "(" ~
        mixin("parameters!" ~ member) ~
      ") { /* Do stuff ... */ }"
    );
  }
}
```

Woud be so much more maintainable if I could have each statement into a variable that could be maintained properly.
September 02, 2018
On Sunday, 2 September 2018 at 19:42:20 UTC, bauss wrote:
> Woud be so much more maintainable if I could have each statement into a variable that could be maintained properly.

You could extract the body of the static foreach into a [template] function.
September 03, 2018
On Sunday, 2 September 2018 at 20:01:08 UTC, Neia Neutuladh wrote:
> On Sunday, 2 September 2018 at 19:42:20 UTC, bauss wrote:
>> Woud be so much more maintainable if I could have each statement into a variable that could be maintained properly.
>
> You could extract the body of the static foreach into a [template] function.

I'm aware of that, but it's an unnecessary work around for something as trivial as the alternative would have been.
September 03, 2018
On Monday, 3 September 2018 at 04:43:30 UTC, bauss wrote:
> On Sunday, 2 September 2018 at 20:01:08 UTC, Neia Neutuladh wrote:
>> On Sunday, 2 September 2018 at 19:42:20 UTC, bauss wrote:
>>> Woud be so much more maintainable if I could have each statement into a variable that could be maintained properly.
>>
>> You could extract the body of the static foreach into a [template] function.
>
> I'm aware of that, but it's an unnecessary work around for something as trivial as the alternative would have been.

You would need to mark symbols as scoped to the static foreach body, or else as exported from a scope to an outer scope. So it's not exactly trivial.
« First   ‹ Prev
1 2 3