what is the mean that call function start with a dot.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Sep 22 06:48:37 UTC 2019


On Saturday, September 21, 2019 10:32:08 PM MDT Paul Backus via Digitalmars-
d-learn wrote:
> On Sunday, 22 September 2019 at 04:15:53 UTC, lili wrote:
> > Hi:
> >
> >    yesterday I saw some d code, where is an .fn() call syntax,
> >
> > what is it mean.
>
> It means that `fn` is looked up in the module's top-level scope.
>
> Source: https://dlang.org/spec/expression.html#identifier

which includes any top-level imports, so the symbol in question isn't
necessarily within the module. The key thing is that when a symbol is
preceded by a dot, no local or member symbols are taken into account. It
makes it so that if you have a local or member symbol which has the same
name as a top-level symbol, you're able to directly reference the top-level
symbol without providing the entire module path.

So, for instance, if a socket class had a close method, that close method
could call the C function, close, with .close, whereas otherwise, it would
have to do something like core.sys.posix.unistd.close (at least on POSIX
systems) to call the C function, since if it called close without any kind
of path, it would end up recursively calling the close member function,
because the module-level function is shadowed by the member function.

- Jonathan M Davis






More information about the Digitalmars-d-learn mailing list