Spurious imports in Phobos ?
Jacob Carlborg
doob at me.com
Wed Nov 9 23:16:58 PST 2011
On 2011-11-09 19:14, Jonathan M Davis wrote:
> On Wednesday, November 09, 2011 05:42 Jacob Carlborg wrote:
>> On 2011-11-09 14:16, Trass3r wrote:
>>>> Phobos contains a lot of templates and if a template isn't
>>>> instantiated it won't be compiled. Meaning there can be hidden compile
>>>> errors if you start to remove imports and they will not show until a
>>>> template that uses something from the import is instantiate.
>>>
>>> Wouldn't it be possible/better then to move the imports into those
>>> template functions?
>>
>> I probably would. Having imports in non-global scope is quite a new
>> feature and I guess nobody has either thought of the idea or just
>> haven't had the time yet.
>
> You also get the question of what to do if a lot of templated functions use
> the same import. Sure, you could put the import in each individual function so
> that it doesn't get imported if you don't use any of those functions, but then
> you've duplicated the same import in a bunch of places. It's not an entirely
> clear issue.
Didn't think of that.
> And I don't think that it ever affects the size of the executable. If you're
> dealing with static libraries, then all of the functions that aren't used
> shouldn't be compiled in. And if you're using a shared library, everything in
> the library is going to need to be there anyway (which would just affect the
> size of the library, not the executably anyway). So, I don't see how
> unnecessary imports would be a problem with regards to executable size.
>
> The bigger problem is static constructors and circular dependencies. If the
> wrong modules import one another when they don't need to, it can result in
> circular dependencies which will then cause the runtime to exit with an error
> when you try and run your program. So that - and the fact that it's just
> cleaner - is a good reason to not have unnecessary imports in a module.
> However, the circular dependency situation can be made _worse_ by putting
> imports within a template, since not only is there no way to detect the
> circular dependency at compile time, but whether you _have_ a circular
> dependency can depend on whether a particular template was instantiated or
> not. So, this is definitely not a clear-cut issue.
That would be bad.
> I'd definitely say that if there are only one or two functions in a module
> which need a particular import, then it's probably better to restrict that
> import to those functions, but beyond that, it's better to just stick them at
> the module level. But since (as you point out) local imports are quite new, I
> really don't think that much of Phobos is taking advantage of them even if it
> should be. That'll probably change over time, but it's not exactly a high
> priority.
>
> - Jonathan M Davis
Yes, exactly.
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list