Bug 1297 – [module] Static modules, aka enforcing static import.

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-06-28T14:52:05Z
Last change time
2018-05-15T13:33:45Z
Keywords
spec
Assigned to
No Owner
Creator
Christopher Nicholson-Sauls

Comments

Comment #0 by ibisbasenji — 2007-06-28T14:52:05Z
I find myself using the very nice static-renaming-import scheme to provide namespacing capability. For example, rather than have a module full of database functions with, for example, a 'db_' prefix to avoid collisions, its useful to just use 'static import Db = foo.bar.Db;' and call all functions via 'Db.func()'. Since some modules are tailored toward use in this way, I feel it would be nice to have a way to specifically enforce our module being static-imported. The proposal is simple enough: allow the 'static' attribute on 'module' declerations, with the meaning that any corresponding 'import' must also be 'static'. Example: -----[ foo/bar/Db.d ]----- static module foo.bar.Db; void func () {} -----[ fred.d ]----- import foo.bar.Db; // <- Error: import for module 'foo.bar.Db' must be static -----[ greg.d ]----- static import foo.bar.Db; // <- Ok Naturally, static-modules should still play nice with renaming imports and selective imports.
Comment #1 by leandro.lucarella — 2009-11-13T15:32:20Z
I think static import should be the default, and there should be an explicit way to import everything, not the other way around.
Comment #2 by andrej.mitrovich — 2013-02-06T17:07:34Z
Since D is supposed to prevent symbol hijacking I don't see much benefit for this. If the user has conflicts he will use one of several techniques such as using static imports, using aliases, using renamed imports and/or selective imports. I don't see why you would force a module to be imported one way or another. It seems like a sure way to make importing modules annoying for the user.
Comment #3 by dmitry.olsh — 2018-05-15T13:33:45Z
A whole language feature to save on one-liner: static import Db = foo.bar.Db; > and call all functions via 'Db.func() Which is partly subjective style anyway.