std.algorithm.countUntil and alias
Anton Pastukhov
caffeine9999 at mailbox.org
Wed Oct 23 19:46:57 UTC 2024
On Wednesday, 23 October 2024 at 19:10:05 UTC, Jonathan M Davis
wrote:
> On Wednesday, October 23, 2024 11:18:47 AM MDT Anton Pastukhov
> via Digitalmars-d-learn wrote:
>> On Wednesday, 23 October 2024 at 14:50:44 UTC, Paul Backus
>> wrote:
>> > On Wednesday, 23 October 2024 at 12:46:24 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.
>
> It aliases the symbol, but a member function does you no good
> without an object to go with it. For the alias of a member
> function to actually be properly callable, it would need more
> than just the symbol. It would actually need to reference the
> object itself in some fashion, and that's simply not how
> aliases work. All they really do is provide an alternate name
> for a symbol.
>
> Really, it should probably just be illegal to alias a member
> function from a variable rather than the type, because the fact
> that it's allowed and then treated like it's an alias from the
> type causes confusion like you're experiencing. However, it's
> likely allowed for similar reasons to why it's legal to call
> static member functions on an instance rather than requiring
> that they be called on the type (which I'm inclined to argue
> was a bad design decision, but it's what we have).
>
> - Jonathan M Davis
Thanks for the clarification. I understand that aliasing a
delegate would be problematic because it's context-aware. In my
case, however, I'm aliasing not a delegate and not even a free
function, I'm aliasing a struct field of enum type, which holds
no references to the context. In my book it should be perfectly
ok and inability to do so looks like a bug for me. Other
participants mentioned that there is some rationale behind that
design, so I'm just trying to understand it before heading to
Bugzilla
More information about the Digitalmars-d-learn
mailing list