January 24, 2022

On Monday, 24 January 2022 at 11:23:38 UTC, rempas wrote:

>

My point is if there is a true reason to implement "const" itself other than just protecting programmers that haven't read the documentation and are trying to modify a string literal?

There's this idea among some programmers that to prevent software bugs, we just need more well-trained / competent people, "like myself". This opinion tends to wane over time as you start writing those bugs you thought you'd never write.

I never understood the idea of people always including {} braces in if-statements and for-loops. The idea is to prevent this:

if (x)
    y();
    z();

What idiot would write something like that, z() is obviously not part of the if-statement anymore right? One day I had a single-line for-loop body, and to fix an error I quickly added an import statement above it, pushing the for-loop body outside the for-loop scope. Oops.

Yesterday I wrote a bug like this:

void alphabeta(ref Field field, /*...*/) {
    // (...)
    Field fieldCopy = field;
    field.modify(); // < should be fieldCopy.modify()
    alphabeta(fieldCopy);
    // (...)
}

I was debugging it and noticed that I might as well mark the field parameter const since it wasn't supposed to change. The next compile I immediately got an error pointing at the bug!

Usually I mark variables/functions as strict as possible as early as possible, const / scope / pure / @safe etc. I don't know how many bugs I would have written when I wouldn't do that, but it's also comforting knowing that I don't have to worry about accidentally breaking these properties I rely on when I modify a part of the code days or weeks later. Because that's how it gets you: sure, you can do these checks yourself the first time you write a function, but will you do them again every time you make a modification in the future? The compiler can, you likely won't.

That said, it can also be annoying marking all function parameters const appropriately to convince the compiler that you indeed don't modify a const variable. I've heard of C++ people applying a macro #define const to a remove all const from a const-correct codebase because they don't like dealing with it. If you don't think it's worth it, you can not mark parameters const if you also don't mark your variables const.

January 24, 2022

On Monday, 24 January 2022 at 10:06:49 UTC, rempas wrote:

>

What do you guys think?

const has utility at interface boundaries. You don't want an API to be used incorrectly.
If it's internal code though, it only helps the maintainers, and is often line noise.

January 24, 2022

On 1/24/22 5:06 AM, rempas wrote:

>

Rather than just tell the compiler to not allow me to modify a variable (like I don't know what I'm doing with my program), are there any reason to use "const" variables?

If you want the compiler to help you prevent mistakes, yes. If you don't, then no.

Other than that, if you want to write a library and have it most accessible to others that may prefer to use const, you should use const, as it will allow the most usage.

>

Other than out of curiosity, I'm actually asking because I'm writing a transpiler and I want to know if I should support "const" (or the concept of immutability in general) or not.

immutable/const is purely a compile-time concept. It's not reflected in the final binary, so it's not necessary to forward the attributes to a language that doesn't support it.

Same thing with types (see for instance, TypeScript compiled to JavaScript).

-Steve

January 24, 2022
On 1/24/2022 2:06 AM, rempas wrote:
> Also another problem is that I don't like the way "const" is treated in functions. For example, you cannot past "const" variables to functions that take non-const parameters even if we are using variables which are ALWAYS copied 

Not sure what you mean. This compiles:

  int foo(const int);

  int bar(int i)
  {
    return foo(i);
  }
January 24, 2022
On 1/24/2022 6:40 AM, Steven Schveighoffer wrote:
> immutable/const is purely a compile-time concept. It's not reflected in the final binary, so it's not necessary to forward the attributes to a language that doesn't support it.

Immutable global data gets placed in read-only memory sections. Read-only memory sections are nice in a demand-paged virtual system, as the pages they are in never have to be copied because they are never marked as "dirty".
January 24, 2022

On Monday, 24 January 2022 at 14:22:55 UTC, Guillaume Piolat wrote:

>

const has utility at interface boundaries. You don't want an API to be used incorrectly.
If it's internal code though, it only helps the maintainers, and is often line noise.

I mean, I can understand and agree the cases where people will just make mistakes because they are humans or because they are coding at a time they should be sleeping but like I said, I think that why should start writing good documentation and people should start reading it. Thanks for your thoughts!

January 24, 2022

On Monday, 24 January 2022 at 14:40:33 UTC, Steven Schveighoffer wrote:

>

On 1/24/22 5:06 AM, rempas wrote:

>

Rather than just tell the compiler to not allow me to modify a variable (like I don't know what I'm doing with my program), are there any reason to use "const" variables?

If you want the compiler to help you prevent mistakes, yes. If you don't, then no.

Other than that, if you want to write a library and have it most accessible to others that may prefer to use const, you should use const, as it will allow the most usage.

>

Other than out of curiosity, I'm actually asking because I'm writing a transpiler and I want to know if I should support "const" (or the concept of immutability in general) or not.

immutable/const is purely a compile-time concept. It's not reflected in the final binary, so it's not necessary to forward the attributes to a language that doesn't support it.

Same thing with types (see for instance, TypeScript compiled to JavaScript).

-Steve

Thanks for your time! My question is if you think that it will be very very bad to not include it in my language or not. I'm slowly changing my mind after seeing all these comments and thinking about including it tho...

January 24, 2022
On Monday, 24 January 2022 at 15:40:56 UTC, Walter Bright wrote:
> Not sure what you mean. This compiles:
>
>   int foo(const int);
>
>   int bar(int i)
>   {
>     return foo(i);
>   }

Actually this compiles but what I thought that didn't was something like this:

```
void mul_num(int num) {
  num *= 2;
}

void main() {
  const int number = 10;
  mul_num(number);
}

```

But it turns out that it compiles but I would swear that I tried something similar to this and it wouldn't compile on both D and C.
January 24, 2022

On Monday, 24 January 2022 at 15:44:54 UTC, Walter Bright wrote:

>

Immutable global data gets placed in read-only memory sections. Read-only memory sections are nice in a demand-paged virtual system, as the pages they are in never have to be copied because they are never marked as "dirty".

Read only memory? So in the same place where string literals are placed? This sounds cool and it's really something considerable! Does this offer things like security as date will be able to be created once and not get modified again? I hope I'm not asking for too much...

January 24, 2022
On 1/24/22 04:31, Dennis wrote:

> I never understood the idea of people always including {} braces in
> if-statements and for-loops.
[...]
> What idiot would write something like that,

Yeah, that thought disappears once one realizes that humans are mistake-making machines. :)

> One day I had a single-line for-loop
> body, and to fix an error I quickly added an `import` statement above
> it, pushing the for-loop body outside the for-loop scope. Oops.

I've done its counterpart just two days ago by commenting out one line (in somebody else's code):

  foreach(i; 0..2)
    // foo();

  bar();

Oops! Now bar() is executed multiple times.

I am saddened with this skipped-braces "optimization" because all that risk for just to skip writing two characters! Wow! Now... that's... interesting... :) Humans are really interesting...

Ali