www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to import & export modules

reply Vinod K Chandran <kcvinu82 gmail.com> writes:
Hi all,
I am practicing D by writting a win API gui wrapper. I want to 
use a single module import to use this Gui lib. Say i have 10 
modules like--
"App.d, Form.d, Button.d, Label.d, TextBox.d, ComboBox.d, 
ListBox.d, CheckBox.d, Panel.d, DateTimePicker.d"
In Nim, i can import and export all these modules in a separate 
single module and then i only need to use that module. Say i have 
a special module named "GuiLib.nim" And inside that  if i write 
like this--
import
App
Form
Button
....

export
import
App
Form
Button
....

Then in my main file, i only need to import the "GuiLib". How is 
this possible in D ?
Nov 10 2019
parent reply userTY <userTYbutno nowhere.fi> writes:
On Sunday, 10 November 2019 at 23:53:22 UTC, Vinod K Chandran 
wrote:
 Hi all,
 I am practicing D by writting a win API gui wrapper. I want to 
 use a single module import to use this Gui lib. Say i have 10 
 modules like--
 "App.d, Form.d, Button.d, Label.d, TextBox.d, ComboBox.d, 
 ListBox.d, CheckBox.d, Panel.d, DateTimePicker.d"
 In Nim, i can import and export all these modules in a separate 
 single module and then i only need to use that module. Say i 
 have a special module named "GuiLib.nim" And inside that  if i 
 write like this--
 import
 App
 Form
 Button
 ....

 export
 import
 App
 Form
 Button
 ....

 Then in my main file, i only need to import the "GuiLib". How 
 is this possible in D ?
You must use a module that has public imports. Public imports are visible from the module that contain them but most importantly from the module that imports the module containing the public imports. --- module all; public import App, Form, Button; --- --- module app; import all; // can see App, Form and Button exported (public) symbols --- See the specifications [1] for more comprehenssive details. [1]: https://dlang.org/spec/module.html#public_imports
Nov 10 2019
next sibling parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Monday, 11 November 2019 at 01:28:54 UTC, userTY wrote:
 On Sunday, 10 November 2019 at 23:53:22 UTC, Vinod K Chandran 
 wrote:
 [...]
You must use a module that has public imports. Public imports are visible from the module that contain them but most importantly from the module that imports the module containing the public imports. --- module all; public import App, Form, Button; --- --- module app; import all; // can see App, Form and Button exported (public) symbols --- See the specifications [1] for more comprehenssive details. [1]: https://dlang.org/spec/module.html#public_imports
Just a nitpick, prefer to use D style: https://dlang.org/dstyle.html Modules are essentially files. So keeping them lower case makes it easier.
Nov 10 2019
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On Monday, 11 November 2019 at 01:28:54 UTC, userTY wrote:

 import all; // can see App, Form and Button exported (public) 
 symbols
 ---
The approach of using an "all" module is an old hack that is no longer necessary. Today, the way to approach is to use a "package module". https://dlang.org/spec/module.html#package-module Given a package "mylib" containing multiple modules, create a file "mylib/package.d". Use the package name as the module name and follow it with your public imports: module mylib; public import mylib.foo, mylib.bar, mylib.baz; Then users of the package can simply: import mylib;
Nov 10 2019