Is it bad form to put code in package.d other than imports?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jan 3 18:00:24 UTC 2018


On Wed, Jan 03, 2018 at 12:43:52AM -0700, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Wednesday, January 03, 2018 06:10:10 Soulsbane via Digitalmars-d-learn 
> wrote:
> > I've only understood that imports should go in package.d. I'm seeing
> > more and more packages on code.dlang.org using it for the packages
> > primary code. Is this alright? As far as I can tell it's just bad
> > form. It would be nice to have one of the maintainers higher up the
> > food chain comment on this!
[...]
> In terms of functionality, there really isn't much special about
> package.d.  If there were, we probably wouldn't have been able to talk
> Walter into it.  We were able to precisely because public imports
> already worked in a way that allowed package.d to work. We just needed
> the feature to be added to make it possible to import a package. So,
> package.d allows that, but beyond that, it's just a normal module. It
> typically containts public imports for the rest of the package, but it
> doesn't have to, and it can contain whatever code you want to put in
> there. You can do whatever you want with it, though really, using it
> for much of anything at all beyond splitting up a package in place is
> beyond the scope of why it was introduced. But ultimately, there's
> nothing really special about package.d, and different folks have
> different ideas about it and how it should be used.
[...]

I personally have used package.d to put declarations that are common to
a particular package, e.g., package-global enums, constants, that sort
of thing. This is particular the case when the package's submodules
aren't usually directly imported by outside code, in which case I tend
to think of package.d as the "baseline, common declarations", and if
outside code demands, it can import a more specific submodule that
exposes a more specific API for that package.

As Jonathan said, you can use package.d pretty much for whatever you
want.


T

-- 
Claiming that your operating system is the best in the world because more people use it is like saying McDonalds makes the best food in the world. -- Carl B. Constantine


More information about the Digitalmars-d-learn mailing list