←back to thread

498 points azhenley | 1 comments | | HN request time: 0.334s | 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 #
josephg ◴[] No.45767891[source]
> 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.

Rust is also like this (let x = 5; / let mut x = 5;).

Or you can also use javascript, typescript and zig like this. Just default to declaring variables with const instead of let / var.

Or swift, which has let (const) vs var (mutable).

FP got there first, but you don't need to use F# to have variables default to constants. Just use almost any language newer than C++.

replies(3): >>45768692 #>>45769193 #>>45769524 #
Fire-Dragon-DoL ◴[] No.45769193[source]
In typescript and js you get immutable references, but the data is mutable. Definitely not the same thing
replies(1): >>45769942 #
dougherty ◴[] No.45769942[source]
You have `as const`. Yes I know it's not enforced at runtime, but the type system does support it.
replies(1): >>45771358 #
1. danenania ◴[] No.45771358[source]
There’s Object.freeze for enforcing at runtime.