`@safe` by default. What about `@pure` and `immutable` by default?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu Apr 18 14:29:06 UTC 2019
On Thursday, April 18, 2019 4:07:57 AM MDT Eugene Wissner via Digitalmars-d
wrote:
> On Thursday, 18 April 2019 at 08:53:37 UTC, Jonathan M Davis
>
> wrote:
> > There _are_ rare cases where a piece of code that isn't
> > technically @noglobal is actually able to follow the compiler's
> > guarantees (e.g. std.datetime's LocalTime() is conceptually
> > pure because it always returns exactly the same value every
> > time it's called, but it has to create that value the first
> > time that it's called, because it was determined unacceptable
> > to have static constructors in Phobos - and that means casting
> > to pure).
>
> It isn't conceptually pure, it depends on the state of the word
> after compilation, it is just impure.
I don't see why the word after compilation would matter one whit. The
semantics are the same whether you use a static constructor to initialize
the underlying variable or do it lazily as it's currently doing (it's just
that lazy way requires a cast). It always returns exactly the same object
every time. That's what matters. And actually, given how pure works in D, it
could legally return a newly allocated object every time so long as its
value were the same (that would defeat the purpose of the singleton, put it
would be perfectly legal as far as D's pure goes and would even negate the
need for a cast). If you're worried about absolute functional purity, D
already threw that out the window when it allowed pure functions to do
imperative stuff. Instead, D just cares about how the function behaves from
the outside - LocalTime() behaves correctly with regard's to D's pure and
it's guarantees. It only needs a cast because it does it lazily instead of
using a static constructor like it did originally.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list