Using inout in delegates

Ali Çehreli acehreli at yahoo.com
Fri Oct 5 07:09:33 PDT 2012


On 10/04/2012 11:30 PM, Jacob Carlborg wrote:
> On 2012-10-04 16:18, Ali Çehreli wrote:
>
>> inout is like a template on 'mutable', const, and immutable; but it need
>> not be applied to templates. Here is a simple example that transfers the
>> mutability to the return type:
>
> I do think I understand how "inout" works, or at least I thought. I
> don't understand how the delegate affects anything. If I remove the
> delegate to this code it compiles:
>
> void foo (inout int[] arr)
> {
> auto a = arr[0];
> }
>
> My actual problem is that I want to be able to pass both mutable, const
> and immutable to a function. In that function I want the "mutability" to
> persist.
>
> I first used const for my function and that works, kind of. The problem
> if I do like this:
>
> void foo (const int[] arr)
> {
> auto s = typeid(typeof(arr)).toString;
> // here "s" will be something like "const(const(int)[])"
> }
>
> I doesn't matter if I pass immutable or mutable, "s" will always be
> "const(const(int)[]". So I thought, "hey, that's what inout is for, I
> use that", then everything blows up.
>
> I'm using "typeid(typeof(arr))" in my serialization library to get a
> string representation of a type. I would prefer that the mutability was
> correct.
>

This workaround makes the compiler happy:

void foo (inout(int)[] arr)
{
     auto a = (inout int) { auto b = arr[0]; };
}

But probably not what you want. :/

IIRC, inout has bugs and incomplete implementation. I think this should 
be in the bug database.

Ali


More information about the Digitalmars-d-learn mailing list