thisExePath purity

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 19 17:50:18 PDT 2016


On Tuesday, September 20, 2016 00:37:10 Stefan Koch via Digitalmars-d-learn 
wrote:
> On Tuesday, 20 September 2016 at 00:01:58 UTC, crimaniak wrote:
> > Hi!
> >
> > Is there situations when output of thisExePath() can be
> > different during runtime? If yes, what the reason?
> > If no, is this possible to mark it as pure in phobos?
> >
> > https://dlang.org/library/std/file/this_exe_path.html
>
> No way to do that.
> It does I/O.
> However you cheat.
> look for assumePure in https://dlang.org/phobos/std_traits.html
> and then you can create a wrapper for it that is fake pure.

Yes, you can cast a function pointer to force purity, but that's almost
always a bad idea. It makes far more sense to just grab the value once and
reuse it. The only time that I'd suggest using the casting trick for
something like this would be when you can't afford to use static
constructors, and you can't afford to pass the value around. Casting to pure
should be a tool of last resort. It _is_ an option though if you really have
no reasonable alternative.

Another thing to consider in this case is that casting like that would
actually be needlessly expensive if he actually needs to make the call more
than once. It would be far cheaper to just save the result and reuse it. And
if it's stored in an immutable module-level or static variable, then it can
be used in a pure function.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list