std.functional.compose compilation error

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 25 08:04:43 PDT 2016


On Thursday, August 25, 2016 14:30:00 Meta via Digitalmars-d-learn wrote:
> On Thursday, 25 August 2016 at 14:06:32 UTC, Antonio Corbi wrote:
> > Hello,
> >
> > Trying to compile this example from Chuck Allison:
> > -------------------------------------------
> > import std.stdio;
> > import std.functional;
> >
> > void main() {
> >
> >     auto div3 = (double x) => x/3.0;
> >     auto sq = (double x) => x*x;
> >     auto pls1 = (double x) => x+1.0;
> >     alias compose!(div3,sq,pls1) comp;
> >     writeln(comp(2.0)); // 3 == (2.0+1.0)^^2 / 3.0
> >     alias pipe!(div3,sq,pls1) pip;
> >     writeln(pip(2.0));  // 1.44444 == (2.0/3.0)^^2 + 1.0
> >
> > }
> > --------------------------------------------
> >
> > I get this error (with DMD64 D Compiler v2.071.1 in linux):
> >
> > compose.d(8): Error: template instance compose!(div3, sq, pls1)
> > compose is not a template declaration, it is a module
> >
> > But the error disappears if I use this import:
> >    import std.functional:compose,pipe;
> >
> > Is this a bug or is it the expected behaviour under the recent
> > 'import' changes?
> > Thanks!
>
> Try renaming your source file to something other than compose.d,
> I think that's confusing the compiler.

Yes. Because the module is compose, within that file, compose will refer to
the module, not anything you import. The selective import essentially
creates a local alias like

alias compose = std.functional.compose;

as part of the import, so then within that scope, compose refers to that
alias and not to the module. You'll run into the same problem any time that
you give a module the same name as a symbol that you're importing.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list