Re: [Vala] Confusing closure behavior



C#(Mono) would say

int[]indices={0};
foreach(variinindices)
{
i=999;<-- Error CS1656: Cannot assign to 'i' because it is a 'foreach iteration variable' (CS1656)
}


Am 09.04.19 um 11:23 schrieb Nor Jaidi Tuah:
On Tue, 2019-04-09 at 08:25 +0200, Vivien Kraus wrote:
Digging a little deeper, I have some more confusion.  This makes the
for-loop and the foreach-loop behave differently.  What do you think
of:

// capturing the for variable should capture a copy if the variable
is
// local to the for loop

public delegate void Thunk();

int main() {
       Thunk thunk = () => {};
       for (int i = 0; i < 1; i++) {
               thunk = () => {
                       stderr.printf ("%d = 0, right?\n", i);
               };
       }
       thunk ();
       int[] indices = { 0 };
       foreach (var i in indices) {
               thunk = () => {
                       stderr.printf ("%d = 0, right?\n", i);
               };
       }
       thunk ();
       return 0;
}
I believe foreach pass the values inside the array.
e.g.,

    foreach (var i in indices) {
       i = 999;
    }

will not change indices[0] at all. A more strict language
will even forbid that "i = 999".


Nice day
Nor Jaidi Tuah




PRIVILEGED/CONFIDENTIAL information may be contained in this message. If you are neither the addressee 
(intended recipient) nor an authorised recipient of the addressee, and have received this message in error, 
please destroy this message (including attachments) and notify the sender immediately. STRICT PROHIBITION: 
This message, whether in part or in whole, should not be reviewed, retained, copied, reused, disclosed, 
distributed or used for any purpose whatsoever. Such unauthorised use may be unlawful and may contain 
material protected by the Official Secrets Act (Cap 153) of the Laws of Brunei Darussalam. DISCLAIMER: 
We/This Department/The Government of Brunei Darussalam, accept[s] no responsibility for loss or damage 
arising from the use of this message in any manner whatsoever. Our messages are checked for viruses but we do 
not accept liability for any viruses which may be transmitted in or with this message.
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]