www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do you do "const nazi" in D?

reply Marc <jckj33 gmail.com> writes:
for a safe programming, since C/C++ times I try to make thing 
const as possible. locals, parameters etc anything which isn't 
going to change.
How do you do that in D? immutable everywhere?

for example:

	foreach(Field field; fields) {
		immutable string htmlOutputfile = genHTMLFilename();
do you use immutable here? and on:
 Field fromFile(in string filename) {
do you use in here if filename isn't going to change? I'm trying to make my own const nazi guide for D, if anyone would like to give your opinion on this, I would be glad.
Jan 03
next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, January 03, 2018 17:27:38 Marc via Digitalmars-d-learn wrote:
 for a safe programming, since C/C++ times I try to make thing
 const as possible. locals, parameters etc anything which isn't
 going to change.
 How do you do that in D? immutable everywhere?

 for example:
 foreach(Field field; fields) {

     immutable string htmlOutputfile = genHTMLFilename();
do you use immutable here? and on:
 Field fromFile(in string filename) {
do you use in here if filename isn't going to change? I'm trying to make my own const nazi guide for D, if anyone would like to give your opinion on this, I would be glad.
Well, D's const is far more restrictive than C++'s const (and immutable even more so). So, I think that you'll find that if you slap const and immutable everywhere, you're going to have a fair bit of difficulty. Feel free to use them as much as you can get away with, but a lot of us have basically given up on using const much, because it just doesn't work in far too many cases (e.g. ranges and const don't work together at all; ranges need to be mutated to iterate). const and immutable are transitive and don't have backdoors (casting away const to mutate is undefined behavior in D, unlike C++). This means that they they provide real guarantees but also that a lot of places where folks would happily use const in C++ simply can't use const in D, either because the transitivity makes something const that wouldn't have been const in C++, or because in C++, you'd use a backdoor like mutable, and that doesn't work in D. To use const or immutable in D, the objects have to not need to mutate in any way shape or form. "Logical" const simply isn't a thing in D. In general, I'd advise using immutable over const, since it provides better optimization opportunities and allows passing objects across threads if need be, but that can often mean having to design your types differently so that they can properly function as immutable. So, good luck, but I don't know very many people who try and use const everywehre in D like you might in C++, because it's often too much of a pain to make it work if it's even possible, and if you're using ranges and templates all over the place like many of us do, it's even worse. - Jonathan M Davis
Jan 03
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 1/3/18 12:27 PM, Marc wrote:
 for a safe programming, since C/C++ times I try to make thing const as 
 possible. locals, parameters etc anything which isn't going to change.
 How do you do that in D? immutable everywhere?
For parameters, I'd recommend const, not immutable, as const allows more to be passed in. If you need to return something from the parameters, inout should be what you use.
 for example:
 
     foreach(Field field; fields) {
         immutable string htmlOutputfile = genHTMLFilename();
immutable is OK here, as it's not a parameter. Note, you can drop the "string" type, as it will be inferred. A further note here -- immutable string can convert implicitly to mutable string, because the data underneath is also immutable. But you won't be so lucky with other data types. You may want to be cautious when declaring something const/immutable inside your functions, as it may not convert for calls to other libraries that haven't been properly "const nazi'd".
 and on:
 
 Field fromFile(in string filename) {
do you use in here if filename isn't going to change?
It depends completely on your internals. In this case, you are accepting a string by value. The string contents are referenced, but those are already immutable. You can't change anything the caller passed in, even if this was not tagged 'in'. All you can do is rebind filename locally to something else. If it were me, I would do this: Field fromFile(const(char)[] filename) Or potentially this if you never are going to change filename internally: Field fromFile(in char[] filename) This allows passing any flavor of char array, so it provides a wider amount of access.
 I'm trying to make my own const nazi guide for D, if anyone would like 
 to give your opinion on this, I would be glad.
If you send me email when you are ready, I'll review! As Jonathan implied, const is viral. If you try using proper const code with libraries that don't use it, you will have lots of trouble. So it's not always worth it. -Steve
Jan 04
prev sibling parent reply Mark <smarksc gmail.com> writes:
On Wednesday, 3 January 2018 at 17:27:38 UTC, Marc wrote:
 for a safe programming, since C/C++ times I try to make thing 
 const as possible. locals, parameters etc anything which isn't 
 going to change.
 How do you do that in D? immutable everywhere?

 for example:

	foreach(Field field; fields) {
		immutable string htmlOutputfile = genHTMLFilename();
do you use immutable here? and on:
 Field fromFile(in string filename) {
do you use in here if filename isn't going to change? I'm trying to make my own const nazi guide for D, if anyone would like to give your opinion on this, I would be glad.
You ma find Ali Cehreli's talk from DConf 2013 [1] useful. It includes some guidelines (basically recommendations) on how to use immutable and const. [1] https://www.youtube.com/watch?v=mPr2UspS0fE
Jan 04
parent Dukc <ajieskola gmail.com> writes:
On Thursday, 4 January 2018 at 13:52:14 UTC, Mark wrote:
 You ma find Ali Cehreli's talk from DConf 2013 [1] useful. It 
 includes some guidelines (basically recommendations) on how to 
 use immutable and const.
Same topic and same mentor here, but in text form: http://ddili.org/ders/d.en/const_and_immutable.html In my opinion it is an excellent answer to this question.
Jan 04