Selective import seems to be importing more than just specified symbol

Mike Parker aldacron at gmail.com
Wed Jan 8 08:30:09 UTC 2025


On Wednesday, 8 January 2025 at 02:07:46 UTC, WhatMeWorry wrote:

>
> rdmd -main -unittest redblacktree.d
>
> This error is returned:
>
> honeycomb.d(11): Error: unable to read module `sdl`
> honeycomb.d(11):        Expected 'bindbc\sdl.d' or 
> 'bindbc\sdl\package.d' in one of the following import paths:
> import path[0] = .
> import path[1] = C:\D\dmd2\windows\bin64\..\..\src\phobos
> import path[2] = 
> C:\D\dmd2\windows\bin64\..\..\src\druntime\import
>
> honeycomb.d module is just:
>
> -----------------------------------------------------------
> module honeycomb;
>
> import bindbc.sdl;
>
> struct Location   // holds a hex of a hexboard
> {
>     int r;  // row
>     int c;  // colum
> }
> -----------------------------------------------------------
>
> I thought that the line
> import honeycomb : Location;
> would only import the symbol struct Location?

Selective imports affect which symbols in an imported module are 
accessible. They have no effect on imports in other modules.

In your `redblacktree` module, you are telling the compiler that 
you only care about `Location` and don't want any other symbols 
in `honeycomb` to be accessible. If you had any other symbols in 
`honeycomb`, like another struct type or a function, you wouldn't 
be able to access them in `redblacktree`.

However, the compiler still needs to parse the imports that 
`honeycomb` brings in so that it knows what symbols are 
accessible in the honeycomb module itself. The selective import 
has no impact on that. Just imagine what would happen if you 
actually called any SDL functions from inside `Location`, say in 
a constructor or something. How would that work if your selective 
import caused the compiler to ignore the `import bindbc.sdl`?

You need `bindbc.sdl` on your import path for this to work.






More information about the Digitalmars-d-learn mailing list