Hipreme's #1 Tip of the day

z z at z.com
Wed Oct 19 23:57:38 UTC 2022


On Wednesday, 19 October 2022 at 23:28:46 UTC, Hipreme wrote:
> Hey guys, I'm going to start making a tip of the day (although 
> I'm pretty sure I won't be able to give every day a tip), but 
> those things are really interesting to newcomers to know and 
> may be obvious to some of the old schoolers there.
>
> Always public import a type that the user (including you) is 
> expected to interact with:
>
> Imagine we have a module A that define a tree
>
> ```d
> module a;
> struct Tree(T){}
> ```
>
> If you create a module b containing a function that returns a 
> tree:
>
> ```d
> module b;
> import a;
> Tree!string getDirectoryTree(string entry){return null;}
> ```
> This is virtually unusable! One must `public import a: Tree;`
>
> This will make your API a lot easier to interact with, keep in 
> mind to always public import some type that is used from 
> another dependency like this, but try to not overdo it.

Will add an important tip for when handling variables of integer 
type smaller than `int` :
```D
ushort a, b, c;
c = a + b;//expression a + b will be promoted to int or compiler 
will emit a deprecation warning
c = cast(ushort) (a + b);//works fine
c = cast(typeof(c)) (a + b);//alternate form
c = cast(ushort) a + b;//bad, only "a" will be casted so you get 
promoted to int/warned anyway
c += a;//assigning operators' operands won't be promoted, with 
same types at least
auto d = a + b;//will probably be of type int
```
note: i didn't 101% check but the above holds true in my usage at 
least.


More information about the Digitalmars-d-learn mailing list