`@safe` by default. What about `@pure` and `immutable` by default?

RazvanN razvan.nitu1305 at gmail.com
Tue Apr 16 07:42:59 UTC 2019


On Tuesday, 16 April 2019 at 03:59:38 UTC, Mike Franklin wrote:
> I think I may have found a simple migration path to @safe by 
> default.  I'm still thinking it through, but if I can justify 
> it, I will write a DIP.
>
+1

> `@safe` by default is a no-brainer in my opinion, but `pure` 
> and `immutable` by default are less obvious.  With the 
> aforementioned potential DIP, I have an opportunity to correct 
> purity and mutability defaults as well...but is that something 
> we want to do?
>
I honestly think that data should be implicitly mutable. Data is 
naturally mutable
and it should be left upon the user to decide about immutability. 
Look at rust, it has `immutable` data by default but it offers a 
mechanism of rebinding immutable variables, so you may have 
something like this:

let x = 5;
.....
500 lines of code later
.....
println!("{}", x);   // 5 or something else?

You know that x is `immutable` but in those 500 lines of code you 
could always rebind x to another variable by doing `let x = 10`. 
This, in opinion, breaks the spirit of immutability where you 
know that once you see a variable initialized
it will never be changed (in @safe code).

As for `pure`, as long as we don't have the equivalent of `not 
pure`, I don't see how it can be the default

> Can anyone save me some trouble and articulate why it would be 
> bad to have `pure` and/or `immutable` by default?
>
> Thanks,
> Mike

Cheers,
RazvanN


More information about the Digitalmars-d mailing list