www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Hipreme's #1 Tip of the day

reply Hipreme <msnmancini hotmail.com> writes:
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.
Oct 19 2022
next sibling parent z <z z.com> writes:
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.
Oct 19 2022
prev sibling parent Sergey <kornburn yandex.ru> writes:
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.
Cool things! Thanks for you tips! Looking forward to see new ones to come soon :) How do you think - if it will be possible to save them in some dedicated page (even just GitHub or blog) and share the link with community somewhere here: https://wiki.dlang.org/The_D_Programming_Language (The structure of the page looks a little messy) https://wiki.dlang.org/Tutorials - for example section of “Best practices” And also two my fav sites in this theme D idioms - https://p0nce.github.io/d-idioms/ and D Garden - https://garden.dlang.io/ They could be good examples of how the page with tips could look like.
Oct 20 2022