Using inout in delegates

Jacob Carlborg doob at me.com
Thu Oct 4 23:30:34 PDT 2012


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.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list