Jump to page: 1 26  
Page
Thread overview
Is there any real reason to use "const"?
Jan 24, 2022
rempas
Jan 24, 2022
rikki cattermole
Jan 24, 2022
rempas
Jan 24, 2022
Dom DiSc
Jan 24, 2022
Dennis
Jan 24, 2022
rempas
Jan 24, 2022
Dennis
Jan 24, 2022
Ali Çehreli
Jan 24, 2022
rempas
Jan 24, 2022
H. S. Teoh
Jan 25, 2022
rempas
Jan 24, 2022
H. S. Teoh
Jan 24, 2022
Dennis
Jan 24, 2022
Dennis
Jan 24, 2022
Ali Çehreli
Jan 24, 2022
H. S. Teoh
Jan 24, 2022
Patrick Schluter
Jan 25, 2022
rempas
Jan 24, 2022
vit
Jan 25, 2022
bauss
Jan 25, 2022
rempas
Jan 25, 2022
bauss
Jan 27, 2022
rempas
Jan 27, 2022
rempas
Jan 24, 2022
Dennis
Jan 24, 2022
rempas
Jan 24, 2022
Mark
Jan 24, 2022
Guillaume Piolat
Jan 24, 2022
rempas
Jan 24, 2022
Ali Çehreli
Jan 25, 2022
Timon Gehr
Jan 24, 2022
Patrick Schluter
Jan 25, 2022
rempas
Jan 24, 2022
Walter Bright
Jan 24, 2022
rempas
Jan 24, 2022
Walter Bright
Jan 24, 2022
H. S. Teoh
Jan 25, 2022
rempas
Jan 24, 2022
Walter Bright
Jan 24, 2022
Walter Bright
Jan 24, 2022
H. S. Teoh
Jan 24, 2022
jmh530
Jan 24, 2022
H. S. Teoh
Jan 24, 2022
Walter Bright
Jan 24, 2022
Paul Backus
Jan 24, 2022
rempas
Jan 24, 2022
Walter Bright
Jan 24, 2022
rempas
Jan 25, 2022
rempas
Jan 25, 2022
forkit
January 24, 2022

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?

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. To me, "const" has a lot of burdens like:

"should we use it all the time if we don't want to be sure that we won't modify a variable or only for critical ones?"

"Then in this case, why not make "const" the default and use another word to allow a variable to be mutable (just like Rust and other languages)?"

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 (which means than modifying them will not modify the original value). This is stupid because if I want to support "const" for a function, I need to make an extra instruction to copy the value to a new non-const variable that is created inside the function and make an extra instruction. Copying a value is not a slow operation but it can be for a big struct. And in any case, it is stupid and it pisses me off...

So yeah, I would like to know as soon as possible if there are any real reasons to support something like that so I can implement it.

What do you guys think?

January 24, 2022
If you are working with raw pointers, you need a way to express read only memory.

Because the CPU does have it, and if you tried to write to it, bye bye process.
January 24, 2022
On Monday, 24 January 2022 at 10:13:02 UTC, rikki cattermole wrote:
> If you are working with raw pointers, you need a way to express read only memory.
>
> Because the CPU does have it, and if you tried to write to it, bye bye process.

We would try to avoid working with pointers directly in read-only memory but in any case, I still think that the programmer should know what they are doing.

The problem you described is why most language use a "string" type that is immutable (in case you point to a string literal) but they also have a way to allow you to modify a "string" type in case you have allocated memory. So it's always up to you.

So that's how I see it and I can't see it going wrong. I mean, you will properly read documentation and learn the language right? Well that was sarcastic... no one properly reads documentation these days which is the reason why programmers do all these stuff bringing all the downsides along. I think we should not reward people that don't want to properly learn the language.

At the same time I understand that a lot of languages don't have a proper tutorial (book, say it however you want) to teach people about the language. Some don't even have a full language/library reference so even advanced programmer that want to dig cannot learn the language. So they can't blame people for not knowing the language in the end but this is the language creators fault to begin with and I don't plan on doing that. I will make sure that people can have learning the language.
January 24, 2022
On Monday, 24 January 2022 at 10:23:14 UTC, rempas wrote:
> The problem you described is why most language use a "string" type that is immutable (in case you point to a string literal) but they also have a way to allow you to modify a "string" type in case you have allocated memory. So it's always up to you.
>
You need const for functions that need to be able to take a mutable or immutable variable as parameter. And as you don't know which it is, you have to either propagate the constness or make a mutable copy if you want to call a function (depending if the called function has its parameter declared const or not). So better to declare const wherever that is possible, to _avoid_ making useless copies.
I would NEVER EVER declare a variable const. It's always either mutable or immutable. I would go as far as forbidding to declare variables const, it's just confusing and useless. const is only for parameters.
January 24, 2022

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

>

For example, you cannot past "const" variables to functions that take non-const parameters even if we are using variables which are ALWAYS copied (which means than modifying them will not modify the original value).

You can? This compiles:

struct S {
    int x;
    const(int)[] arr;
    immutable(char)[] str;
}

void main() {
    const S sc;
    immutable S si;
    f(sc); // mutable copy passed to f
    f(si); // mutable copy passed to f
}

void f(S s) {
    s.x = 3;
}

It will only error if the type has mutable indirections, because then you could violate const by changing the contents behind the array/pointer.

January 24, 2022

On Monday, 24 January 2022 at 10:49:36 UTC, Dom DiSc wrote:

>

I would NEVER EVER declare a variable const. It's always either mutable or immutable.

I usually mark local variables const because it's shorter than immutable, and immutable doesn't buy me anything over const in those cases.

January 24, 2022
On Monday, 24 January 2022 at 10:49:36 UTC, Dom DiSc wrote:
> You need const for functions that need to be able to take a mutable or immutable variable as parameter. And as you don't know which it is, you have to either propagate the constness or make a mutable copy if you want to call a function (depending if the called function has its parameter declared const or not). So better to declare const wherever that is possible, to _avoid_ making useless copies.
> I would NEVER EVER declare a variable const. It's always either mutable or immutable. I would go as far as forbidding to declare variables const, it's just confusing and useless. const is only for parameters.

That's what I'm saying. If there were no "const" and it was up to the programmer to decide what to do, we wouldn't had to worry about all that. Is you string pointing to a string literal? Then don't use the "add" function to add more data to it! Or maybe have a member in the "string type" that will check if the string points to a string literal and in this case, allocate memory (for both the old and the new value), copy the old value and then add the new value! This is just so simple and easy for both the creators of the language and the users

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? Any.. "practical" reason so to say.
January 24, 2022

On Monday, 24 January 2022 at 11:15:47 UTC, Dennis wrote:

>

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

>

For example, you cannot past "const" variables to functions that take non-const parameters even if we are using variables which are ALWAYS copied (which means than modifying them will not modify the original value).

You can? This compiles:

struct S {
    int x;
    const(int)[] arr;
    immutable(char)[] str;
}

void main() {
    const S sc;
    immutable S si;
    f(sc); // mutable copy passed to f
    f(si); // mutable copy passed to f
}

void f(S s) {
    s.x = 3;
}

It will only error if the type has mutable indirections, because then you could violate const by changing the contents behind the array/pointer.

I thought I tried it out some months ago and it wasn't possible. Well, turns out that I was wrong about the second part of my post. And that I'm an idiot of course but this is not something I didn't knew before neither something that will stop me....

January 24, 2022

On Monday, 24 January 2022 at 10:49:36 UTC, Dom DiSc wrote:

>

On Monday, 24 January 2022 at 10:23:14 UTC, rempas wrote:

>

The problem you described is why most language use a "string" type that is immutable (in case you point to a string literal) but they also have a way to allow you to modify a "string" type in case you have allocated memory. So it's always up to you.

...
I would NEVER EVER declare a variable const. It's always either mutable or immutable. I would go as far as forbidding to declare variables const, it's just confusing and useless. const is only for parameters.

const variable are necessary:

  • in generic code when you don't know if variable is mutable or immutable.
  • if you initialize variable from const function parameter or other const variables
  • when you don't want shared variable (immutable is implicitly shared, const is not).
  • ...
January 24, 2022

On Monday, 24 January 2022 at 10:06:49 UTC, 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?

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. To me, "const" has a lot of burdens like:

"should we use it all the time if we don't want to be sure that we won't modify a variable or only for critical ones?"

"Then in this case, why not make "const" the default and use another word to allow a variable to be mutable (just like Rust and other languages)?"

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 (which means than modifying them will not modify the original value). This is stupid because if I want to support "const" for a function, I need to make an extra instruction to copy the value to a new non-const variable that is created inside the function and make an extra instruction. Copying a value is not a slow operation but it can be for a big struct. And in any case, it is stupid and it pisses me off...

So yeah, I would like to know as soon as possible if there are any real reasons to support something like that so I can implement it.

What do you guys think?

AFAIK, const is a contentious topic. You can find many previous discussions on this forum and elsewhere revolving around the questions of how to use const, whether D's const is generally useful, the effect it has on APIs, whether we need tail-const/head-mutable in the language, etc. Jonathan M Davis wrote an article which covers some of these questions (and also considers C++'s const):
http://jmdavisprog.com/articles/why-const-sucks.html
The article is 4 years old but I don't think much has changed since then.

I know that Rust also has const (by default; non-const variable are marked with mut) but I don't have enough experience to give an honest opinion on it.

« First   ‹ Prev
1 2 3 4 5 6