On 4/10/18 4:37 AM, David Bennett wrote:
On Tuesday, 10 April 2018 at 08:10:32 UTC, Jonathan M Davis wrote:
Yes. They expect it to work, and as the language is currently
designed, it works perfectly well. In fact, it's even built into the
language. e.g.
int[] foo() pure
{
return [1, 2, 3, 4];
}
void main()
{
immutable arr = foo();
}
compiles thanks to the fact that the compiler can guarantee from the
signature of foo that its return value is unique.
Oh is that run at runtime? I thought D was just smart and did it using
CTFE.
Well, D could be smart enough and call a runtime function that says it's
moving data from thread-local to shared (or vice versa).
We also have std.exception.assumeUnique (which is just a cast to
immutable) as a way to document that you're guaranteeing that a
reference to an object is unique and therefore can be safely cast to
immutable.
Can't say I've used std.exception.assumeUnique, but I guess other people
have a use for it as it exists.
Would be nice if you could inject type checking information at compile
time without effecting the storage class. But thats a bit OT now.
assumeUnique is a library function, it could be instrumented to do the
right thing.
I think it's possible to do this in D, but you need language support.
-Steve