Mitigating the attribute proliferation - attribute inference for functions

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 17 11:23:53 PDT 2015


On Friday, 17 July 2015 at 17:36:08 UTC, Martin Nowak wrote:
> On Friday, 17 July 2015 at 16:40:56 UTC, Jonathan M Davis wrote:
>> On Friday, 17 July 2015 at 12:43:33 UTC, Martin Nowak wrote:
>>> =================== Attributes are hardly useful 
>>> ======================
>>
>> nothrow and pure - pure especially - help with reasoning about 
>> code. You actually lose out a fair bit with them IMHO when 
>> they're inferred, since a lot of their value is derived from 
>> the programmer knowing for sure that a function is nothrow or 
>> pure.
>
> But I already know if a function is "logically" pure or not 
> from what it does.
>
> http.get -> impure
> writeln -> impure
> swap -> pure
> reduce/map/filter/any algo -> pure
> containers -> pure (no longer when they use std.allocator?)
> gettime -> impure
> random -> impure
> format string -> pure
> fib -> pure (even when using a TLS memorization)
>
> Using global state in a non-obvious way is an indicator for bad 
> code.

Yes, but being able to have the compiler guarantee it is useful, 
and we've already seen some benefits from it in terms of implicit 
casts. I don't think that we've gotten the optimization benefits 
out of it that we should though. And while it's usually obvious 
that there's no global state, it's not always obvious - 
especially when you're dealing with higher level functions - and 
you can inadvertently end up with global state if a function 
buried in the chain didn't behave. Honestly, I think that pure is 
the biggest win out of all of the various attributes.

- Jonathan M Davis


More information about the Digitalmars-d mailing list