←back to thread

498 points azhenley | 5 comments | | HN request time: 0s | source
Show context
munchler ◴[] No.45767832[source]
> Making almost every variable const at initialization is good practice. I wish it was the default, and mutable was a keyword.

It's funny how functional programming is slowly becoming the best practice for modern code (pure functions, no side-effects), yet functional programming languages are still considered fringe tech for some reason.

If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back.

replies(19): >>45767880 #>>45767882 #>>45767891 #>>45767892 #>>45767898 #>>45767926 #>>45767975 #>>45767989 #>>45768118 #>>45768188 #>>45768230 #>>45769840 #>>45769875 #>>45770104 #>>45770306 #>>45771134 #>>45771926 #>>45772136 #>>45775848 #
skeezyjefferson ◴[] No.45771134[source]
> If you want a language where const is the default and mutable is a keyword

whats the difference between const and mutable?

replies(1): >>45771189 #
1. Thorrez ◴[] No.45771189[source]
"const" means something can't be changed. "mutable" means it can be changed.

You don't need both a "const" keyword and a "mutable" keyword in a programming language. You only need 1 of the keywords, because the other can be the default. munchler is saying the "const" keyword shouldn't exist, and instead all variables should be constant by default, and we should have a "mutable" keyword to mark variables as mutable.

As opposed to how C++ works today, where there is no "mutable" keyword, variables are mutable by default, and we use a "const" keyword to mark them constant.

replies(1): >>45771571 #
2. alcover ◴[] No.45771571[source]
> you don't need both a "const" keyword and a "mutable" keyword

What if the lang has pointers? How express read-only?

replies(2): >>45773144 #>>45781369 #
3. skeezyjefferson ◴[] No.45773144[source]
compiler flags with line and column number seems like the easiest way
4. Thorrez ◴[] No.45781369[source]
You can make everything read-only by default, and if you need non-read-only, you use "mutable".
replies(1): >>45784475 #
5. alcover ◴[] No.45784475{3}[source]
You need two keywords. One for assignability and one for writability :

    const ptr;  // can't reassign, can't write-through (if r/o by default)
    const mut ptr;  // can write