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

Meta jared771 at gmail.com
Tue Apr 16 14:10:44 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.
>
> `@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?
>
> Can anyone save me some trouble and articulate why it would be 
> bad to have `pure` and/or `immutable` by default?
>
> Thanks,
> Mike

I think pure by default is a no-brainer as well. It's not nearly 
as restrictive in D to mark your function as pure as it is to 
have a pure function in other languages, due to weak purity - 
that seems to be a wholly unique invention on D's part and is 
just waiting for an academic paper or two to be written on the 
subject.

All pure restricts you from doing is accessing global variables 
in a function scope. Ironically, PHP got this right; you need to 
declare inside the function which global variables you are 
accessing to use them (I think this might be the case in python 
as well, but may be misremembering). You can still throw 
Errors/Exceptions and modify the arguments passed to the 
function, and if those arguments are implicitly convertible to 
immutable, you get all the nice properties of a strongly-pure 
function. I can't see a downside, OTOH, other than needing 
pure(false) or @impure or something to turn it off when needed.


More information about the Digitalmars-d mailing list