Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing modules in a D project

I come from Java backgrounds and the problem of packaging is as follows then:

I can have many files under the same package, say com.parobay.io. I can then distribute this as a library, and the users will use it like this:

import com.parobay.io.Input;
import com.parobay.io.Output;

or

import com.parobay.io.*; // to import everything

So I can have a single "module (com.parobay.io) and classes defined in multiple files.

So how to I achieve the same in D? Do I have to create a directory com\parobay\io and there place two files called Input.d and Output.d or is there a smarter way?

In Java the rules are very strict, so it's hard to get it wrong. In D there are many possibilities. So are there any conventions, like one class per file, or file name equal to the name of class?

like image 456
Parobay Avatar asked May 28 '14 11:05

Parobay


People also ask

What are the modules in a project?

A module is a collection of source files and build settings that allow you to divide your project into discrete units of functionality. Your project can have one or many modules, and one module may use another module as a dependency. You can independently build, test, and debug each module.

What is module in F#?

In the context of F#, a module is a grouping of F# code, such as values, types, and function values, in an F# program. Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.


1 Answers

You can choose to do it basically the same as Java, though remember these items:

  • import foo.* doesn't work in D, but you CAN make a file called package.d in the directory which manually lists public import foo.Input; public import foo.Output; etc. which allows you to import the whole package.

  • ALWAYS put a module com.parobay.io.Input; or whatever line at the top of any file that is imported. Don't expect it to just work based on directory structure and filename. The directory structure actually isn't strictly necessary, it is just a convention to easily find the file. The module line at the top with the name is the authoritative thing the compiler checks.

  • D modules often have all lowercase names, but you can use uppercase ones if you like. I think it is nice to use a lowercase name like the class name, so you might call the module io.input and the class Input. The reason for this convention is sometimes filename case gets lost when transferring from system to system. But developers are pretty aware of case so in practice either way should work.

  • One class per file will work fine or you can put two tightly coupled classes together in the same file (they'll have access to each other's private members if they are in the same file).

See this page for more info: http://dlang.org/module especially search for the heading "Package Module"

like image 116
Adam D. Ruppe Avatar answered Sep 20 '22 02:09

Adam D. Ruppe