Puzzled
Walter Bright
newshound2 at digitalmars.com
Wed Dec 13 06:52:59 UTC 2023
On 12/12/2023 11:02 AM, Don Allen wrote:
> Since ceasing my use of D out of concern for the direction and health of the
> project, I've nonetheless been following these forums and have seen what I
> consider to be sincere efforts to improve things.
Welcome back!
> So I've started doing a bit of D work again and ran into this:
>
> ````
> import std.stdio;
>
> struct Foo {
> int bar;
> int baz;
> }
>
> void bletch(alias field)(Foo s, int x) {
> s.field = x;
> writeln("%d", s.field);
> }
>
> int main(string[] args)
> {
> Foo s;
> bletch!bar(s); // undefined identifier `bar`
bar is looked up in this scope, meaning it is looked up in main(), and then
looked up in the module. It is not found, because bar is in the scope of Foo.
Hence the error message for this line
> bletch!baz(s);
> return 0;
> }
> ````
> But I'm failing to understand why the problem exists in the first place.
The misunderstanding appears to be the assumption that `field` is resolved
inside the definition of `bletch`. It is not, it is resolved at the point of
instantiation of the template `bletch`.
> Perhaps
> inherited from C++, which I do not know, having had some early exposure to it
> and concluded it was awful?
C++ has Argument Dependent Lookup (ADL) which follows different rules that I
have largely forgotten, despite having implemented them in my C++ compiler. ADL
was invented by Andrew Koenig to get around lookup problems with operator
overloading where `this` was on the right hand side of the operator. I never
liked ADL, it is confusing and a giant ugly kludge. I know of no other language
that has an equivalent.
More information about the Digitalmars-d
mailing list