std.algorithm.countUntil and alias

Paul Backus snarwin at gmail.com
Wed Oct 23 23:01:16 UTC 2024


On Wednesday, 23 October 2024 at 17:18:47 UTC, Anton Pastukhov 
wrote:
> On Wednesday, 23 October 2024 at 14:50:44 UTC, Paul Backus 
> wrote:
>
>
>> You can't use an `alias` to refer to a member variable like 
>> this. When you write
>>
>>     alias myAlias = myStruct.test;
>>
>> ...it is silently rewritten by the compiler to
>>
>>     alias myAlias = MyStruct.test;
>>
>> So, in reality, there is no difference between the two 
>> versions.
>
> Is it intended behavior? Is it documented somewhere? I'm 
> looking here https://dlang.org/spec/declaration.html#alias and 
> it states: "An AliasDeclaration creates a symbol name that 
> refers to a type or another symbol". `myStruct.test` is a 
> symbol.

Yes, it's intended.

A "symbol" in D means a named entity declared in the program's 
source code. While every instance of `MyStruct` has its own copy 
of the `test` member variable, there is only one *declaration* of 
`MyStruct.test`--and thus, only one symbol.

In this case, instead of an `alias`, you can use a local function:

```d
MyStruct myStruct;
auto getTest() { return myStruct.test; }
auto idx = countUntil!(e => e == getTest())(...);
```


More information about the Digitalmars-d-learn mailing list