digitalmars.D.learn - Is it possible to disallow import for certain functions?
- BoQsc (7/19) Jul 27 2019 I would like to make sure that function in module that I have
- Mike Parker (14/34) Jul 27 2019 This has nothing to do with main being imported. It's because you
- Mike Parker (4/8) Jul 27 2019 eh...
- BoQsc (4/33) Jul 27 2019 mainFile.d
- evilrat (5/15) Jul 27 2019 Of course. When you import that module "version = ..." is likely
- Jonathan M Davis (14/34) Jul 27 2019 You can't declare a version identifier within a module and have them aff...
- XavierAP (5/7) Jul 27 2019 In general, make the function private.
I would like to make sure that function in module that I have won't be imported, is this possible to achieve? Test subject: mainFile.dimport otherFile; void main(){ }otherFile.dimport std.stdio : writeln; import std.file : mkdir; int main(){ writeln("test "); return 0; }Error, main() method has been imported: how to disallow main() method from being imported?otherFile.d(5): Error: only one main, WinMain, or DllMain allowed. Previously found main at mainFile.d(3)
Jul 27 2019
On Saturday, 27 July 2019 at 11:54:09 UTC, BoQsc wrote:I would like to make sure that function in module that I have won't be imported, is this possible to achieve? Test subject: mainFile.dThis has nothing to do with main being imported. It's because you *compiled* two main functions. If you absolutely want two main functions and can't see a better way to achieve whatever it is you're doing, then you can wrap them in version blocks: module mainFile; version(defaultMain) { ... } ========= module otherFile; version otherMain() { ... }import otherFile; void main(){ }otherFile.dimport std.stdio : writeln; import std.file : mkdir; int main(){ writeln("test "); return 0; }Error, main() method has been imported: how to disallow main() method from being imported?otherFile.d(5): Error: only one main, WinMain, or DllMain allowed. Previously found main at mainFile.d(3)
Jul 27 2019
On Saturday, 27 July 2019 at 12:05:27 UTC, Mike Parker wrote:module otherFile; version otherMain() { ... }eh... version(otherMain) { }
Jul 27 2019
On Saturday, 27 July 2019 at 12:06:11 UTC, Mike Parker wrote:On Saturday, 27 July 2019 at 12:05:27 UTC, Mike Parker wrote:I seem to be doing something wrong, the result is the same.module otherFile; version otherMain() { ... }eh... version(otherMain) { }otherFile.d(8): Error: only one main, WinMain, or DllMain allowed. Previously found main at mainFile.d(11)mainFile.dmodule mainFile; import otherFile; import std.stdio : writeln; private version = defaultMain; version(defaultMain) { void main(){ writeln("test"); } }otherFile.dmodule otherFile; import std.stdio : writeln; private version = otherMain; version(otherMain) { void main(){ writeln("Interesting"); } }
Jul 27 2019
On Saturday, 27 July 2019 at 12:48:12 UTC, BoQsc wrote:I seem to be doing something wrong, the result is the same.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.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"); } }
Jul 27 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: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 DavisI seem to be doing something wrong, the result is the same.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.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"); } }
Jul 27 2019
On Saturday, 27 July 2019 at 11:54:09 UTC, BoQsc wrote:I would like to make sure that function in module that I have won't be imported, is this possible to achieve?In general, make the function private. But indeed, on your case, it is a terrible idea to define a main() function in a module that you plan to import. Move it out into its own main module.
Jul 27 2019