std.algorithm.countUntil and alias
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Oct 23 19:10:05 UTC 2024
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
More information about the Digitalmars-d-learn
mailing list