DConf 2013 Day 1 Talk 2: Copy and Move Semantics in D by Ali Cehreli

Jonathan M Davis jmdavisProg at gmx.com
Wed May 15 10:32:03 PDT 2013


On Wednesday, May 15, 2013 09:42:06 Dicebot wrote:
> On Friday, 10 May 2013 at 12:08:10 UTC, Andrei Alexandrescu wrote:
> > ...

You probably should actually quote part of the message so that it's easier to 
figure out exactly which message you're replying to it.

> Presentation recommends to use assumeUnique in such cases as you
> can be sure that mutable entity only exists in calling scope and
> is practically immutable for the duration of function call.
> However, this is potentially dangerous, because foo may save
> reference of some kind to its immutable argument in global state
> and then your program is in undefined behavior. It is OK with
> Phobos string processing functions because common sense and open
> sources guarantee that no such stuff happens, but in general this
> sounds like a type system hole.

assumeUnique really should only be used when you construct something that you 
can't construct as immutable but want as immutable. Using it to simply pass to 
a function is a _bad_ idea in the general case. You can get away with it if 
you know exactly what the function does and code accordingly, but all it takes 
is the code being changed, and you could get some nasty bugs (especially if 
you were casting away immutable on the return value with the assemption that 
it was a slice of the original, and that assumption didn't hold true in the 
long term).

But really, what it comes down to is that in general, if functions need 
immutable(char)[] or are going to be copying the string to immutable, they 
should take string explicitly, but otherwise, they should probably be 
accepting const or inout or be templated. Most string functions in Phobos are 
templated. What's less clear is what to do when a function accepts strings but 
isn't really operating on them (e.g stuff in std.file or std.net.curl), as they 
may need immutable(char)[]. In that case, it depends on what's being done with 
the string. For better or worse though, at this point, I think that it's most 
common to just accept string for those cases. It's not something that always 
has a clearcut answer though.

- Jonathan M Davis


More information about the Digitalmars-d-announce mailing list