It turns out it's quite hard to have @safe pure nothrow functions. Oh, and const.

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 12 10:50:16 PDT 2014


On Fri, Sep 12, 2014 at 09:53:44AM +0000, Atila Neves via Digitalmars-d wrote:
> This happens to me all the time. I write a function, stick the
> aforementioned attributes on as a default then let the compiler tell
> me when I can't.
> 
> That happens a lot more often than I thought it would. Pretty much
> anytime I call a Phobos function I have to remove at least one of them
> but usually all three.

Yeah, I run into that a lot. Unfortunately, it just makes me give up and
not use those attributes.


> Is it similar for everyone else? Is it considered a problem?

Well, we've been getting a steady stream of Phobos PR's to annotate
Phobos functions so that they have maximal attributes. It's slow work,
but it's progressing. Not long ago, for example, std.format was made
pure and @safe, so now it is CTFE-able, which is a big boon for
compile-time metaprogramming. Additional help in this area would be
greatly appreciated!


> The other thing is I frequently have to "unconstify" my variables to
> get them accepted by Phobos functions as well.
> 
> I should've kept examples but I didn't, this is all from what I
> remember happening in the last week or so.
[...]

Keep in mind that D's const is much stronger than C++'s const, it
imposes *binary* const-ness, not just logical const-ness like in C++.
This means you can't do logically const operations like lazy class
object loading, caching, updating reference counts, etc.. Which in turn
means that there are less places where you can use const in D, than in
C++.

There are some rough corners, though, like iterating over a const
container. If the container is a linked-list, say, and you pass in a
const container, then all pointers in the container will be const as
well, by transitivity. However, that means you can't iterate over the
pointers, because, being const, they cannot be modified, so it's illegal
to update your iteration pointer! There are ways to work around this
without breaking the type system, but it does require extra work.


T

-- 
Public parking: euphemism for paid parking. -- Flora


More information about the Digitalmars-d mailing list