FYI: Be careful with imports when using public:

Salih Dincer salihdb at hotmail.com
Wed Oct 11 11:34:50 UTC 2023


On Wednesday, 11 October 2023 at 05:31:33 UTC, Jonathan M Davis 
wrote:
>
> The problem is not using the imports inside of the struct where 
> you have the public import (since whether imports are public or 
> not has no impact on the part of the code where they're 
> imported, just on other modules importing that code). The 
> problem is using them on the struct from within another module.

I see! In this situation there are 2+1 (the first one doesn't 
count) solutions if I do not remember wrong:

The first thing is not to use "public:" but still be careful. 
Because the imports may accidentally be in scope, as you 
mentioned.

Other is "Renamed Imports", a local name for an import can be 
given. For example:

```d
   public:

      import std.range.primitives : wl = walkLength;
```

Finally, use selective import again and define it inside the 
member function. For example:

```d
   public:

     size_t length() {
       import std.range.primitives : walkLength;
       return this.walkLength;
     }
```

Thank you very much for this information. There is a huge 
difference between "import" alone and "public import"!

SDB at 79


More information about the Digitalmars-d mailing list