Const initialization issue, looking for solution

Kenji Hara k.hara.pg at gmail.com
Wed May 29 08:14:40 PDT 2013


With 2.063, this code would work.

struct S { int* ptr; }  // has mutable indirection

void main()
{
    immutable S si = function () pure
    {
        S sm;
        sm.ptr = new int;
        *sm.ptr = 10;
        return sm;  // construct and return mutable object
    }();
    static assert(is(typeof(*si.ptr) == immutable int));
    assert(*si.ptr == 10);
}

The local function would return an unique object, so it is implicitly
convertible to immutable.

Kenji Hara


2013/5/29 Jakob Ovrum <jakobovrum at gmail.com>

> On Wednesday, 29 May 2013 at 12:40:39 UTC, Dicebot wrote:
>
>> Why something like this is not usable?
>> -----------------------
>> int tmp;
>> try
>> {
>>    tmp = ...;
>> }
>> catch(..)
>> {
>> }
>> const(int) i = tmp;
>> -----------------------
>> Not really pretty but nothing crazy.
>>
>
> const(int) i = tmp; // fails when the type has mutable indirection.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130530/5d833d5c/attachment.html>


More information about the Digitalmars-d mailing list