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

Eugene Wissner belka at caraus.de
Tue Apr 16 05:44:22 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

immutable by default would be good if D wasn't a systems 
programming language. Immutability plays nice with a GC, but even 
with a GC it probably requires a different one which is more 
efficient with immutable data and I'm not sure we can have one, 
because we have still to support pointers, pointer arithmetic and 
similar stuff that can break garbage collection.

On the other hand what Phobos/D assumes that some things are 
mutable. What happens if I say:

auto range = map!(a => a )([1, 2, 3]);

Will it return an immutable range I can't work with?

Immutability shouldn't be default at least as long as there isn't 
something like __mutable or _metadata or head const for custom 
types.

As for pure, I think it would be better to just remove it instead 
of making it default. A year ago, after my first years with a lot 
of D, I was totally crazy about pure. But after that time I began 
to write again some JS and PHP and wondered first "How can I 
write programs without pure?". And now I would say it isn't bad 
at all without it.

But if there is no way around pure, I would say yes, pure by 
default would be reasonable even if I would prefer 
"@pure(false)/@pure(true)".


More information about the Digitalmars-d mailing list