inout and function/delegate parameters

Stewart Gordon smjg_1998 at yahoo.com
Mon Mar 5 17:34:09 PST 2012


On 05/03/2012 22:31, Steven Schveighoffer wrote:
> On Mon, 05 Mar 2012 11:17:41 -0500, Stewart Gordon <smjg_1998 at yahoo.com> wrote:
>
>> On 05/03/2012 13:49, Steven Schveighoffer wrote:
<snip>
>>> It's currently transitive, and this would break transitivity. If we want to look at
>>> fundamentally redefining inout so that it can break transitivity, then we can look at
>>> that. But I don't think this is a simple "add-on" to the current functionality.
>> <snip>
>>
>> Can you give an example of how it breaks transitivity?
>
> Using the above foo function:

Which has only one level of indirection.  So I can't see how transitivity comes into play.

> void main()
> {
> void bar(int *x) {*x = 5;}
> int i = 2;
> foo(&i, &bar); // this sets i to 5 via the inout reference we pass into it.
> }
<snip>

The foo function doesn't set i.  The bar function does.

The design of inout is that the function with inout parameters doesn't know or care about 
the constancy of its arguments, _therefore_ cannot modify the data.  The function that is 
passed in as a delegate, OTOH, _does_ know the constancy of its argument, and so _can_ 
modify it if the constancy permits.  This is necessary to support foreach in the way we're 
talking about.

Stewart.


More information about the Digitalmars-d mailing list