Is it possible to disallow import for certain functions?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Jul 27 18:12:26 UTC 2019


On Saturday, July 27, 2019 7:12:32 AM MDT evilrat via Digitalmars-d-learn 
wrote:
> On Saturday, 27 July 2019 at 12:48:12 UTC, BoQsc wrote:
> > I seem to be doing something wrong, the result is the same.
> >
> >> otherFile.d(8): Error: only one main, WinMain, or DllMain
> >> allowed. Previously found main at mainFile.d(11)
> >>
> >>private version = otherMain;
> >>version(otherMain) {
> >>
> >>    void main(){
> >>
> >>        writeln("Interesting");
> >>
> >>    }
> >>
> >>}
>
> Of course. When you import that module "version = ..." is likely
> still evaluated and it enables that second main. Either move it
> to yet another module(and that's just masks the problem) or use
> command-line parameter to toggle which one to use.

You can't declare a version identifier within a module and have them affect
other modules. Only built-in version identifiers and those passed to the
compiler can affect multiple modules.

I don't know what the OP is really trying to do, but in general, it makes no
sense to have multiple mains. Pretty much the only time that I'd even
consider it would be with version(unittest) so that the normal main isn't
run when compiling and running unit tests. If code is going to be used
across multiple projects (and thus need multiple mains), I'd have the shared
code in a library, with the code including the different mains being
completely separate - probably even in completely separate repos, since
they'd be for different programs.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list