best/proper way to declare constants ?

someone someone at somewhere.com
Thu Aug 5 00:47:06 UTC 2021


What are the pros/cons of the following approaches ?

```d
/// first day with D:

public const dstring gstrWhatever = "...";

/// next:

public immutable dstring gstrWhatever = "...";

/// next:

public immutable dstring gstrWhatever;

this() {

    gstrWhatever = "...";

}

/// next (manifest-constant):

public immutable enum gstrWhatever = "...";
```

Are manifest-constants the proper way to go ?

In [http://ddili.org/ders/d.en/enum.html] Ali says:

"We have discussed that it is important to avoid magic constants 
and instead to take advantage of the enum feature ..."

```d
enum fileName = "list.txt";
```

"Such constants are rvalues and they are called manifest 
constants."

"It is possible to create manifest constants of arrays and 
associative arrays as well. However, as we will see later in the 
Immutability chapter, enum arrays and associative arrays may have 
hidden costs."


More information about the Digitalmars-d-learn mailing list