DIP49 - Define qualified postblit

Kenji Hara k.hara.pg at gmail.com
Sun Nov 10 03:07:48 PST 2013


2013/11/10 deadalnix <deadalnix at gmail.com>

> I like it up to the unique part. For mutable/const/immutable postblit, I
> do think this is it, you nailed it perfectly.
>

Thans for the comment, @deadlnix.

For the unique part, this become tricky, as we do not specify what is a
> unique expression. For this, I do not think that overloading inout make too
> much sense. inout already mean something else, and uniqueness is generally
> a useful concept that is not reserved to postblit. In fact, to make the
> unique posblit work, it is required that we define unique expression, and
> that is way beyond the scope of this DIP.
>

The unique expression concept is not a trick. It already exists in D.

Currently sometimes you can qualify returned objects from a pure function
with arbitrary qualifier.

int[] foo(int n) pure { ... }
int[] marr = foo(1);
const int[] carr = foo(1);
immutable int[] iarr = foo(1);

The reason why it's possible is that the pure function call `foo(1)` makes
unique expression.

Currently you can create arbitrary typed array object by using just only
one syntax.

int[] marr = [1,2,3];
const int[] carr = [1,2,3];
immutable int[] iarr = [1,2,3];

The reason why it's possible is that the array literal makes unique
expression.

So, the "unique expression" is the name for one another aspect of existing
D concept.


> Additionally, inout posblit make sense without changing the meaning of
> posblit, which make the proposal confusing.
>
> Let's make the posblit inout be what inout always has been : a wildcard
> for a qualifier in the callee that is known from the caller.
>

No, the unique postblit concept is strongly related to inout type qualifier.

Condider a case that copying "inout struct" inside inout function.

struct S {
    int[] arr;
    this(this) ??? { }
}
int[] foo(inout S src)
{
    S dst = src; // copy inout S to S
    return dst.arr;
}

If the struct S has postblit, what shold be done for "copying S from inout
to mutable"?

1. You cannot modify elements of arr field, because originally it may be
immutable.
2. You must re-initialize arr field by unique expression, otherwise it may
break type system

The requirements are exactly same as what necessary for unique postblit.
Essentially "creating unique copy" is exactly same as "treating the copy
source as inout".

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20131110/aae395ad/attachment.html>


More information about the Digitalmars-d mailing list